From 478932737800b67d93b8665635430a5707b137fe Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 17 Aug 2020 15:17:40 +0200 Subject: [PATCH] Encapsulate fields --- .../bukkit/util/BukkitInventoryUtil.java | 22 +++++++++---------- .../com/plotsquared/core/command/Music.java | 12 +++++----- .../com/plotsquared/core/command/Rate.java | 10 ++++----- .../components/ComponentPresetManager.java | 18 +++++++-------- .../plotsquared/core/plot/PlotInventory.java | 17 +++++++++----- .../plotsquared/core/plot/PlotItemStack.java | 20 +++++++++++++---- 6 files changed, 59 insertions(+), 40 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitInventoryUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitInventoryUtil.java index 1ae647804..a849f16b5 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitInventoryUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitInventoryUtil.java @@ -49,11 +49,11 @@ import java.util.stream.IntStream; @Singleton public class BukkitInventoryUtil extends InventoryUtil { @Override public void open(PlotInventory inv) { - BukkitPlayer bp = (BukkitPlayer) inv.player; - Inventory inventory = Bukkit.createInventory(null, inv.size * 9, + BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); + Inventory inventory = Bukkit.createInventory(null, inv.getSize() * 9, ChatColor.translateAlternateColorCodes('&', inv.getTitle())); PlotItemStack[] items = inv.getItems(); - for (int i = 0; i < inv.size * 9; i++) { + for (int i = 0; i < inv.getSize() * 9; i++) { PlotItemStack item = items[i]; if (item != null) { inventory.setItem(i, getItem(item)); @@ -66,12 +66,12 @@ import java.util.stream.IntStream; if (!inv.isOpen()) { return; } - BukkitPlayer bp = (BukkitPlayer) inv.player; + BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); bp.player.closeInventory(); } @Override public void setItem(PlotInventory inv, int index, PlotItemStack item) { - BukkitPlayer bp = (BukkitPlayer) inv.player; + BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); InventoryView opened = bp.player.getOpenInventory(); if (!inv.isOpen()) { return; @@ -84,18 +84,18 @@ import java.util.stream.IntStream; if (item == null) { return null; } - ItemStack stack = new ItemStack(BukkitAdapter.adapt(item.getType()), item.amount); + ItemStack stack = new ItemStack(BukkitAdapter.adapt(item.getType()), item.getAmount()); ItemMeta meta = null; - if (item.name != null) { + if (item.getName() != null) { meta = stack.getItemMeta(); - meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', item.name)); + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', item.getName())); } - if (item.lore != null) { + if (item.getLore() != null) { if (meta == null) { meta = stack.getItemMeta(); } List lore = new ArrayList<>(); - for (String entry : item.lore) { + for (String entry : item.getLore()) { lore.add(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(BukkitUtil.MINI_MESSAGE.deserialize(entry))); } meta.setLore(lore); @@ -141,7 +141,7 @@ import java.util.stream.IntStream; if (!plotInventory.isOpen()) { return false; } - BukkitPlayer bp = (BukkitPlayer) plotInventory.player; + BukkitPlayer bp = (BukkitPlayer) plotInventory.getPlayer(); InventoryView opened = bp.player.getOpenInventory(); if (plotInventory.isOpen()) { if (opened.getType() == InventoryType.CRAFTING) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Music.java b/Core/src/main/java/com/plotsquared/core/command/Music.java index 5b2c27b0d..bd73c2282 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Music.java +++ b/Core/src/main/java/com/plotsquared/core/command/Music.java @@ -87,27 +87,27 @@ public class Music extends SubCommand { .createFlagInstance(item.getType()); PlotFlagRemoveEvent event = new PlotFlagRemoveEvent(plotFlag, plot); if (event.getEventResult() == Result.DENY) { - player.sendMessage( + getPlayer().sendMessage( TranslatableCaption.of("events.event_denied"), Template.of("value", "Music removal")); return true; } plot.removeFlag(event.getFlag()); - player.sendMessage(TranslatableCaption.of("flag.flag_removed")); - } else if (item.name.toLowerCase(Locale.ENGLISH).contains("disc")) { + getPlayer().sendMessage(TranslatableCaption.of("flag.flag_removed")); + } else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) { PlotFlag plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) .createFlagInstance(item.getType()); PlotFlagAddEvent event = new PlotFlagAddEvent(plotFlag, plot); if (event.getEventResult() == Result.DENY) { - player.sendMessage( + getPlayer().sendMessage( TranslatableCaption.of("events.event_denied"), Template.of("value", "Music addition")); return true; } plot.setFlag(event.getFlag()); - player.sendMessage(TranslatableCaption.of("flag.flag_added")); + getPlayer().sendMessage(TranslatableCaption.of("flag.flag_added")); } else { - player.sendMessage(TranslatableCaption.of("flag.flag_not_added")); + getPlayer().sendMessage(TranslatableCaption.of("flag.flag_not_added")); } return false; } diff --git a/Core/src/main/java/com/plotsquared/core/command/Rate.java b/Core/src/main/java/com/plotsquared/core/command/Rate.java index 5fab272e8..3172a0078 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Rate.java +++ b/Core/src/main/java/com/plotsquared/core/command/Rate.java @@ -160,19 +160,19 @@ public class Rate extends SubCommand { if (index.getValue() >= Settings.Ratings.CATEGORIES.size()) { int rV = rating.getValue(); PlotRateEvent event = Rate.this.eventDispatcher - .callRating(this.player, plot, new Rating(rV)); + .callRating(this.getPlayer(), plot, new Rating(rV)); if (event.getRating() != null) { - plot.addRating(this.player.getUUID(), event.getRating()); - player.sendMessage( + plot.addRating(this.getPlayer().getUUID(), event.getRating()); + getPlayer().sendMessage( TranslatableCaption.of("ratings.rating_applied"), Template.of("plot", plot.getId().toString()) ); if (Permissions - .hasPermission(this.player, Permission.PERMISSION_COMMENT)) { + .hasPermission(this.getPlayer(), Permission.PERMISSION_COMMENT)) { Command command = MainCommand.getInstance().getCommand(Comment.class); if (command != null) { - player.sendMessage( + getPlayer().sendMessage( TranslatableCaption.of("tutorial.comment_this"), Template.of("plot", "/plot rate") ); diff --git a/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java b/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java index 550142225..d6ee38901 100644 --- a/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java +++ b/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java @@ -155,7 +155,7 @@ public class ComponentPresetManager { final int size = (int) Math.ceil((double) allowedPresets.size() / 9.0D); final PlotInventory plotInventory = new PlotInventory(this.inventoryUtil, player, size, this.guiName) { @Override public boolean onClick(final int index) { - if (!player.getCurrentPlot().equals(plot)) { + if (!getPlayer().getCurrentPlot().equals(plot)) { return false; } @@ -169,28 +169,28 @@ public class ComponentPresetManager { } if (plot.getRunning() > 0) { - player.sendMessage(TranslatableCaption.of("errors.wait_for_timer")); + getPlayer().sendMessage(TranslatableCaption.of("errors.wait_for_timer")); return false; } final Pattern pattern = PatternUtil.parse(null, componentPreset.getPattern(), false); if (pattern == null) { - player.sendMessage(TranslatableCaption.of("preset.preset_invalid")); + getPlayer().sendMessage(TranslatableCaption.of("preset.preset_invalid")); return false; } if (componentPreset.getCost() > 0.0D && econHandler != null && plot.getArea().useEconomy()) { - if (econHandler.getMoney(player) < componentPreset.getCost()) { - player.sendMessage(TranslatableCaption.of("preset.preset_cannot_afford")); + if (econHandler.getMoney(getPlayer()) < componentPreset.getCost()) { + getPlayer().sendMessage(TranslatableCaption.of("preset.preset_cannot_afford")); return false; } else { - econHandler.withdrawMoney(player, componentPreset.getCost()); - player.sendMessage(TranslatableCaption.of("economy.removed_balance"), + econHandler.withdrawMoney(getPlayer(), componentPreset.getCost()); + getPlayer().sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", componentPreset.getCost() + "")); } } - BackupManager.backup(player, plot, () -> { + BackupManager.backup(getPlayer(), plot, () -> { plot.addRunning(); QueueCoordinator queue = plot.getArea().getQueue(); for (Plot current : plot.getConnectedPlots()) { @@ -198,7 +198,7 @@ public class ComponentPresetManager { } queue.setCompleteTask(plot::removeRunning); queue.enqueue(); - player.sendMessage(TranslatableCaption.of("working.generating_component")); + getPlayer().sendMessage(TranslatableCaption.of("working.generating_component")); }); return false; } diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotInventory.java b/Core/src/main/java/com/plotsquared/core/plot/PlotInventory.java index 9e23dc726..2b7132c7b 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotInventory.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotInventory.java @@ -38,8 +38,8 @@ public class PlotInventory { private static final Logger logger = LoggerFactory.getLogger("P2/" + PlotInventory.class.getSimpleName()); - public final PlotPlayer player; - public final int size; + private final PlotPlayer player; + private final int size; private final PlotItemStack[] items; private String title; private boolean open = false; @@ -88,9 +88,9 @@ public class PlotInventory { if (this.title == null) { return; } - if (!hasPlotInventoryOpen(player)) { + if (!hasPlotInventoryOpen(getPlayer())) { this.open = true; - setPlotInventoryOpen(player, this); + setPlotInventoryOpen(getPlayer(), this); this.inventoryUtil.open(this); } } @@ -99,7 +99,7 @@ public class PlotInventory { if (this.title == null) { return; } - removePlotInventoryOpen(player); + removePlotInventoryOpen(getPlayer()); this.inventoryUtil.close(this); this.open = false; } @@ -140,4 +140,11 @@ public class PlotInventory { return this.open; } + public PlotPlayer getPlayer() { + return player; + } + + public int getSize() { + return size; + } } diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotItemStack.java b/Core/src/main/java/com/plotsquared/core/plot/PlotItemStack.java index fbf17d8f0..1b42fa86f 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotItemStack.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotItemStack.java @@ -32,9 +32,9 @@ import com.sk89q.worldedit.world.registry.LegacyMapper; public class PlotItemStack { - public final int amount; - public final String name; - public final String[] lore; + private final int amount; + private final String name; + private final String[] lore; private final ItemType type; /** @@ -69,10 +69,22 @@ public class PlotItemStack { } public BlockState getBlockState() { - return type.getBlockType().getDefaultState(); + return getType().getBlockType().getDefaultState(); } public ItemType getType() { return this.type; } + + public int getAmount() { + return amount; + } + + public String getName() { + return name; + } + + public String[] getLore() { + return lore; + } }