From 25e6dd337bc8582f5c4d68dbf3a51e3e9e4a509f Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 5 Sep 2019 13:29:19 +0200 Subject: [PATCH] Improved Slimefun Guide Performance --- .../mrCookieSlime/Slimefun/SlimefunGuide.java | 96 +++++++++---------- .../Slimefun/listeners/BlockListener.java | 86 +++++++++-------- .../Slimefun/utils/Utilities.java | 2 +- 3 files changed, 97 insertions(+), 87 deletions(-) diff --git a/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java b/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java index 7c17a612d..74bd3b68a 100644 --- a/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java +++ b/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java @@ -3,6 +3,7 @@ package me.mrCookieSlime.Slimefun; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.UUID; @@ -57,7 +58,8 @@ public final class SlimefunGuide { private SlimefunGuide() {} private static final int category_size = 36; - + private static final int[] slots = new int[] {0, 2, 3, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}; + @Deprecated public static ItemStack getItem() { return getItem(SlimefunGuideLayout.CHEST); @@ -86,8 +88,6 @@ public final class SlimefunGuide { return new CustomItem(new ItemStack(Material.ENCHANTED_BOOK), "&eSlimefun Guide &7(Right Click)", (book ? "": "&2"), "&rThis is your basic Guide for Slimefun", "&rYou can see all Items added by this Plugin", "&ror its Addons including their Recipes", "&ra bit of information and more"); } - private static final int[] slots = new int[] {0, 2, 3, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}; - public static void openSettings(Player p, final ItemStack guide) { final ChestMenu menu = new ChestMenu("Settings / Info"); @@ -184,16 +184,12 @@ public final class SlimefunGuide { final ChestMenu menu = new ChestMenu("Credits"); menu.setEmptySlotsClickable(false); - menu.addMenuOpeningHandler( - pl -> pl.playSound(pl.getLocation(), Sound.BLOCK_NOTE_BLOCK_HARP, 0.7F, 0.7F) - ); + menu.addMenuOpeningHandler(pl -> pl.playSound(pl.getLocation(), Sound.BLOCK_NOTE_BLOCK_HARP, 0.7F, 0.7F)); for (int i = 0; i < 9; i++) { if (i != 4) { menu.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " ")); - menu.addMenuClickHandler(i, - (pl, slot, item, action) -> false - ); + menu.addMenuClickHandler(i, (pl, slot, item, action) -> false); } else { menu.addItem(4, new CustomItem(new ItemStack(Material.EMERALD), "&7\u21E6 Back to Settings")); @@ -205,11 +201,7 @@ public final class SlimefunGuide { } int index = 9; - double total = 0; - - for (Contributor contributor : SlimefunPlugin.getUtilities().contributors) { - total += contributor.getCommits(); - } + double total = 1.0 * SlimefunPlugin.getUtilities().contributors.stream().mapToInt(Contributor::getCommits).sum(); for (final Contributor contributor: SlimefunPlugin.getUtilities().contributors) { ItemStack skull = new ItemStack(Material.PLAYER_HEAD); @@ -259,7 +251,10 @@ public final class SlimefunGuide { public static void openGuide(Player p, boolean book) { if (!SlimefunPlugin.getWhitelist().getBoolean(p.getWorld().getName() + ".enabled")) return; if (!SlimefunPlugin.getWhitelist().getBoolean(p.getWorld().getName() + ".enabled-items.SLIMEFUN_GUIDE")) return; - if (!getHistory().containsKey(p.getUniqueId())) openMainMenu(p, true, book, 1); + + if (!getHistory().containsKey(p.getUniqueId())) { + openMainMenu(p, true, book, 1); + } else { Object last = getLastEntry(p, false); if (last instanceof Category) openCategory(p, (Category) last, true, 1, book); @@ -270,8 +265,9 @@ public final class SlimefunGuide { } public static void openMainMenu(final Player p, final boolean survival, final boolean book, final int selected_page) { - if (survival) + if (survival) { clearHistory(p.getUniqueId()); + } if (book) { List pages = new ArrayList<>(); @@ -293,7 +289,7 @@ public final class SlimefunGuide { } if (locked) { - // Dont display that Category... + // Don't display that Category... } else { if (tier < category.getTier()) { @@ -400,9 +396,7 @@ public final class SlimefunGuide { final ChestMenu menu = new ChestMenu("Slimefun Guide"); menu.setEmptySlotsClickable(false); - menu.addMenuOpeningHandler( - pl -> pl.playSound(pl.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 0.7F) - ); + menu.addMenuOpeningHandler(pl -> pl.playSound(pl.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 0.7F)); List categories = SlimefunPlugin.getUtilities().enabledCategories; List handlers = SlimefunPlugin.getUtilities().guideHandlers.values().stream().flatMap(List::stream).collect(Collectors.toList()); @@ -412,16 +406,12 @@ public final class SlimefunGuide { for (int i = 0; i < 9; i++) { menu.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " ")); - menu.addMenuClickHandler(i, - (pl, slot, item, action) -> false - ); + menu.addMenuClickHandler(i, (pl, slot, item, action) -> false); } for (int i = 45; i < 54; i++) { menu.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " ")); - menu.addMenuClickHandler(i, - (pl, slot, item, action) -> false - ); + menu.addMenuClickHandler(i, (pl, slot, item, action) -> false); } int target = (category_size * (selected_page - 1)) - 1; @@ -435,12 +425,13 @@ public final class SlimefunGuide { target++; if (target >= categories.size()) { - if (!survival) break; + if (!survival) { + break; + } index = handlers.get(target - categories.size()).next(p, index, menu); } else { final Category category = categories.get(target); - boolean locked = true; for (SlimefunItem item: category.getItems()) { @@ -487,13 +478,13 @@ public final class SlimefunGuide { parents.add(ChatColor.translateAlternateColorCodes('&', "&rYou need to unlock all Items")); parents.add(ChatColor.translateAlternateColorCodes('&', "&rfrom the following Categories first:")); parents.add(""); + for (Category parent : ((LockedCategory) category).getParents()) { parents.add(parent.getItem().getItemMeta().getDisplayName()); } + menu.addItem(index, new CustomItem(Material.BARRIER, "&4LOCKED &7- &r" + category.getItem().getItemMeta().getDisplayName(), parents.toArray(new String[parents.size()]))); - menu.addMenuClickHandler(index, - (pl, slot, item, action) -> false - ); + menu.addMenuClickHandler(index, (pl, slot, item, action) -> false); index++; } } @@ -611,19 +602,23 @@ public final class SlimefunGuide { for (int i = 0; i < texts.size(); i = i + 10) { TellRawMessage page = new TellRawMessage(); page.addText(ChatColor.translateAlternateColorCodes('&', "&b&l- Slimefun Guide -\n\n")); + for (int j = i; j < texts.size() && j < i + 10; j++) { page.addText(texts.get(j) + "\n"); if (tooltips.get(j) != null) page.addHoverEvent(HoverAction.SHOW_TEXT, tooltips.get(j)); if (actions.get(j) != null) page.addClickEvent(actions.get(j)); } + page.addText("\n"); page.addText(ChatColor.translateAlternateColorCodes('&', "&6\u21E6 &lBack")); page.addHoverEvent(HoverAction.SHOW_TEXT, ChatColor.translateAlternateColorCodes('&', "&eClick to go back to the Category Overview")); page.addClickEvent(new PlayerRunnable(2) { + @Override public void run(final Player p) { Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> openMainMenu(p, survival, true, 1), 1L); } + }); pages.add(page); } @@ -680,10 +675,12 @@ public final class SlimefunGuide { int target = categoryIndex + i; if (target >= category.getItems().size()) break; final SlimefunItem sfitem = category.getItems().get(target); + if (Slimefun.isEnabled(p, sfitem, false)) { if (survival && !Slimefun.hasUnlocked(p, sfitem.getItem(), false) && sfitem.getResearch() != null) { if (Slimefun.hasPermission(p, sfitem, false)) { final Research research = sfitem.getResearch(); + menu.addItem(index, new CustomItem(Material.BARRIER, "&r" + StringUtils.formatItemName(sfitem.getItem(), false), "&4&lLOCKED", "", "&a> Click to unlock", "", "&7Cost: &b" + research.getCost() + " Level")); menu.addMenuClickHandler(index, (pl, slot, item, action) -> { if (!Research.isResearching(pl)) { @@ -716,10 +713,8 @@ public final class SlimefunGuide { index++; } else { - List list = Messages.local.getTranslation("tooltips.item-permission"); - String[] strings = list.toArray(new String[list.size()]); - CustomItem display = new CustomItem(Material.BARRIER, StringUtils.formatItemName(sfitem.getItem(), false), strings); - menu.addItem(index, display); + List tooltip = Messages.local.getTranslation("tooltips.item-permission"); + menu.addItem(index, new CustomItem(Material.BARRIER, StringUtils.formatItemName(sfitem.getItem(), false), tooltip.toArray(new String[tooltip.size()]))); menu.addMenuClickHandler(index, (pl, slot, item, action) -> false); index++; } @@ -745,25 +740,28 @@ public final class SlimefunGuide { } public static void addToHistory(Player p, Object obj) { - List list = new ArrayList<>(); - if (getHistory().containsKey(p.getUniqueId())) list = getHistory().get(p.getUniqueId()); + LinkedList list = getHistory().get(p.getUniqueId()); + + if (list == null) { + list = new LinkedList<>(); + getHistory().put(p.getUniqueId(), list); + } + list.add(obj); - getHistory().put(p.getUniqueId(), list); } private static Object getLastEntry(Player p, boolean remove) { - List list = new ArrayList<>(); - if (getHistory().containsKey(p.getUniqueId())) list = getHistory().get(p.getUniqueId()); + LinkedList history = getHistory().get(p.getUniqueId()); - if (remove && !list.isEmpty()) { - Object obj = list.get(list.size() - 1); - list.remove(obj); + if (remove && history != null && !history.isEmpty()) { + history.removeLast(); } - if (list.isEmpty()) getHistory().remove(p.getUniqueId()); - else getHistory().put(p.getUniqueId(), list); + if (history != null && history.isEmpty()) { + getHistory().remove(p.getUniqueId()); + } - return list.isEmpty() ? null: list.get(list.size() - 1); + return history == null || history.isEmpty() ? null: history.getLast(); } public static void displayItem(Player p, final ItemStack item, boolean addToHistory, final boolean book, final int page) { @@ -834,7 +832,9 @@ public final class SlimefunGuide { if (addToHistory) addToHistory(p, sfItem != null ? sfItem: item); - if (getHistory().containsKey(p.getUniqueId()) && getHistory().get(p.getUniqueId()).size() > 1) { + LinkedList history = getHistory().get(p.getUniqueId()); + + if (history != null && history.size() > 1) { menu.addItem(0, new CustomItem(new ItemStack(Material.ENCHANTED_BOOK), "&7\u21E6 Back", "", "&rLeft Click: &7Go back to previous Page", "&rShift + left Click: &7Go back to Main Menu")); menu.addMenuClickHandler(0, (pl, slot, itemstack, action) -> { if (action.isShiftClicked()) openMainMenu(p, true, book, 1); @@ -1021,7 +1021,7 @@ public final class SlimefunGuide { menu.open(p); } - private static Map> getHistory() { + private static Map> getHistory() { return SlimefunPlugin.getUtilities().guideHistory; } diff --git a/src/me/mrCookieSlime/Slimefun/listeners/BlockListener.java b/src/me/mrCookieSlime/Slimefun/listeners/BlockListener.java index 652d5e2e9..8fcf9618f 100644 --- a/src/me/mrCookieSlime/Slimefun/listeners/BlockListener.java +++ b/src/me/mrCookieSlime/Slimefun/listeners/BlockListener.java @@ -82,8 +82,11 @@ public class BlockListener implements Listener { if (mb.getTriggerBlock() == b.getType()) { Material[] blocks = mb.getBuild(); - if (mb.getTriggerBlock() == blocks[1]) { - if ( + // Please. + // Someone please find a better way to do this... + + if (( + mb.getTriggerBlock() == blocks[1] && BlockAdjacents.hasMaterialOnSide(b, blocks[0]) && BlockAdjacents.hasMaterialOnSide(b, blocks[2]) && BlockAdjacents.isMaterial(b.getRelative(BlockFace.DOWN), blocks[4]) && @@ -92,42 +95,49 @@ public class BlockListener implements Listener { BlockAdjacents.isMaterial(b.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN), blocks[7]) && BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN), blocks[6]) && BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN), blocks[8]) - ) { - if (blocks[0] != null && blocks[0] == blocks[2] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 0, 0), blocks[0])); - else if (blocks[3] != null && blocks[3] == blocks[5] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, -1, 0), blocks[5])); - else if (blocks[6] != null && blocks[6] == blocks[8] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, -2, 0), blocks[8])); - else multiblocks.add(mb); - } - } - else if (mb.getTriggerBlock() == blocks[4] && - BlockAdjacents.hasMaterialOnSide(b, blocks[3]) && - BlockAdjacents.hasMaterialOnSide(b, blocks[5]) && - BlockAdjacents.isMaterial(b.getRelative(BlockFace.DOWN), blocks[7]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.DOWN), blocks[6]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.DOWN), blocks[8]) && - BlockAdjacents.isMaterial(b.getRelative(BlockFace.UP), blocks[1]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[0]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[2]) - ) { - if (blocks[0] != null && blocks[0] == blocks[2] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 1, 0), blocks[0])); - else if (blocks[3] != null && blocks[3] == blocks[5] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 0, 0), blocks[5])); - else if (blocks[6] != null && blocks[6] == blocks[8] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, -1, 0), blocks[8])); - else multiblocks.add(mb); - } - else if (mb.getTriggerBlock() == blocks[7] && - BlockAdjacents.hasMaterialOnSide(b, blocks[6]) && - BlockAdjacents.hasMaterialOnSide(b, blocks[8]) && - BlockAdjacents.isMaterial(b.getRelative(BlockFace.UP).getRelative(BlockFace.UP), blocks[1]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP).getRelative(BlockFace.UP), blocks[0]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP).getRelative(BlockFace.UP), blocks[2]) && - BlockAdjacents.isMaterial(b.getRelative(BlockFace.UP), blocks[4]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[3]) && - BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[5]) - ) { - if (blocks[0] != null && blocks[0] == blocks[2] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 2, 0), blocks[0])); - else if (blocks[3] != null && blocks[3] == blocks[5] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 1, 0), blocks[5])); - else if (blocks[6] != null && blocks[6] == blocks[8] && !BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 0, 0), blocks[8])); - else multiblocks.add(mb); + && + blocks[0] != null && blocks[0] == blocks[2] + && BlockAdjacents.hasMaterialOnBothSides(b, blocks[0]) && + blocks[3] != null && blocks[3] == blocks[5] + && BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, -1, 0), blocks[5]) && + blocks[6] != null && blocks[6] == blocks[8] + && BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, -2, 0), blocks[8]) + ) || ( + mb.getTriggerBlock() == blocks[4] && + BlockAdjacents.hasMaterialOnSide(b, blocks[3]) && + BlockAdjacents.hasMaterialOnSide(b, blocks[5]) && + BlockAdjacents.isMaterial(b.getRelative(BlockFace.DOWN), blocks[7]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.DOWN), blocks[6]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.DOWN), blocks[8]) && + BlockAdjacents.isMaterial(b.getRelative(BlockFace.UP), blocks[1]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[0]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[2]) + && + blocks[0] != null && blocks[0] == blocks[2] + && BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 1, 0), blocks[0]) && + blocks[3] != null && blocks[3] == blocks[5] + && BlockAdjacents.hasMaterialOnBothSides(b, blocks[5]) && + blocks[6] != null && blocks[6] == blocks[8] + && BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, -1, 0), blocks[8]) + ) || ( + mb.getTriggerBlock() == blocks[7] && + BlockAdjacents.hasMaterialOnSide(b, blocks[6]) && + BlockAdjacents.hasMaterialOnSide(b, blocks[8]) && + BlockAdjacents.isMaterial(b.getRelative(BlockFace.UP).getRelative(BlockFace.UP), blocks[1]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP).getRelative(BlockFace.UP), blocks[0]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP).getRelative(BlockFace.UP), blocks[2]) && + BlockAdjacents.isMaterial(b.getRelative(BlockFace.UP), blocks[4]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[3]) && + BlockAdjacents.hasMaterialOnSide(b.getRelative(BlockFace.UP), blocks[5]) + && + blocks[0] != null && blocks[0] == blocks[2] + && BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 2, 0), blocks[0]) && + blocks[3] != null && blocks[3] == blocks[5] + && BlockAdjacents.hasMaterialOnBothSides(b.getRelative(0, 1, 0), blocks[5]) && + blocks[6] != null && blocks[6] == blocks[8] + && BlockAdjacents.hasMaterialOnBothSides(b, blocks[8]) + )) { + multiblocks.add(mb); } } } diff --git a/src/me/mrCookieSlime/Slimefun/utils/Utilities.java b/src/me/mrCookieSlime/Slimefun/utils/Utilities.java index 98ad43ece..91547f332 100644 --- a/src/me/mrCookieSlime/Slimefun/utils/Utilities.java +++ b/src/me/mrCookieSlime/Slimefun/utils/Utilities.java @@ -123,7 +123,7 @@ public final class Utilities { public final Map contributorHeads = new HashMap<>(); public final List contributors = new ArrayList<>(); - public final Map> guideHistory = new HashMap<>(); + public final Map> guideHistory = new HashMap<>(); public final List cargoTransportEvents = new ArrayList<>();