Skip to content

Commit

Permalink
implementing player text input via AnvilUI;
Browse files Browse the repository at this point in the history
implementing server name and server host name changing via AnvilUI
  • Loading branch information
PrinzPizza42 committed Jan 6, 2025
1 parent 7768f3f commit 3fe4e05
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/main/java/de/j/deathMinigames/dmUtil/DmUtil.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package de.j.deathMinigames.dmUtil;

import de.j.deathMinigames.main.Config;
import de.j.deathMinigames.main.HandlePlayers;
import de.j.deathMinigames.main.PlayerData;
import de.j.stationofdoom.main.Main;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class DmUtil {
private static DmUtil instance;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/j/deathMinigames/dmUtil/TestCMD.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.j.deathMinigames.dmUtil;

import de.j.stationofdoom.util.Tablist;
import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -35,5 +36,6 @@ public boolean canUse(@NotNull CommandSender sender) {
public void execute(@NotNull CommandSourceStack stack, @NotNull String[] args) {
Player player = (Player) stack.getSender();
player.sendMessage("test");
Tablist.setHostetBy(args[0]);
}
}
113 changes: 113 additions & 0 deletions src/main/java/de/j/deathMinigames/listeners/AnvilListener.java
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);
}
}
15 changes: 13 additions & 2 deletions src/main/java/de/j/deathMinigames/listeners/InventoryListener.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.j.deathMinigames.listeners;

import de.j.deathMinigames.dmUtil.DmUtil;
import de.j.deathMinigames.main.HandlePlayers;
import de.j.deathMinigames.main.PlayerData;
import de.j.deathMinigames.settings.AnvilUI;
import de.j.deathMinigames.settings.MainMenu.InventoryMenus;
import de.j.stationofdoom.util.translations.TranslationFactory;
import net.kyori.adventure.text.Component;
Expand All @@ -13,12 +15,16 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.*;
import de.j.deathMinigames.main.Config;
import de.j.stationofdoom.main.Main;
import de.j.deathMinigames.minigames.Minigame;
import de.j.deathMinigames.settings.GUI;
import de.j.deathMinigames.settings.MainMenu;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.view.AnvilView;

import java.util.HashMap;
import java.util.UUID;
Expand All @@ -37,7 +43,6 @@ public void onSettingsClick(InventoryClickEvent event) {
MainMenu mainMenu = new MainMenu();
InventoryHolder invHolder = event.getInventory().getHolder();
Minigame minigame = Minigame.getInstance();

UUID ID;
int slot = event.getSlot();
if(slot < 0) {
Expand Down Expand Up @@ -245,6 +250,12 @@ private void handleMainMenuGUI(InventoryClickEvent event, Player player, MainMen
MainMenu.getDifficulty().addBackButton(player);
MainMenu.getDifficulty().showInventory(player);
break;
case 4:
MainMenu.getSetHost().showInventory(player);
break;
case 5:
MainMenu.getSetServerName().showInventory(player);
break;
}
}

Expand Down
50 changes: 49 additions & 1 deletion src/main/java/de/j/deathMinigames/main/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.j.deathMinigames.main;

import de.j.deathMinigames.database.PlayerDataDatabase;
import de.j.stationofdoom.listener.PlayerJoin;
import de.j.stationofdoom.util.Tablist;
import org.bukkit.Location;

import de.j.stationofdoom.main.Main;
Expand Down Expand Up @@ -115,6 +117,15 @@ public void cloneConfigToPlugin() {
config.set("Database.schema", "public");
Main.getPlugin().saveConfig();
}
//Tablist
if(config.contains("Tablist")) {
if(config.contains("Tablist.ServerName")) {
Tablist.setServerName(config.getString("Tablist.ServerName"));
}
if(config.contains("Tablist.HostetBy")) {
Tablist.setHostetBy(config.getString("Tablist.HostetBy"));
}
}
}

