Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing player text input via AnvilUI; #196

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 0 additions & 39 deletions src/main/java/de/j/deathMinigames/dmUtil/TestCMD.java

This file was deleted.

118 changes: 118 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,118 @@
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 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;
PrinzPizza42 marked this conversation as resolved.
Show resolved Hide resolved
private TranslationFactory tf = new TranslationFactory();

@EventHandler
public void onAnvilPrepare(PrepareAnvilEvent event) {
if(!(event.getViewers().getFirst() instanceof Player player)) {
Main.getMainLogger().warning("Anvil prepare event has invalid viewer type");
return;
}
Location loc = event.getInventory().getLocation();
if(loc == null) {
Main.getMainLogger().warning("Anvil prepare event has no location");
return;
}
AnvilView anvilView = event.getView();
if(anvilView == null) {
Main.getMainLogger().warning("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(event.getSlot() != 2) return;
if (MainMenu.getSetHost().compareLocIDTo(loc)) {
event.setCancelled(true);
if (hostName == null) return;
Tablist.setHostedBy(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 (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) {
if(event == null || player == null) {
Main.getMainLogger().warning("parameters are null: " + event + " " + player + "!");
return;
}
ItemStack output = new ItemStack(Material.GREEN_CONCRETE);
ItemMeta outputItemMeta = output.getItemMeta();
if(outputItemMeta == null) {
Main.getMainLogger().warning("outputItemMeta is null!");
return;
}
outputItemMeta.displayName(Component.text(tf.getTranslation(player, "anvilOutput")));
output.setItemMeta(outputItemMeta);
event.setResult(output);

event.getView().setRepairCost(0);
}
}
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
51 changes: 48 additions & 3 deletions src/main/java/de/j/deathMinigames/main/Config.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package de.j.deathMinigames.main;

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

import de.j.stationofdoom.main.Main;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.util.*;

Expand Down Expand Up @@ -115,6 +114,15 @@ public void cloneConfigToPlugin() {
config.set("Database.schema", "public");
Main.getPlugin().saveConfig();
}
//Tablist
if(config.contains("Tablist")) {
if(config.contains("Tablist.ServerName") && config.get("Tablist.ServerName") != null) {
Tablist.setServerName(config.getString("Tablist.ServerName"));
}
if(config.contains("Tablist.HostedBy") && config.get("Tablist.HostedBy") != null) {
Tablist.setHostedBy(config.getString("Tablist.HostedBy"));
}
}
}

public void cloneWaitingListLocationToPlugin(World world) {
Expand Down Expand Up @@ -195,7 +203,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 +320,42 @@ public HashMap<String, String> getDatabaseConfig() {
}
return databaseConnectionInfo;
}

public synchronized 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();
}
}
PrinzPizza42 marked this conversation as resolved.
Show resolved Hide resolved

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 synchronized void setHostedBy(String serverName) {
if(!Main.getPlugin().getConfig().contains("Tablist.HostedBy") || !Main.getPlugin().getConfig().getString("Tablist.HostedBy").equals(serverName)) {
Main.getPlugin().getConfig().set("Tablist.HostedBy", serverName);
Main.getPlugin().saveConfig();
}
}

public String getHostedBy() {
if(Main.getPlugin().getConfig().contains("Tablist.HostedBy")) {
return Main.getPlugin().getConfig().getString("Tablist.HostedBy");
}
else {
Main.getMainLogger().warning("HostedBy not found in config!");
return null;
}
}
}
82 changes: 82 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,82 @@
package de.j.deathMinigames.settings;

import de.j.stationofdoom.util.Tablist;
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.World;
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 Location loc = null;
private ItemStack input = new ItemStack(Material.RED_CONCRETE);
Player player = null;
private MainMenu.AnvilUIs title;

public AnvilUI(MainMenu.AnvilUIs title) {
if(title == null) throw new IllegalArgumentException("Title cannot be null");
this.title = title;
createUniqueLocation();
}

public void showInventory(Player playerToShowTheInvTo) {
if(playerToShowTheInvTo == null) return;
this.player = playerToShowTheInvTo;
playerToShowTheInvTo.openAnvil(loc, true);
setInputMeta(playerToShowTheInvTo);
playerToShowTheInvTo.getOpenInventory().getTopInventory().setItem(0, input);
}

@Override
public @NotNull Inventory getInventory() {
return null;
}

/**
* Random location used as a unique identifier for the anvil UI.
* This prevents conflicts between multiple anvil UIs.
*/
private void createUniqueLocation() {
World world = Bukkit.getWorld("world");
if(world == null) throw new IllegalStateException("world `world´ could not be found");
loc = new Location(world,
ThreadLocalRandom.current().nextInt(0, 1000),
ThreadLocalRandom.current().nextInt(0, 1000),
ThreadLocalRandom.current().nextInt(0, 1000));
}

public boolean compareLocIDTo(Location loc) {
return loc.getBlockX() == this.loc.getBlockX() && loc.getBlockZ() == this.loc.getBlockZ();
}

private void setInputMeta(Player player) {
ItemMeta inputMeta = input.getItemMeta();
String inputItemName = null;
if(player == null) {
inputMeta.displayName(Component.text("default"));
}
else {
switch (title) {
case SET_HOST_NAME -> inputItemName = Tablist.getHostedBy();
case SET_SERVER_NAME -> inputItemName = Tablist.getServerName();
default -> throw new IllegalArgumentException("Title: " + title + " is not supported");
}
if(inputItemName == null) {
inputMeta.displayName(Component.text(new TranslationFactory().getTranslation(player, "noNameSet")));
}
else {
inputMeta.displayName(Component.text(inputItemName));
}
}
input.setItemMeta(inputMeta);
}
}
Loading
Loading