From d48fddfeb2ad3fa2c1f16ea0c978e356723051d1 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 21 Apr 2020 05:55:26 -0400 Subject: [PATCH] Redid the remote system. It was bad. --- .../songoda/epicfurnaces/EpicFurnaces.java | 13 ++++- .../epicfurnaces/commands/CommandRemote.java | 47 ++++++++++--------- .../songoda/epicfurnaces/furnace/Furnace.java | 33 +++---------- .../epicfurnaces/furnace/FurnaceBuilder.java | 6 +-- .../songoda/epicfurnaces/gui/GUIOverview.java | 13 ++--- .../songoda/epicfurnaces/storage/Storage.java | 4 +- 6 files changed, 55 insertions(+), 61 deletions(-) diff --git a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java index 8bcfaed..300ac28 100644 --- a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java +++ b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java @@ -42,6 +42,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; public class EpicFurnaces extends SongodaPlugin { @@ -225,12 +226,22 @@ public class EpicFurnaces extends SongodaPlugin { String placedByStr = row.get("placedBy").asString(); UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr); + List list = row.get("accesslist").asStringList(); + if (!list.isEmpty()) { + for (String uuid : new ArrayList<>(list)) + if (uuid.contains(":")) { + list = new ArrayList<>(); + break; + } + } + List usableList = list.stream().map(UUID::fromString).collect(Collectors.toList()); + Furnace furnace = new FurnaceBuilder(location) .setLevel(levelManager.getLevel(row.get("level").asInt())) .setNickname(row.get("nickname").asString()) .setUses(row.get("uses").asInt()) .setToLevel(row.get("tolevel").asInt()) - .setAccessList(row.get("accesslist").asStringList()) + .setAccessList(usableList) .setPlacedBy(placedBy).build(); furnaceManager.addFurnace(furnace); diff --git a/src/main/java/com/songoda/epicfurnaces/commands/CommandRemote.java b/src/main/java/com/songoda/epicfurnaces/commands/CommandRemote.java index 89f47cd..25e8c94 100644 --- a/src/main/java/com/songoda/epicfurnaces/commands/CommandRemote.java +++ b/src/main/java/com/songoda/epicfurnaces/commands/CommandRemote.java @@ -4,9 +4,12 @@ import com.songoda.core.commands.AbstractCommand; import com.songoda.epicfurnaces.EpicFurnaces; import com.songoda.epicfurnaces.furnace.Furnace; import com.songoda.epicfurnaces.settings.Settings; +import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.scheduler.BukkitRunnable; import java.util.List; import java.util.UUID; @@ -22,37 +25,39 @@ public class CommandRemote extends AbstractCommand { @Override protected ReturnType runCommand(CommandSender sender, String... args) { - if (!Settings.REMOTE.getBoolean() || !sender.hasPermission("EpicFurnaces.Remote")) { plugin.getLocale().getMessage("event.general.nopermission").sendPrefixedMessage(sender); return ReturnType.FAILURE; } if (args.length < 1) return ReturnType.SYNTAX_ERROR; - StringBuilder name = new StringBuilder(); - for (int i = 0; i < args.length; i++) { - name.append(" ").append(args[i]); + String name = String.join(" ", args); + Furnace furnace = plugin.getFurnaceManager().getFurnaces().values() + .stream().filter(f -> f.getNickname() != null + && f.getNickname().equalsIgnoreCase(name)).findFirst().orElse(null); + if (furnace == null) { + plugin.getLocale().getMessage("event.remote.notfound").sendPrefixedMessage(sender); + return ReturnType.FAILURE; } - name = new StringBuilder(name.toString().trim()); - for (Furnace furnace : plugin.getFurnaceManager().getFurnaces().values()) { - if (furnace.getNickname() == null) continue; - if (!furnace.getNickname().equalsIgnoreCase(name.toString())) { - plugin.getLocale().getMessage("event.general.nopermission").sendPrefixedMessage(sender); - continue; - } - for (UUID uuid : furnace.getAccessList()) { - if (!uuid.equals(((Player) sender).getUniqueId())) { - continue; + for (UUID uuid : furnace.getAccessList()) { + if (!uuid.equals(((Player) sender).getUniqueId())) continue; + Block b = furnace.getLocation().getBlock(); + b.getChunk().load(); + org.bukkit.block.Furnace furnaceBlock = (org.bukkit.block.Furnace) b.getState(); + Inventory inventory = furnaceBlock.getInventory(); + ((Player) sender).openInventory(inventory); + new BukkitRunnable() { + public void run() { + if (inventory.getViewers().size() == 0) { + b.getChunk().unload(); + this.cancel(); + } } - Block b = furnace.getLocation().getBlock(); - org.bukkit.block.Furnace furnaceBlock = (org.bukkit.block.Furnace) b.getState(); - ((Player) sender).openInventory(furnaceBlock.getInventory()); - return ReturnType.SUCCESS; - } - + }.runTaskTimer(plugin, 5L, 5L); + return ReturnType.SUCCESS; } - plugin.getLocale().getMessage("event.remote.notfound").sendPrefixedMessage(sender); + plugin.getLocale().getMessage("event.general.nopermission").sendPrefixedMessage(sender); return ReturnType.FAILURE; } diff --git a/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java b/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java index a187a9b..0285422 100644 --- a/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java +++ b/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java @@ -37,7 +37,7 @@ public class Furnace { private int uses, tolevel, radiusOverheatLast, radiusFuelshareLast = 0; private final List radiusOverheat = new ArrayList<>(); private final List radiusFuelshare = new ArrayList<>(); - private final List accessList = new ArrayList<>(); + private final List accessList = new ArrayList<>(); private final Map cache = new HashMap<>(); public Furnace(Location location) { @@ -210,20 +210,9 @@ public class Furnace { public List getAccessList() { - List list = new ArrayList<>(); - for (String line : accessList) { - String[] halfs = line.split(":"); - list.add(UUID.fromString(halfs[0])); - } - - return list; + return Collections.unmodifiableList(accessList); } - public List getRawAccessList() { - return new ArrayList<>(accessList); - } - - public int getPerformanceTotal(Material material) { String cap = (material.name().contains("BLAST") || material.name().contains("SMOKER") ? "100" : "200"); String equation = "(" + level.getPerformance() + " / 100) * " + cap; @@ -243,24 +232,14 @@ public class Furnace { return 0; } - - public List getOriginalAccessList() { - return Collections.unmodifiableList(accessList); + public boolean addToAccessList(OfflinePlayer player) { + return addToAccessList(player.getUniqueId()); } - - public boolean addToAccessList(Player player) { - String formatted = player.getUniqueId().toString() + ":" + player.getName(); - if (accessList.contains(formatted)) return false; - return addToAccessList(formatted); + public boolean addToAccessList(UUID uuid) { + return accessList.add(uuid); } - public boolean addToAccessList(String formatted) { - if (accessList.contains(formatted)) return false; - return accessList.add(formatted); - } - - public boolean removeFromAccessList(String string) { return accessList.remove(string); } diff --git a/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java b/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java index 71ffc31..f18f270 100644 --- a/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java +++ b/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java @@ -36,9 +36,9 @@ public class FurnaceBuilder { return this; } - public FurnaceBuilder setAccessList(List accessList) { - for (String line : accessList) - this.furnace.addToAccessList(line); + public FurnaceBuilder setAccessList(List accessList) { + for (UUID uuid : accessList) + this.furnace.addToAccessList(uuid); return this; } diff --git a/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java b/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java index bec02ca..3249af0 100644 --- a/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java @@ -12,6 +12,7 @@ import com.songoda.epicfurnaces.settings.Settings; import com.songoda.epicfurnaces.utils.CostType; import com.songoda.epicfurnaces.utils.Methods; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; @@ -19,6 +20,7 @@ import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.UUID; public class GUIOverview extends Gui { @@ -238,14 +240,9 @@ public class GUIOverview extends Gui { lorehook.add(""); lorehook.add(plugin.getLocale().getMessage("interface.furnace.remotelist").getMessage()); - for (String line : furnace.getRawAccessList()) { - String[] halfs = line.split(":"); - String name = halfs[1]; - Player remotePlayer = Bukkit.getPlayer(halfs[0]); - if (remotePlayer != null) { - name = remotePlayer.getDisplayName(); - } - lorehook.add(Methods.formatText("&6" + name)); + for (UUID uuid : furnace.getAccessList()) { + OfflinePlayer remotePlayer = Bukkit.getOfflinePlayer(uuid); + lorehook.add(Methods.formatText("&6" + remotePlayer.getName())); } return lorehook; } diff --git a/src/main/java/com/songoda/epicfurnaces/storage/Storage.java b/src/main/java/com/songoda/epicfurnaces/storage/Storage.java index b340498..44a5cf3 100644 --- a/src/main/java/com/songoda/epicfurnaces/storage/Storage.java +++ b/src/main/java/com/songoda/epicfurnaces/storage/Storage.java @@ -7,6 +7,8 @@ import com.songoda.epicfurnaces.furnace.Furnace; import com.songoda.epicfurnaces.utils.Methods; import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; public abstract class Storage { @@ -42,7 +44,7 @@ public abstract class Storage { new StorageItem("uses", furnace.getUses()), new StorageItem("tolevel", furnace.getTolevel()), new StorageItem("nickname", furnace.getNickname()), - new StorageItem("accesslist", furnace.getOriginalAccessList()), + new StorageItem("accesslist", furnace.getAccessList().stream().map(UUID::toString).collect(Collectors.toList())), new StorageItem("placedby", furnace.getPlacedBy() == null ? null : furnace.getPlacedBy().toString())); }