public void cloneWaitingListLocationToPlugin(World world) {
Expand Down Expand Up @@ -195,7 +206,6 @@ public synchronized void setCostToLowerTheDifficulty(int cost) {
* @param time The time limit for minigame decision in seconds to be set.
*/
public synchronized void setTimeToDecideWhenRespawning(int time) {

configTimeToDecideWhenRespawning = time;
Main.getPlugin().getConfig().set("TimeToDecideWhenRespawning", time);
Main.getPlugin().saveConfig();
Expand Down Expand Up @@ -313,4 +323,42 @@ public HashMap<String, String> getDatabaseConfig() {
}
return databaseConnectionInfo;
}

public void setServerName(String serverName) {
if(!Main.getPlugin().getConfig().contains("Tablist.ServerName")) {
Main.getPlugin().getConfig().set("Tablist.ServerName", serverName);
Main.getPlugin().saveConfig();
}
else if(!Main.getPlugin().getConfig().getString("Tablist.ServerName").equals(serverName)) {
Main.getPlugin().getConfig().set("Tablist.ServerName", serverName);
Main.getPlugin().saveConfig();
}
}

public String getServerName() {
if(Main.getPlugin().getConfig().contains("Tablist.ServerName")) {
return Main.getPlugin().getConfig().getString("Tablist.ServerName");
}
else {
Main.getMainLogger().warning("ServerName not found in config!");
return null;
}
}

public void setHostetBy(String serverName) {
if(!Main.getPlugin().getConfig().contains("Tablist.HostetBy") || !Main.getPlugin().getConfig().getString("Tablist.HostetBy").equals(serverName)) {
Main.getPlugin().getConfig().set("Tablist.HostetBy", serverName);
Main.getPlugin().saveConfig();
}
}

public String getHostetBy() {
if(Main.getPlugin().getConfig().contains("Tablist.HostetBy")) {
return Main.getPlugin().getConfig().getString("Tablist.HostetBy");
}
else {
Main.getMainLogger().warning("HostetBy not found in config!");
return null;
}
}
}
55 changes: 55 additions & 0 deletions src/main/java/de/j/deathMinigames/settings/AnvilUI.java
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;
}
}
1 change: 0 additions & 1 deletion src/main/java/de/j/deathMinigames/settings/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void addPlayerHeads(HashMap<UUID, PlayerData> knownPlayers) {
ItemStack head = new ItemStack(Material.PLAYER_HEAD, 1);
SkullMeta skullMeta = (SkullMeta) head.getItemMeta();
skullMeta.setOwnerProfile(Bukkit.createProfile(playerData.getUUID()));
Main.getMainLogger().info("Skull has owner: " + skullMeta.hasOwner());
skullMeta.displayName(Component.text(playerData.getName()));
head.setItemMeta(skullMeta);

Expand Down
21 changes: 20 additions & 1 deletion src/main/java/de/j/deathMinigames/settings/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ public enum InventoryMenus {
PARKOUR_START_HEIGHT,
PARKOUR_LENGTH,
COST_TO_LOWER_THE_DIFFICULTY,
TIME_TO_DECIDE_WHEN_RESPAWNING
TIME_TO_DECIDE_WHEN_RESPAWNING,
SET_HOST,
SET_SERVER_NAME
}

public enum AnvilUIs {
SET_SERVER_NAME,
SET_HOST_NAME,
}

public MainMenu() {
Expand Down Expand Up @@ -68,6 +75,14 @@ public synchronized static GUI getTimeToDecideWhenRespawning() {
return timeToDecideWhenRespawning;
}

public synchronized static AnvilUI getSetHost() {
return setHost;
}

public synchronized static AnvilUI getSetServerName() {
return setServerName;
}

private static final GUI introduction = new GUI("Introduction", true, false);
private static final GUI difficulty = new GUI("Difficulty", true, true);
private static final GUI usesPlugin = new GUI("UsesPlugin", true, false);
Expand All @@ -77,6 +92,8 @@ public synchronized static GUI getTimeToDecideWhenRespawning() {
private static final GUI parkourLength = new GUI("ParkourLength", false, false);
private static final GUI costToLowerTheDifficulty = new GUI("CostToLowerTheDifficulty", false, false);
private static final GUI timeToDecideWhenRespawning = new GUI("TimeToDecideWhenRespawning", false, false);
private static final AnvilUI setHost = new AnvilUI(AnvilUIs.SET_HOST_NAME);
private static final AnvilUI setServerName = new AnvilUI(AnvilUIs.SET_SERVER_NAME);

/**
* Opens the main menu for the given player, where the player can
Expand Down Expand Up @@ -108,6 +125,8 @@ private void addSubmenus() {
addClickableItemStack("Introduction", Material.GREEN_CONCRETE, 1, 1);
addClickableItemStack("UsesPlugin", Material.GREEN_CONCRETE, 1, 2);
addClickableItemStack("Difficulty", Material.RED_CONCRETE, 1, 3);
addClickableItemStack("SetHost", Material.BOOK, 1, 4);
addClickableItemStack("SetServerName", Material.BOOK, 1, 5);
}

/**
Expand Down
Loading

0 comments on commit 3fe4e05

Please sign in to comment.