diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java index 2724d76..599587b 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java @@ -28,7 +28,7 @@ } @Override - protected void handleMessage(ChallengeMessageType challengeMessageType, GlobalServer globalServer, DataInputStream data) throws IOException { + protected void handleMessage(ChallengeMessageType challengeMessageType, GlobalServer globalServer, DataInputStream data) throws IOException { String serverName = data.readUTF(); switch (challengeMessageType) { case TIMER -> { diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java index 3638a14..dddb6d2 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java @@ -39,9 +39,9 @@ private static ChallengesJoinEntities plugin; private Config config; private ChallengesGlobalDataHelper globalDataHelper; - private Map entityData; - private Map entityServerMapping; - private Set availableServers; + private final Map entityData; + private final Map entityServerMapping; + private final Set availableServers; private CategoriesConfig categoriesConfig; private AdventureMapsConfig adventureMapsConfig; public NMSUtils nmsUtils; @@ -50,7 +50,7 @@ public ChallengesJoinEntities() { this.entityData = new HashMap<>(); this.entityServerMapping = new HashMap<>(); - this.availableServers = new HashSet(); + this.availableServers = new HashSet<>(); } @Override @@ -79,7 +79,7 @@ adventurerouter.addCommandMapping(new AdventureAddMapCommand(this), "addmap"); adventurerouter.addCommandMapping(new AdventureMapsCommand(this), "maps"); adventurerouter.addCommandMapping(new AdventureTestMapCommand(this), "testmap"); - adventurerouter.addCommandMapping(new ChallengesRemovePlayerServer(this), "removeplayerfromserver"); + adventurerouter.addCommandMapping(new ChallengesRemovePlayerServer(), "removeplayerfromserver"); } Bukkit.getPluginManager().registerEvents(new EntityListener(this), this); Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this); @@ -120,7 +120,7 @@ } public void despawnPiglin(String serverName) { - if(entityData.get(serverName) != null){ + if (entityData.get(serverName) != null) { despawnPiglin(entityData.get(serverName).getEntityUUID(), serverName); } } diff --git a/src/main/java/de/fanta/challengesjoinentities/Config.java b/src/main/java/de/fanta/challengesjoinentities/Config.java index e7ce5d6..2177dbf 100644 --- a/src/main/java/de/fanta/challengesjoinentities/Config.java +++ b/src/main/java/de/fanta/challengesjoinentities/Config.java @@ -13,9 +13,9 @@ public class Config { - private ChallengesJoinEntities plugin; - private Set challengeServers; - private Set adventureServers; + private final ChallengesJoinEntities plugin; + private final Set challengeServers; + private final Set adventureServers; public Config(ChallengesJoinEntities plugin) { this.plugin = plugin; @@ -51,7 +51,7 @@ double x = locSection.getDouble("x"); double y = locSection.getDouble("y"); double z = locSection.getDouble("z"); - float yaw = (float)locSection.getDouble("yaw"); + float yaw = (float) locSection.getDouble("yaw"); Location location = new Location(world, x, y, z, yaw, 0.0F); challengeServers.add(new Server(serverName, server.getString("gpLocation"), server.getString("dir"), ServerType.CHALLENGES, plugin, location)); @@ -68,7 +68,7 @@ double x = locSection.getDouble("x"); double y = locSection.getDouble("y"); double z = locSection.getDouble("z"); - float yaw = (float)locSection.getDouble("yaw"); + float yaw = (float) locSection.getDouble("yaw"); Location location = new Location(world, x, y, z, yaw, 0.0F); adventureServers.add(new Server(serverName, server.getString("gpLocation"), server.getString("dir"), ServerType.ADVENTURE, plugin, location)); diff --git a/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java b/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java index ac67829..3d78c16 100644 --- a/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java +++ b/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java @@ -65,9 +65,7 @@ } public String createCustomEntityName() { - String statusString = serverStatus == ServerStatus.ONLINE ? ChatUtil.GREEN + "ONLINE" : - serverStatus == ServerStatus.OFFLINE ? ChatUtil.RED + "OFFLINE" : - serverStatus == ServerStatus.RUNNING ? ChatUtil.ORANGE + "RUNNING" : ""; + String statusString = serverStatus == ServerStatus.ONLINE ? ChatUtil.GREEN + "ONLINE" : serverStatus == ServerStatus.OFFLINE ? ChatUtil.RED + "OFFLINE" : serverStatus == ServerStatus.RUNNING ? ChatUtil.ORANGE + "RUNNING" : ""; String playerCountString = ChatUtil.BLUE + "(" + playerCount + "/" + maxPlayers + ")"; return ChatUtil.BLUE + serverDisplayName + " " + statusString + (serverStatus != ServerStatus.OFFLINE ? (" " + playerCountString) : ""); diff --git a/src/main/java/de/fanta/challengesjoinentities/Server.java b/src/main/java/de/fanta/challengesjoinentities/Server.java index 32faf2f..a7e324d 100644 --- a/src/main/java/de/fanta/challengesjoinentities/Server.java +++ b/src/main/java/de/fanta/challengesjoinentities/Server.java @@ -1,5 +1,10 @@ package de.fanta.challengesjoinentities; +import org.apache.commons.io.FileUtils; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; + import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -9,13 +14,6 @@ import java.util.UUID; import java.util.logging.Level; -import org.apache.commons.io.FileUtils; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - public class Server { public static HashMap serverPlayers = new HashMap<>(); @@ -152,7 +150,7 @@ plugin.getLogger().log(Level.SEVERE, line); int exitCode = process.waitFor(); plugin.getLogger().log(Level.SEVERE, "Exited with error code : " + exitCode); - } catch (IOException|InterruptedException e) { + } catch (IOException | InterruptedException e) { e.printStackTrace(); } } diff --git a/src/main/java/de/fanta/challengesjoinentities/ServerType.java b/src/main/java/de/fanta/challengesjoinentities/ServerType.java index 1ddaadf..9ac3ee0 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ServerType.java +++ b/src/main/java/de/fanta/challengesjoinentities/ServerType.java @@ -7,7 +7,7 @@ private String dir; - ServerType(String dir){ + ServerType(String dir) { this.dir = dir; } diff --git a/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMap.java b/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMap.java index daec339..d636b31 100644 --- a/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMap.java +++ b/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMap.java @@ -1,6 +1,7 @@ package de.fanta.challengesjoinentities.adventure; import de.fanta.challengesjoinentities.ChallengesJoinEntities; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -13,56 +14,56 @@ import org.bukkit.inventory.meta.ItemMeta; public class AdventureMap { - private final ChallengesJoinEntities plugin; - - private final String name; - - private final ItemStack item; - - private final String creator; - - private final String webLink; - - public AdventureMap(ChallengesJoinEntities plugin, String name, Material material, String creator, String webLink) { - this.name = name; - this.creator = creator; - this.webLink = webLink; - this.plugin = plugin; - this.item = new ItemStack((material != null) ? material : Material.STONE); - ItemMeta meta = this.item.getItemMeta(); - meta.setDisplayName("" + ChatUtil.ORANGE + ChatColor.BOLD + getName()); - List lore = new ArrayList<>(); - lore.add(""); - lore.add(ChatUtil.GREEN + "Erbauer: " + ChatUtil.BLUE + creator); - lore.add(ChatUtil.GREEN + "MapLink: " + ChatUtil.BLUE + "Rechts Klick"); - lore.add(ChatUtil.GREEN + "Map laden: " + ChatUtil.BLUE + "Links Klick"); - meta.setLore(lore); - this.item.setItemMeta(meta); - } - - public AdventureMap(ChallengesJoinEntities plugin, String name, ConfigurationSection section) { - this(plugin, name, Material.matchMaterial(Objects.requireNonNull(section.getString("item"))), section.getString("creator"), section.getString("web_link")); - } - - public String getName() { - return this.name; - } - - public ItemStack getItem() { - return this.item; - } - - public String getCreator() { - return this.creator; - } - - public String getWebLink() { - return this.webLink; - } - - public void save(ConfigurationSection section) { - section.set("item", this.item.getType().getKey().toString()); - section.set("creator", this.creator); - section.set("web_link", this.webLink); - } + private final ChallengesJoinEntities plugin; + + private final String name; + + private final ItemStack item; + + private final String creator; + + private final String webLink; + + public AdventureMap(ChallengesJoinEntities plugin, String name, Material material, String creator, String webLink) { + this.name = name; + this.creator = creator; + this.webLink = webLink; + this.plugin = plugin; + this.item = new ItemStack((material != null) ? material : Material.STONE); + ItemMeta meta = this.item.getItemMeta(); + meta.setDisplayName("" + ChatUtil.ORANGE + ChatColor.BOLD + getName()); + List lore = new ArrayList<>(); + lore.add(""); + lore.add(ChatUtil.GREEN + "Erbauer: " + ChatUtil.BLUE + creator); + lore.add(ChatUtil.GREEN + "MapLink: " + ChatUtil.BLUE + "Rechts Klick"); + lore.add(ChatUtil.GREEN + "Map laden: " + ChatUtil.BLUE + "Links Klick"); + meta.setLore(lore); + this.item.setItemMeta(meta); + } + + public AdventureMap(ChallengesJoinEntities plugin, String name, ConfigurationSection section) { + this(plugin, name, Material.matchMaterial(Objects.requireNonNull(section.getString("item"))), section.getString("creator"), section.getString("web_link")); + } + + public String getName() { + return this.name; + } + + public ItemStack getItem() { + return this.item; + } + + public String getCreator() { + return this.creator; + } + + public String getWebLink() { + return this.webLink; + } + + public void save(ConfigurationSection section) { + section.set("item", this.item.getType().getKey().toString()); + section.set("creator", this.creator); + section.set("web_link", this.webLink); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMapsConfig.java b/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMapsConfig.java index a765573..89f01a9 100644 --- a/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMapsConfig.java +++ b/src/main/java/de/fanta/challengesjoinentities/adventure/AdventureMapsConfig.java @@ -2,76 +2,78 @@ import com.google.common.collect.ImmutableMap; import de.fanta.challengesjoinentities.ChallengesJoinEntities; + import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; + import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.Nullable; public class AdventureMapsConfig { - private final ChallengesJoinEntities plugin; - - private final Map maps = new HashMap<>(); - - private final File file; - - private final YamlConfiguration config; - - public AdventureMapsConfig(ChallengesJoinEntities plugin) { - this.plugin = plugin; - this.file = new File(plugin.getDataFolder(), "adventure_maps.yml"); - this.config = new YamlConfiguration(); - } - - private boolean fileExists() throws IOException { - if (!this.file.exists()) - return this.file.createNewFile(); - return true; - } - - public void load() { - try { - if (fileExists()) { - this.config.load(this.file); - this.config.getKeys(false).forEach(key -> { - ConfigurationSection section = this.config.getConfigurationSection(key); - if (section != null) { - AdventureMap map = new AdventureMap(this.plugin, key, section); - if (!this.maps.containsKey(key)) - this.maps.put(key, map); - } - }); - } - } catch (IOException|org.bukkit.configuration.InvalidConfigurationException e) { - e.printStackTrace(); - } - } - - public void save() { - this.maps.forEach((key, map) -> map.save(this.config.createSection(key))); - try { - this.config.save(this.file); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public boolean addMap(AdventureMap map) { - if (!this.maps.containsKey(map.getName())) { - this.maps.put(map.getName(), map); - return true; - } - return false; - } - - public Map getMaps() { - return ImmutableMap.copyOf(this.maps); - } - - @Nullable - public AdventureMap getMap(String name) { - return this.maps.get(name); - } + private final ChallengesJoinEntities plugin; + + private final Map maps = new HashMap<>(); + + private final File file; + + private final YamlConfiguration config; + + public AdventureMapsConfig(ChallengesJoinEntities plugin) { + this.plugin = plugin; + this.file = new File(plugin.getDataFolder(), "adventure_maps.yml"); + this.config = new YamlConfiguration(); + } + + private boolean fileExists() throws IOException { + if (!this.file.exists()) + return this.file.createNewFile(); + return true; + } + + public void load() { + try { + if (fileExists()) { + this.config.load(this.file); + this.config.getKeys(false).forEach(key -> { + ConfigurationSection section = this.config.getConfigurationSection(key); + if (section != null) { + AdventureMap map = new AdventureMap(this.plugin, key, section); + if (!this.maps.containsKey(key)) + this.maps.put(key, map); + } + }); + } + } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) { + e.printStackTrace(); + } + } + + public void save() { + this.maps.forEach((key, map) -> map.save(this.config.createSection(key))); + try { + this.config.save(this.file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean addMap(AdventureMap map) { + if (!this.maps.containsKey(map.getName())) { + this.maps.put(map.getName(), map); + return true; + } + return false; + } + + public Map getMaps() { + return ImmutableMap.copyOf(this.maps); + } + + @Nullable + public AdventureMap getMap(String name) { + return this.maps.get(name); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/adventure/CategoriesConfig.java b/src/main/java/de/fanta/challengesjoinentities/adventure/CategoriesConfig.java index 8c8740b..e0b1d03 100644 --- a/src/main/java/de/fanta/challengesjoinentities/adventure/CategoriesConfig.java +++ b/src/main/java/de/fanta/challengesjoinentities/adventure/CategoriesConfig.java @@ -2,76 +2,78 @@ import com.google.common.collect.ImmutableMap; import de.fanta.challengesjoinentities.ChallengesJoinEntities; + import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; + import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.Nullable; public class CategoriesConfig { - private final ChallengesJoinEntities plugin; - - private final Map categories = new HashMap<>(); - - private final File file; - - private final YamlConfiguration config; - - public CategoriesConfig(ChallengesJoinEntities plugin) { - this.plugin = plugin; - this.file = new File(plugin.getDataFolder(), "categories.yml"); - this.config = new YamlConfiguration(); - } - - private boolean fileExists() throws IOException { - if (!this.file.exists()) - return this.file.createNewFile(); - return true; - } - - public void load() { - try { - if (fileExists()) { - this.config.load(this.file); - this.config.getKeys(false).forEach(key -> { - ConfigurationSection section = this.config.getConfigurationSection(key); - if (section != null) { - Category category = new Category(this.plugin, key, section); - if (!this.categories.containsKey(key)) - this.categories.put(key, category); - } - }); - } - } catch (IOException|org.bukkit.configuration.InvalidConfigurationException e) { - e.printStackTrace(); - } - } - - public void save() { - this.categories.forEach((key, category) -> category.save(this.config.createSection(key))); - try { - this.config.save(this.file); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public boolean addCategory(Category category) { - if (!this.categories.containsKey(category.getName())) { - this.categories.put(category.getName(), category); - return true; - } - return false; - } - - public Map getCategories() { - return ImmutableMap.copyOf(this.categories); - } - - @Nullable - public Category getCategory(String name) { - return this.categories.get(name); - } + private final ChallengesJoinEntities plugin; + + private final Map categories = new HashMap<>(); + + private final File file; + + private final YamlConfiguration config; + + public CategoriesConfig(ChallengesJoinEntities plugin) { + this.plugin = plugin; + this.file = new File(plugin.getDataFolder(), "categories.yml"); + this.config = new YamlConfiguration(); + } + + private boolean fileExists() throws IOException { + if (!this.file.exists()) + return this.file.createNewFile(); + return true; + } + + public void load() { + try { + if (fileExists()) { + this.config.load(this.file); + this.config.getKeys(false).forEach(key -> { + ConfigurationSection section = this.config.getConfigurationSection(key); + if (section != null) { + Category category = new Category(this.plugin, key, section); + if (!this.categories.containsKey(key)) + this.categories.put(key, category); + } + }); + } + } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) { + e.printStackTrace(); + } + } + + public void save() { + this.categories.forEach((key, category) -> category.save(this.config.createSection(key))); + try { + this.config.save(this.file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean addCategory(Category category) { + if (!this.categories.containsKey(category.getName())) { + this.categories.put(category.getName(), category); + return true; + } + return false; + } + + public Map getCategories() { + return ImmutableMap.copyOf(this.categories); + } + + @Nullable + public Category getCategory(String name) { + return this.categories.get(name); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/adventure/Category.java b/src/main/java/de/fanta/challengesjoinentities/adventure/Category.java index 0e67ae3..a01449a 100644 --- a/src/main/java/de/fanta/challengesjoinentities/adventure/Category.java +++ b/src/main/java/de/fanta/challengesjoinentities/adventure/Category.java @@ -1,14 +1,6 @@ package de.fanta.challengesjoinentities.adventure; -import de.fanta.challenges.Challenges; import de.fanta.challengesjoinentities.ChallengesJoinEntities; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; - import de.fanta.challengesjoinentities.ChatUtil; import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; @@ -16,61 +8,64 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -public class Category { - private final String name; - - private final ItemStack item; - - private final Set maps; +import java.util.*; +import java.util.stream.Collectors; - private final ChallengesJoinEntities plugin = ChallengesJoinEntities.getPlugin(); - - public Category(String name, Material material) { - this(name, material, new HashSet<>()); - } - - public Category(ChallengesJoinEntities plugin, String name, ConfigurationSection section) { - this(name, Material.matchMaterial(Objects.requireNonNull(section.getString("item"))), section.getStringList("maps").stream().map(mapName -> { +public class Category { + private final String name; + + private final ItemStack item; + + private final Set maps; + + private final ChallengesJoinEntities plugin = ChallengesJoinEntities.getPlugin(); + + public Category(String name, Material material) { + this(name, material, new HashSet<>()); + } + + public Category(ChallengesJoinEntities plugin, String name, ConfigurationSection section) { + this(name, Material.matchMaterial(Objects.requireNonNull(section.getString("item"))), section.getStringList("maps").stream().map(mapName -> { AdventureMap map = plugin.getAdventureMapsConfig().getMap(mapName); if (map == null) - plugin.getLogger().warning("Die map \"" + mapName + "\" existiert nicht!"); + plugin.getLogger().warning("Die map \"" + mapName + "\" existiert nicht!"); return map; - }).filter(Objects::nonNull).collect(Collectors.toSet())); - } - - public Category(String name, Material material, Set maps) { - this.name = name; - this.maps = maps; - this.item = new ItemStack((material != null) ? material : Material.STONE); - ItemMeta meta = this.item.getItemMeta(); - meta.setDisplayName("" + ChatUtil.ORANGE + ChatColor.BOLD + getName()); - List lore = new ArrayList<>(); - lore.add(""); - lore.add(ChatUtil.GREEN + "Maps: " + ChatUtil.BLUE + maps.size()); - meta.setLore(lore); - this.item.setItemMeta(meta); - } - - public String getName() { - return this.name; - } - - public ItemStack getItem() { - return this.item; - } - - public Set getMaps() { - return this.maps; - } - - public void addMap(AdventureMap map) { - this.maps.add(map); - plugin.saveCategoriesAndMaps(); - plugin.loadCategoriesAndMaps(); - } - - public void save(ConfigurationSection section) { - section.set("item", this.item.getType().getKey().toString()); - section.set("maps", this.maps.stream().map(AdventureMap::getName).collect(Collectors.toList())); - } + }).filter(Objects::nonNull).collect(Collectors.toSet())); + } + + public Category(String name, Material material, Set maps) { + this.name = name; + this.maps = maps; + this.item = new ItemStack((material != null) ? material : Material.STONE); + ItemMeta meta = this.item.getItemMeta(); + meta.setDisplayName("" + ChatUtil.ORANGE + ChatColor.BOLD + getName()); + List lore = new ArrayList<>(); + lore.add(""); + lore.add(ChatUtil.GREEN + "Maps: " + ChatUtil.BLUE + maps.size()); + meta.setLore(lore); + this.item.setItemMeta(meta); + } + + public String getName() { + return this.name; + } + + public ItemStack getItem() { + return this.item; + } + + public Set getMaps() { + return this.maps; + } + + public void addMap(AdventureMap map) { + this.maps.add(map); + plugin.saveCategoriesAndMaps(); + plugin.loadCategoriesAndMaps(); + } + + public void save(ConfigurationSection section) { + section.set("item", this.item.getType().getKey().toString()); + section.set("maps", this.maps.stream().map(AdventureMap::getName).collect(Collectors.toList())); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/adventure/ui/AdventureMapsUI.java b/src/main/java/de/fanta/challengesjoinentities/adventure/ui/AdventureMapsUI.java index e319296..6a9a9cb 100644 --- a/src/main/java/de/fanta/challengesjoinentities/adventure/ui/AdventureMapsUI.java +++ b/src/main/java/de/fanta/challengesjoinentities/adventure/ui/AdventureMapsUI.java @@ -7,10 +7,6 @@ import de.fanta.challengesjoinentities.adventure.Category; import de.fanta.challengesjoinentities.utils.ui.AbstractWindow; import de.fanta.challengesjoinentities.utils.ui.ItemUtil; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.TextColor; @@ -18,71 +14,74 @@ import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Material; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class AdventureMapsUI extends AbstractWindow { - private final ChallengesJoinEntities plugin; - - private final Category category; - - private final Map mapsPerSlot = new HashMap<>(); - - private final int BACK_INDEX = getInventory().getSize() - 9; - - public AdventureMapsUI(ChallengesJoinEntities plugin, Category category, Player player) { - super(player, Bukkit.createInventory(player, 9 * (category.getMaps().size() / 7 + ((category.getMaps().size() % 7 > 0) ? 1 : 0)) + 18, ChatUtil.RED + category.getName() + "Map's ")); - this.category = category; - this.plugin = plugin; - } - - protected void rebuildInventory() { - List maps = this.category.getMaps().stream().sorted(Comparator.comparing(AdventureMap::getName)).toList(); - int row = 0; - int cat = 0; - for (AdventureMap category : maps) { - int slot = row * 9 + cat + 10; - getInventory().setItem(slot, category.getItem()); - this.mapsPerSlot.put(slot, category); - if (cat < 6) { - cat++; - continue; - } - row++; - cat = 0; - } - ItemStack backButtton = new ItemStack(Material.PAPER); - ItemMeta meta = backButtton.getItemMeta(); - meta.setDisplayName("" + ChatColor.GREEN + "Zurück"); - backButtton.setItemMeta(meta); - getInventory().setItem(this.BACK_INDEX, backButtton); - for (int i1 = 0; i1 < getInventory().getSize(); i1++) { - ItemStack item = getInventory().getItem(i1); - if (item == null) - getInventory().setItem(i1, ItemUtil.EMPTY_ICON); - } - } - - public void onItemClicked(InventoryClickEvent event) { - super.onItemClicked(event); - int slot = event.getSlot(); - AdventureMap map = this.mapsPerSlot.get(slot); - Player p = getPlayer(); - if (map != null) { - if (event.isRightClick()) { - getPlayer().sendMessage(ChallengesJoinEntities.PREFIX_COMPONENT.append(Component.text("MapLink: klick mich :)", TextColor.color(Color.YELLOW.asRGB())).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, map.getWebLink())))); - } else if (!Server.serverPlayers.containsValue(p.getUniqueId())) { - this.plugin.getPluginConfig().getOnlineAdventureServer().ifPresentOrElse(server -> server.loadNewAdventure(p, category.getName(), map.getName()), () -> ChatUtil.sendErrorMessage(p, "Aktuell ist kein Server Frei!")); - } else { - ChatUtil.sendErrorMessage(p, "Du kannst nur einen Server starten!"); - } - getPlayer().closeInventory(); - } - if (slot == this.BACK_INDEX) - (new CategoryUI(this.plugin, this.plugin.getCategoriesConfig(), p)).open(); - } + private final ChallengesJoinEntities plugin; + + private final Category category; + + private final Map mapsPerSlot = new HashMap<>(); + + private final int BACK_INDEX = getInventory().getSize() - 9; + + public AdventureMapsUI(ChallengesJoinEntities plugin, Category category, Player player) { + super(player, Bukkit.createInventory(player, 9 * (category.getMaps().size() / 7 + ((category.getMaps().size() % 7 > 0) ? 1 : 0)) + 18, ChatUtil.RED + category.getName() + "Map's ")); + this.category = category; + this.plugin = plugin; + } + + protected void rebuildInventory() { + List maps = this.category.getMaps().stream().sorted(Comparator.comparing(AdventureMap::getName)).toList(); + int row = 0; + int cat = 0; + for (AdventureMap category : maps) { + int slot = row * 9 + cat + 10; + getInventory().setItem(slot, category.getItem()); + this.mapsPerSlot.put(slot, category); + if (cat < 6) { + cat++; + continue; + } + row++; + cat = 0; + } + ItemStack backButtton = new ItemStack(Material.PAPER); + ItemMeta meta = backButtton.getItemMeta(); + meta.setDisplayName("" + ChatColor.GREEN + "Zurück"); + backButtton.setItemMeta(meta); + getInventory().setItem(this.BACK_INDEX, backButtton); + for (int i1 = 0; i1 < getInventory().getSize(); i1++) { + ItemStack item = getInventory().getItem(i1); + if (item == null) + getInventory().setItem(i1, ItemUtil.EMPTY_ICON); + } + } + + public void onItemClicked(InventoryClickEvent event) { + super.onItemClicked(event); + int slot = event.getSlot(); + AdventureMap map = this.mapsPerSlot.get(slot); + Player p = getPlayer(); + if (map != null) { + if (event.isRightClick()) { + getPlayer().sendMessage(ChallengesJoinEntities.PREFIX_COMPONENT.append(Component.text("MapLink: klick mich :)", TextColor.color(Color.YELLOW.asRGB())).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, map.getWebLink())))); + } else if (!Server.serverPlayers.containsValue(p.getUniqueId())) { + this.plugin.getPluginConfig().getOnlineAdventureServer().ifPresentOrElse(server -> server.loadNewAdventure(p, category.getName(), map.getName()), () -> ChatUtil.sendErrorMessage(p, "Aktuell ist kein Server Frei!")); + } else { + ChatUtil.sendErrorMessage(p, "Du kannst nur einen Server starten!"); + } + getPlayer().closeInventory(); + } + if (slot == this.BACK_INDEX) + (new CategoryUI(this.plugin, this.plugin.getCategoriesConfig(), p)).open(); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/adventure/ui/CategoryUI.java b/src/main/java/de/fanta/challengesjoinentities/adventure/ui/CategoryUI.java index 9271cd8..d3a9217 100644 --- a/src/main/java/de/fanta/challengesjoinentities/adventure/ui/CategoryUI.java +++ b/src/main/java/de/fanta/challengesjoinentities/adventure/ui/CategoryUI.java @@ -6,6 +6,10 @@ import de.fanta.challengesjoinentities.adventure.Category; import de.fanta.challengesjoinentities.utils.ui.AbstractWindow; import de.fanta.challengesjoinentities.utils.ui.ItemUtil; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; import java.util.Comparator; import java.util.HashMap; @@ -13,13 +17,6 @@ import java.util.Map; import java.util.stream.Collectors; -import net.md_5.bungee.api.ChatColor; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.ItemStack; - public class CategoryUI extends AbstractWindow { private final ChallengesJoinEntities plugin; diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddCategoryCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddCategoryCommand.java index 4d8617c..6cc8053 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddCategoryCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddCategoryCommand.java @@ -12,41 +12,37 @@ import org.bukkit.entity.Player; public class AdventureAddCategoryCommand extends SubCommand { - private final CategoriesConfig config; - - public AdventureAddCategoryCommand(ChallengesJoinEntities plugin) { - this.config = plugin.getCategoriesConfig(); - } - - public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player player; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - player = (Player)commandSender; - } else { - ChatUtil.sendErrorMessage(sender, new Object[] { "Du musst ein Spieler sein!" }); - return true; - } - if (!player.hasPermission("challenges.createcategorie")) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Keine Rechte!" }); - return true; - } - Material item = player.getInventory().getItemInMainHand().getType(); - if (item == Material.AIR) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Du musst ein Item in der Hand haben!" }); - return true; - } - if (args.hasNext()) { - String name = args.getNext(); - Category category = new Category(name, item); - if (!this.config.addCategory(category)) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Die category " + name + " existiert bereits!" }); - return true; - } - ChatUtil.sendNormalMessage(sender, new Object[] { "Die category " + name + " wurde erfolgreich hinzugefügt!" }); - this.config.save(); - return true; - } - return false; - } + private final CategoriesConfig config; + + public AdventureAddCategoryCommand(ChallengesJoinEntities plugin) { + this.config = plugin.getCategoriesConfig(); + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + if (!p.hasPermission("challenges.createcategorie")) { + ChatUtil.sendErrorMessage(sender, "Keine Rechte!"); + return true; + } + Material item = p.getInventory().getItemInMainHand().getType(); + if (item == Material.AIR) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Item in der Hand haben!"); + return true; + } + if (args.hasNext()) { + String name = args.getNext(); + Category category = new Category(name, item); + if (!this.config.addCategory(category)) { + ChatUtil.sendErrorMessage(sender, "Die category " + name + " existiert bereits!"); + return true; + } + ChatUtil.sendNormalMessage(sender, "Die category " + name + " wurde erfolgreich hinzugefügt!"); + this.config.save(); + return true; + } + return false; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddMapCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddMapCommand.java index a0471db..352af68 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddMapCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureAddMapCommand.java @@ -13,62 +13,58 @@ import org.bukkit.entity.Player; public class AdventureAddMapCommand extends SubCommand { - private ChallengesJoinEntities plugin; - - private AdventureMapsConfig config; - - public AdventureAddMapCommand(ChallengesJoinEntities plugin) { - this.plugin = plugin; - this.config = plugin.getAdventureMapsConfig(); - } - - public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player player; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - player = (Player)commandSender; - } else { - ChatUtil.sendErrorMessage(sender, new Object[] { "Du musst ein Spieler sein!" }); - return true; - } - if (!player.hasPermission("challenges.createadventuremap")) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Keine Rechte!" }); - return true; - } - Material item = player.getInventory().getItemInMainHand().getType(); - if (item == Material.AIR) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Du musst ein Item in der Hand haben!" }); - return true; - } - if (args.hasNext()) { - String name = args.getNext(); - if (args.hasNext()) { - String categoryName = args.getNext(); - Category category = this.plugin.getCategoriesConfig().getCategory(categoryName); - if (category == null) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Die Kategorie " + categoryName + " existiert nicht!" }); - return true; - } + private ChallengesJoinEntities plugin; + + private AdventureMapsConfig config; + + public AdventureAddMapCommand(ChallengesJoinEntities plugin) { + this.plugin = plugin; + this.config = plugin.getAdventureMapsConfig(); + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + if (!p.hasPermission("challenges.createadventuremap")) { + ChatUtil.sendErrorMessage(sender, "Keine Rechte!"); + return true; + } + Material item = p.getInventory().getItemInMainHand().getType(); + if (item == Material.AIR) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Item in der Hand haben!"); + return true; + } if (args.hasNext()) { - String webLink = args.getNext(); - webLink = webLink.equalsIgnoreCase("null") ? "" : webLink; - if (args.hasNext()) { - String creator = args.getAll(null); - if (creator != null) { - AdventureMap map = new AdventureMap(this.plugin, name, item, creator, webLink); - if (!this.config.addMap(map)) { - ChatUtil.sendErrorMessage(sender, new Object[] { "Die map " + name + " existiert bereits!" }); - return true; - } - category.addMap(map); - this.plugin.saveCategoriesAndMaps(); - ChatUtil.sendNormalMessage(sender, new Object[] { "Die map " + name + " wurde erfolgreich hinzugefügt!" }); - return true; - } - } - } - } - } - return false; - } + String name = args.getNext(); + if (args.hasNext()) { + String categoryName = args.getNext(); + Category category = this.plugin.getCategoriesConfig().getCategory(categoryName); + if (category == null) { + ChatUtil.sendErrorMessage(sender, "Die Kategorie " + categoryName + " existiert nicht!"); + return true; + } + if (args.hasNext()) { + String webLink = args.getNext(); + webLink = webLink.equalsIgnoreCase("null") ? "" : webLink; + if (args.hasNext()) { + String creator = args.getAll(null); + if (creator != null) { + AdventureMap map = new AdventureMap(this.plugin, name, item, creator, webLink); + if (!this.config.addMap(map)) { + ChatUtil.sendErrorMessage(sender, "Die map " + name + " existiert bereits!"); + return true; + } + category.addMap(map); + this.plugin.saveCategoriesAndMaps(); + ChatUtil.sendNormalMessage(sender, "Die map " + name + " wurde erfolgreich hinzugefügt!"); + return true; + } + } + } + } + } + return false; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureLoadCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureLoadCommand.java index c38ba60..f563c21 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureLoadCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureLoadCommand.java @@ -17,11 +17,7 @@ } public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player p; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - p = (Player) commandSender; - } else { + if (!(sender instanceof Player p)) { ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); return true; } diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureMapsCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureMapsCommand.java index 5d7eb1f..e5b5062 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureMapsCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureMapsCommand.java @@ -10,22 +10,18 @@ import org.bukkit.entity.Player; public class AdventureMapsCommand extends SubCommand { - private final ChallengesJoinEntities plugin; - - public AdventureMapsCommand(ChallengesJoinEntities plugin) { - this.plugin = plugin; - } - - public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player p; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - p = (Player)commandSender; - } else { - ChatUtil.sendErrorMessage(sender, new Object[] { "Du musst ein Spieler sein!" }); - return true; - } - (new CategoryUI(this.plugin, this.plugin.getCategoriesConfig(), p)).open(); - return true; - } + private final ChallengesJoinEntities plugin; + + public AdventureMapsCommand(ChallengesJoinEntities plugin) { + this.plugin = plugin; + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + (new CategoryUI(this.plugin, this.plugin.getCategoriesConfig(), p)).open(); + return true; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureTestMapCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureTestMapCommand.java index 28fc09a..56b8a00 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureTestMapCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureTestMapCommand.java @@ -10,40 +10,36 @@ import org.bukkit.entity.Player; public class AdventureTestMapCommand extends SubCommand { - private final ChallengesJoinEntities plugin; - - public AdventureTestMapCommand(ChallengesJoinEntities plugin) { - this.plugin = plugin; - } - - public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player p; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - p = (Player)commandSender; - } else { - ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); - return true; - } - if (sender.hasPermission("challenges.starttestmap")) { - if (args.hasNext()) { - String category = args.getNext(); - if (args.hasNext()) { - String map = args.getNext(); - if (!Server.serverPlayers.containsValue(p.getUniqueId())) { - this.plugin.getPluginConfig().getOnlineAdventureServer().ifPresentOrElse(server -> server.loadNewAdventure(p, category, map), () -> ChatUtil.sendErrorMessage(p, "Aktuell ist kein Server Frei!")); - } else { - ChatUtil.sendErrorMessage(p, "Du kannst nur einen Server starten!"); - } + private final ChallengesJoinEntities plugin; + + public AdventureTestMapCommand(ChallengesJoinEntities plugin) { + this.plugin = plugin; + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + if (sender.hasPermission("challenges.starttestmap")) { + if (args.hasNext()) { + String category = args.getNext(); + if (args.hasNext()) { + String map = args.getNext(); + if (!Server.serverPlayers.containsValue(p.getUniqueId())) { + this.plugin.getPluginConfig().getOnlineAdventureServer().ifPresentOrElse(server -> server.loadNewAdventure(p, category, map), () -> ChatUtil.sendErrorMessage(p, "Aktuell ist kein Server Frei!")); + } else { + ChatUtil.sendErrorMessage(p, "Du kannst nur einen Server starten!"); + } + } else { + ChatUtil.sendWarningMessage(p, "/ladventure testmap "); + } + } else { + ChatUtil.sendWarningMessage(p, "/ladventure testmap "); + } } else { - ChatUtil.sendWarningMessage(p, "/ladventure testmap "); - } - } else { - ChatUtil.sendWarningMessage(p, "/ladventure testmap "); - } - } else { - ChatUtil.sendErrorMessage(p, "Keine Rechte!"); - } - return true; - } + ChatUtil.sendErrorMessage(p, "Keine Rechte!"); + } + return true; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/ChallengesRemovePlayerServer.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/ChallengesRemovePlayerServer.java index 06d7e82..b3382b2 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/ChallengesRemovePlayerServer.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/ChallengesRemovePlayerServer.java @@ -1,6 +1,5 @@ package de.fanta.challengesjoinentities.commands.AdventureCommand; -import de.fanta.challengesjoinentities.ChallengesJoinEntities; import de.fanta.challengesjoinentities.ChatUtil; import de.fanta.challengesjoinentities.Server; import de.iani.cubesideutils.bukkit.commands.SubCommand; @@ -11,44 +10,38 @@ import org.bukkit.entity.Player; public class ChallengesRemovePlayerServer extends SubCommand { - private final ChallengesJoinEntities plugin; - - public ChallengesRemovePlayerServer(ChallengesJoinEntities plugin) { - this.plugin = plugin; - } - - public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player p; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - p = (Player)commandSender; - } else { - ChatUtil.sendErrorMessage(sender, new Object[] { "Du musst ein Spieler sein!" }); - return true; - } - if (!p.hasPermission("challenges.overrite")) { - ChatUtil.sendErrorMessage((CommandSender)p, new Object[] { "Keine Rechte!" }); - return true; - } - if (args.hasNext()) { - String next = args.getNext(); - if (next.equalsIgnoreCase("all")) { - Server.serverPlayers.clear(); - ChatUtil.sendNormalMessage((CommandSender)p, new Object[] { "Server liste geleert!" }); - } else { - Player serverplayer = Bukkit.getPlayer(next); - if (serverplayer != null) { - if (Server.serverPlayers.containsValue(serverplayer.getUniqueId())) { - Server.serverPlayers.remove(Server.getServerfromPlayer(serverplayer)); - ChatUtil.sendNormalMessage((CommandSender)p, new Object[] { "Spieler " + next + " wurde aus der liste entfernt!" }); - } - } else { - ChatUtil.sendErrorMessage((CommandSender)p, new Object[] { "Spieler " + next + " ist nicht Online" }); - } - } - return true; - } - ChatUtil.sendWarningMessage((CommandSender)p, new Object[] { "/challenges removeplayerserver {Playername | all}" }); - return true; - } + + public ChallengesRemovePlayerServer() { + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + if (!p.hasPermission("challenges.overrite")) { + ChatUtil.sendErrorMessage(p, "Keine Rechte!"); + return true; + } + if (args.hasNext()) { + String next = args.getNext(); + if (next.equalsIgnoreCase("all")) { + Server.serverPlayers.clear(); + ChatUtil.sendNormalMessage(p, "Server liste geleert!"); + } else { + Player serverplayer = Bukkit.getPlayer(next); + if (serverplayer != null) { + if (Server.serverPlayers.containsValue(serverplayer.getUniqueId())) { + Server.serverPlayers.remove(Server.getServerfromPlayer(serverplayer)); + ChatUtil.sendNormalMessage(p, "Spieler " + next + " wurde aus der liste entfernt!"); + } + } else { + ChatUtil.sendErrorMessage(p, "Spieler " + next + " ist nicht Online"); + } + } + return true; + } + ChatUtil.sendWarningMessage(p, "/challenges removeplayerserver {Playername | all}"); + return true; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengesloadCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengesloadCommand.java index 76fd72e..f7d7020 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengesloadCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengesloadCommand.java @@ -10,36 +10,32 @@ import org.bukkit.entity.Player; public class ChallengesloadCommand extends SubCommand { - private final ChallengesJoinEntities plugin; - - public ChallengesloadCommand(ChallengesJoinEntities plugin) { - this.plugin = plugin; - } - - public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - Player p; - CommandSender commandSender = sender; - if (commandSender instanceof Player) { - p = (Player)commandSender; - } else { - ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); - return true; - } - if (!args.hasNext()) { - if (!Server.serverPlayers.containsValue(p.getUniqueId())) { - this.plugin.getPluginConfig().getOnlineChanllengeServer().ifPresentOrElse(server -> server.loadSaved(p), () -> ChatUtil.sendErrorMessage(sender, "Aktuell ist kein Server Frei!")); - } else { - ChatUtil.sendErrorMessage(sender, "Du kannst nur einen Server starten!"); - } - return true; - } - String next = args.getNext(); - if (sender.hasPermission("challenges.load.admin")) - if (!Server.serverPlayers.containsValue(p.getUniqueId())) { - this.plugin.getPluginConfig().getOnlineChanllengeServer().ifPresentOrElse(server -> server.loadSaved(p, next.toLowerCase()), () -> ChatUtil.sendErrorMessage(sender, "Aktuell ist kein Server Frei!")); - } else { - ChatUtil.sendErrorMessage(sender, "Du kannst nur einen Server starten!"); - } - return true; - } + private final ChallengesJoinEntities plugin; + + public ChallengesloadCommand(ChallengesJoinEntities plugin) { + this.plugin = plugin; + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + if (!args.hasNext()) { + if (!Server.serverPlayers.containsValue(p.getUniqueId())) { + this.plugin.getPluginConfig().getOnlineChanllengeServer().ifPresentOrElse(server -> server.loadSaved(p), () -> ChatUtil.sendErrorMessage(sender, "Aktuell ist kein Server Frei!")); + } else { + ChatUtil.sendErrorMessage(sender, "Du kannst nur einen Server starten!"); + } + return true; + } + String next = args.getNext(); + if (sender.hasPermission("challenges.load.admin")) + if (!Server.serverPlayers.containsValue(p.getUniqueId())) { + this.plugin.getPluginConfig().getOnlineChanllengeServer().ifPresentOrElse(server -> server.loadSaved(p, next.toLowerCase()), () -> ChatUtil.sendErrorMessage(sender, "Aktuell ist kein Server Frei!")); + } else { + ChatUtil.sendErrorMessage(sender, "Du kannst nur einen Server starten!"); + } + return true; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/utils/ui/AbstractWindow.java b/src/main/java/de/fanta/challengesjoinentities/utils/ui/AbstractWindow.java index 8031ed6..003b864 100644 --- a/src/main/java/de/fanta/challengesjoinentities/utils/ui/AbstractWindow.java +++ b/src/main/java/de/fanta/challengesjoinentities/utils/ui/AbstractWindow.java @@ -7,69 +7,66 @@ import org.bukkit.inventory.Inventory; public abstract class AbstractWindow implements Window { - private Player player; - - private Inventory inventory; - - private Window parentWindow; - - public AbstractWindow(Player player, Inventory inventory) { - this.player = player; - this.inventory = inventory; - } - - public AbstractWindow(Window parentWindow, Inventory inventory) { - this(parentWindow.getPlayer(), inventory); - this.parentWindow = parentWindow; - } - - public Player getPlayer() { - return this.player; - } - - public Inventory getInventory() { - return this.inventory; - } - - public Window getParent() { - return this.parentWindow; - } - - public void onItemClicked(InventoryClickEvent event) { - if (mayAffectThisInventory(event)) - event.setCancelled(true); - } - - protected boolean mayAffectThisInventory(InventoryClickEvent event) { - if (getInventory().equals(event.getClickedInventory())) - return (event.getAction() != InventoryAction.NOTHING); - switch (event.getAction()) { - case COLLECT_TO_CURSOR: - case MOVE_TO_OTHER_INVENTORY: - case UNKNOWN: - return true; - } - return false; - } - - public void onItemDraged(InventoryDragEvent event) { - if (mayAffectThisInventory(event)) - event.setCancelled(true); - } - - protected boolean mayAffectThisInventory(InventoryDragEvent event) { - for (Integer slot : event.getRawSlots()) { - if (slot.intValue() < getInventory().getSize()) - return true; - } - return false; - } - - public void open() { - rebuildInventory(); - getPlayer().openInventory(getInventory()); - WindowManager.getInstance().registerOpenWindow(this); - } - - protected abstract void rebuildInventory(); + private final Player player; + + private final Inventory inventory; + + private Window parentWindow; + + public AbstractWindow(Player player, Inventory inventory) { + this.player = player; + this.inventory = inventory; + } + + public AbstractWindow(Window parentWindow, Inventory inventory) { + this(parentWindow.getPlayer(), inventory); + this.parentWindow = parentWindow; + } + + public Player getPlayer() { + return this.player; + } + + public Inventory getInventory() { + return this.inventory; + } + + public Window getParent() { + return this.parentWindow; + } + + public void onItemClicked(InventoryClickEvent event) { + if (mayAffectThisInventory(event)) + event.setCancelled(true); + } + + protected boolean mayAffectThisInventory(InventoryClickEvent event) { + if (getInventory().equals(event.getClickedInventory())) + return (event.getAction() != InventoryAction.NOTHING); + return switch (event.getAction()) { + case COLLECT_TO_CURSOR, MOVE_TO_OTHER_INVENTORY, UNKNOWN -> true; + default -> false; + }; + } + + public void onItemDraged(InventoryDragEvent event) { + if (mayAffectThisInventory(event)) + event.setCancelled(true); + } + + protected boolean mayAffectThisInventory(InventoryDragEvent event) { + for (Integer slot : event.getRawSlots()) { + if (slot < getInventory().getSize()) + return true; + } + return false; + } + + public void open() { + rebuildInventory(); + getPlayer().openInventory(getInventory()); + WindowManager.getInstance().registerOpenWindow(this); + } + + protected abstract void rebuildInventory(); } diff --git a/src/main/java/de/fanta/challengesjoinentities/utils/ui/ItemUtil.java b/src/main/java/de/fanta/challengesjoinentities/utils/ui/ItemUtil.java index f05d8ac..2fb8c1f 100644 --- a/src/main/java/de/fanta/challengesjoinentities/utils/ui/ItemUtil.java +++ b/src/main/java/de/fanta/challengesjoinentities/utils/ui/ItemUtil.java @@ -7,16 +7,16 @@ import org.bukkit.inventory.ItemStack; public class ItemUtil { - public static final ItemStack EMPTY_ICON = createGuiItem(Material.GRAY_STAINED_GLASS_PANE, " ", true, new String[0]); - - public static ItemStack createGuiItem(Material material, String name, String... lore) { - return createGuiItem(material, name, false, lore); - } - - public static ItemStack createGuiItem(Material material, String name, boolean glowing, String... lore) { - ItemBuilder builder = ItemBuilder.fromMaterial(material).displayName(name).lore(lore); - if (glowing) - builder.enchantment(Enchantment.DURABILITY, 1, true).flag(ItemFlag.HIDE_ENCHANTS); - return builder.build(); - } + public static final ItemStack EMPTY_ICON = createGuiItem(Material.GRAY_STAINED_GLASS_PANE, " ", true); + + public static ItemStack createGuiItem(Material material, String name, String... lore) { + return createGuiItem(material, name, false, lore); + } + + public static ItemStack createGuiItem(Material material, String name, boolean glowing, String... lore) { + ItemBuilder builder = ItemBuilder.fromMaterial(material).displayName(name).lore(lore); + if (glowing) + builder.enchantment(Enchantment.DURABILITY, 1, true).flag(ItemFlag.HIDE_ENCHANTS); + return builder.build(); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/utils/ui/Window.java b/src/main/java/de/fanta/challengesjoinentities/utils/ui/Window.java index b6287ed..f48d72c 100644 --- a/src/main/java/de/fanta/challengesjoinentities/utils/ui/Window.java +++ b/src/main/java/de/fanta/challengesjoinentities/utils/ui/Window.java @@ -7,21 +7,22 @@ import org.bukkit.inventory.Inventory; public interface Window { - Player getPlayer(); - - Inventory getInventory(); - - Window getParent(); - - void open(); - - default void closed(InventoryCloseEvent event) {} - - default void onItemClicked(InventoryClickEvent event) { - event.setCancelled(true); - } - - default void onItemDraged(InventoryDragEvent event) { - event.setCancelled(true); - } + Player getPlayer(); + + Inventory getInventory(); + + Window getParent(); + + void open(); + + default void closed(InventoryCloseEvent event) { + } + + default void onItemClicked(InventoryClickEvent event) { + event.setCancelled(true); + } + + default void onItemDraged(InventoryDragEvent event) { + event.setCancelled(true); + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/utils/ui/WindowManager.java b/src/main/java/de/fanta/challengesjoinentities/utils/ui/WindowManager.java index 8df97e2..7125c97 100644 --- a/src/main/java/de/fanta/challengesjoinentities/utils/ui/WindowManager.java +++ b/src/main/java/de/fanta/challengesjoinentities/utils/ui/WindowManager.java @@ -2,10 +2,12 @@ import de.fanta.challenges.Challenges; import de.fanta.challengesjoinentities.ChallengesJoinEntities; + import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.logging.Level; + import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -15,59 +17,59 @@ import org.bukkit.plugin.Plugin; public class WindowManager implements Listener { - private static final WindowManager INSTANCE = new WindowManager(); + private static final WindowManager INSTANCE = new WindowManager(); - private final ChallengesJoinEntities plugin = ChallengesJoinEntities.getPlugin(); - - private Map openWindows; - - public WindowManager() { - this.openWindows = new HashMap<>(); - Bukkit.getPluginManager().registerEvents(this, plugin); - } - - public void registerOpenWindow(Window window) { - Window alreadyOpen = this.openWindows.put(window.getPlayer().getUniqueId(), window); - if (alreadyOpen != null) - plugin.getLogger().log(Level.WARNING, "Window opened without the last one being closed."); - } - - @EventHandler - public void onInventoryClickEvent(InventoryClickEvent event) { - Window window = this.openWindows.get(event.getWhoClicked().getUniqueId()); - if (window == null) - return; - try { - window.onItemClicked(event); - } catch (Exception e) { - event.setCancelled(true); - throw e; - } - } - - @EventHandler - public void onInventoryDragEvent(InventoryDragEvent event) { - Window window = this.openWindows.get(event.getWhoClicked().getUniqueId()); - if (window == null) - return; - try { - window.onItemDraged(event); - } catch (Exception e) { - event.setCancelled(true); - throw e; - } - } - - @EventHandler - public void onInventoryClosedEvent(InventoryCloseEvent event) { - Window window = this.openWindows.get(event.getPlayer().getUniqueId()); - if (window == null) - return; - window.closed(event); - this.openWindows.remove(event.getPlayer().getUniqueId()); - } - - public static WindowManager getInstance() { - return INSTANCE; - } + private final ChallengesJoinEntities plugin = ChallengesJoinEntities.getPlugin(); + + private final Map openWindows; + + public WindowManager() { + this.openWindows = new HashMap<>(); + Bukkit.getPluginManager().registerEvents(this, plugin); + } + + public void registerOpenWindow(Window window) { + Window alreadyOpen = this.openWindows.put(window.getPlayer().getUniqueId(), window); + if (alreadyOpen != null) + plugin.getLogger().log(Level.WARNING, "Window opened without the last one being closed."); + } + + @EventHandler + public void onInventoryClickEvent(InventoryClickEvent event) { + Window window = this.openWindows.get(event.getWhoClicked().getUniqueId()); + if (window == null) + return; + try { + window.onItemClicked(event); + } catch (Exception e) { + event.setCancelled(true); + throw e; + } + } + + @EventHandler + public void onInventoryDragEvent(InventoryDragEvent event) { + Window window = this.openWindows.get(event.getWhoClicked().getUniqueId()); + if (window == null) + return; + try { + window.onItemDraged(event); + } catch (Exception e) { + event.setCancelled(true); + throw e; + } + } + + @EventHandler + public void onInventoryClosedEvent(InventoryCloseEvent event) { + Window window = this.openWindows.get(event.getPlayer().getUniqueId()); + if (window == null) + return; + window.closed(event); + this.openWindows.remove(event.getPlayer().getUniqueId()); + } + + public static WindowManager getInstance() { + return INSTANCE; + } }