-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing player text input via AnvilUI;
implementing server name and server host name changing via AnvilUI
- Loading branch information
1 parent
7768f3f
commit 3fe4e05
Showing
12 changed files
with
308 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/de/j/deathMinigames/listeners/AnvilListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package de.j.deathMinigames.listeners; | ||
|
||
import de.j.deathMinigames.dmUtil.DmUtil; | ||
import de.j.deathMinigames.settings.MainMenu; | ||
import de.j.stationofdoom.main.Main; | ||
import de.j.stationofdoom.util.Tablist; | ||
import de.j.stationofdoom.util.translations.TranslationFactory; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.kyori.adventure.text.format.TextColor; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.Sound; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.event.inventory.InventoryCloseEvent; | ||
import org.bukkit.event.inventory.InventoryType; | ||
import org.bukkit.event.inventory.PrepareAnvilEvent; | ||
import org.bukkit.inventory.AnvilInventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.bukkit.inventory.view.AnvilView; | ||
|
||
public class AnvilListener implements Listener { | ||
private String serverName; | ||
private String hostName; | ||
private TranslationFactory tf = new TranslationFactory(); | ||
|
||
@EventHandler | ||
public void onAnvilPrepare(PrepareAnvilEvent event) { | ||
Player player = (Player) event.getViewers().getFirst(); | ||
if(player == null) { | ||
Main.getMainLogger().info("Anvil prepare event has no player"); | ||
return; | ||
} | ||
Location loc = event.getInventory().getLocation(); | ||
if(loc == null) { | ||
Main.getMainLogger().info("Anvil prepare event has no location"); | ||
return; | ||
} | ||
AnvilView anvilView = event.getView(); | ||
if(anvilView == null) { | ||
Main.getMainLogger().info("Anvil prepare event has no anvilView"); | ||
return; | ||
} | ||
String renameText = anvilView.getRenameText(); | ||
if(MainMenu.getSetHost().compareLocIDTo(loc)) { | ||
finishAnvilInvAfterOpening(event, player); | ||
if(renameText == null) return; | ||
hostName = renameText; | ||
} | ||
else if(MainMenu.getSetServerName().compareLocIDTo(loc)) { | ||
finishAnvilInvAfterOpening(event, player); | ||
if(renameText == null) return; | ||
serverName = renameText; | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onAnvilClick(InventoryClickEvent event) { | ||
if(event.getInventory().getType() == InventoryType.ANVIL) { | ||
Location loc = event.getInventory().getLocation(); | ||
if (loc == null) return; | ||
Player player = (Player) event.getWhoClicked(); | ||
if(player == null) return; | ||
if (MainMenu.getSetHost().compareLocIDTo(loc)) { | ||
event.setCancelled(true); | ||
if(event.getSlot() == 2) { | ||
if (hostName == null) return; | ||
Tablist.setHostetBy(hostName); | ||
event.getView().close(); | ||
DmUtil.getInstance().playSoundAtLocation(player.getLocation(), 0.5f, Sound.BLOCK_ANVIL_USE); | ||
player.sendMessage(Component.text("Host name: " + hostName).color(NamedTextColor.GOLD)); | ||
} | ||
} else if (MainMenu.getSetServerName().compareLocIDTo(loc)) { | ||
event.setCancelled(true); | ||
if(event.getSlot() == 2) { | ||
if (serverName == null) return; | ||
Tablist.setServerName(serverName); | ||
event.getView().close(); | ||
DmUtil.getInstance().playSoundAtLocation(player.getLocation(), 0.5f, Sound.BLOCK_ANVIL_USE); | ||
player.sendMessage(Component.text("Server name: " + serverName).color(NamedTextColor.GOLD)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onAnvilClose(InventoryCloseEvent event) { | ||
if(event.getInventory().getType() == InventoryType.ANVIL) { | ||
Location loc = event.getInventory().getLocation(); | ||
if(loc == null) return; | ||
AnvilInventory anvilInventory = (AnvilInventory) event.getView().getTopInventory(); | ||
if(MainMenu.getSetHost().compareLocIDTo(loc)) { | ||
anvilInventory.clear(); | ||
} | ||
else if(MainMenu.getSetServerName().compareLocIDTo(loc)) { | ||
anvilInventory.clear(); | ||
} | ||
} | ||
} | ||
|
||
private void finishAnvilInvAfterOpening(PrepareAnvilEvent event, Player player) { | ||
ItemStack output = new ItemStack(Material.GREEN_CONCRETE); | ||
ItemMeta outputItemMeta = output.getItemMeta(); | ||
outputItemMeta.displayName(Component.text(tf.getTranslation(player, "anvilOutput"))); | ||
output.setItemMeta(outputItemMeta); | ||
event.setResult(output); | ||
event.getView().setRepairCost(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.j.deathMinigames.settings; | ||
|
||
import de.j.deathMinigames.main.Config; | ||
import de.j.stationofdoom.main.Main; | ||
import de.j.stationofdoom.util.translations.TranslationFactory; | ||
import net.kyori.adventure.text.Component; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.InventoryHolder; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class AnvilUI implements InventoryHolder { | ||
private final Location loc = new Location(Bukkit.getWorld("world"), ThreadLocalRandom.current().nextInt(0, 1000), ThreadLocalRandom.current().nextInt(0, 1000), ThreadLocalRandom.current().nextInt(0, 1000)); | ||
private final ItemStack firstSlot = new ItemStack(Material.RED_CONCRETE); | ||
|
||
public AnvilUI(MainMenu.AnvilUIs title) { | ||
ItemMeta paperMeta = firstSlot.getItemMeta(); | ||
switch (title) { | ||
case SET_HOST_NAME: | ||
if(Config.getInstance().getHostetBy() != null) paperMeta.displayName(Component.text(Config.getInstance().getHostetBy())); | ||
else paperMeta.displayName(Component.text("kein Name gesetzt / no name set")); | ||
break; | ||
case SET_SERVER_NAME: | ||
if(Config.getInstance().getServerName() != null) paperMeta.displayName(Component.text(Config.getInstance().getServerName())); | ||
else paperMeta.displayName(Component.text("kein Name gesetzt / no name set")); | ||
break; | ||
default: | ||
paperMeta.displayName(Component.text("")); | ||
} | ||
firstSlot.setItemMeta(paperMeta); | ||
} | ||
|
||
public void showInventory(Player playerToShowTheInvTo) { | ||
if(playerToShowTheInvTo == null) return; | ||
playerToShowTheInvTo.openAnvil(loc, true); | ||
playerToShowTheInvTo.getOpenInventory().getTopInventory().addItem(firstSlot); | ||
} | ||
|
||
@Override | ||
public @NotNull Inventory getInventory() { | ||
return null; | ||
} | ||
|
||
public boolean compareLocIDTo(Location loc) { | ||
if(loc.getBlockX() == this.loc.getBlockX() && loc.getBlockZ() == this.loc.getBlockZ()) return true; | ||
else return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.