Skip to content

Commit

Permalink
Reduce code duplication (#4230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Intybyte authored Sep 6, 2024
1 parent 7d4ed2b commit 9a64617
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -55,8 +54,6 @@

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
Expand Down Expand Up @@ -847,18 +844,7 @@ private void constructMenu(@Nonnull BlockMenuPreset preset) {
preset.drawBackground(ChestMenuUtils.getOutputSlotTexture(), OUTPUT_BORDER);

for (int i : getOutputSlots()) {
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
});
preset.addMenuClickHandler(i, ChestMenuUtils.getDefaultOutputHandler());
}

preset.addItem(34, getFuelSource().getItem(), ChestMenuUtils.getEmptyClickHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.items.CustomItemStack;
Expand All @@ -25,8 +24,6 @@
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;

import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu;
Expand Down Expand Up @@ -126,18 +123,7 @@ protected void constructMenu(BlockMenuPreset preset) {
preset.addItem(22, new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler());

for (int i : getOutputSlots()) {
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
});
preset.addMenuClickHandler(i, ChestMenuUtils.getDefaultOutputHandler());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Levelled;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.blocks.Vein;
Expand All @@ -32,8 +30,6 @@
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand Down Expand Up @@ -100,18 +96,7 @@ private void constructMenu(@Nonnull BlockMenuPreset preset) {
}

for (int i : getOutputSlots()) {
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
});
preset.addMenuClickHandler(i, ChestMenuUtils.getDefaultOutputHandler());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.items.CustomItemStack;
Expand All @@ -38,8 +36,6 @@
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand Down Expand Up @@ -265,18 +261,7 @@ protected void constructMenu(@Nonnull BlockMenuPreset preset) {
preset.addItem(4, new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler());

for (int i : OUTPUT_SLOTS) {
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
});
preset.addMenuClickHandler(i, ChestMenuUtils.getDefaultOutputHandler());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import javax.annotation.Nonnull;

import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -43,6 +45,19 @@ private ChestMenuUtils() {}
private static final ItemStack PREV_BUTTON_INACTIVE = new SlimefunItemStack("_UI_PREVIOUS_INACTIVE", Material.BLACK_STAINED_GLASS_PANE, "&8\u21E6 Previous Page");
private static final ItemStack NEXT_BUTTON_INACTIVE = new SlimefunItemStack("_UI_NEXT_INACTIVE", Material.BLACK_STAINED_GLASS_PANE, "&8Next Page \u21E8");

private static final ChestMenu.AdvancedMenuClickHandler OUTPUT_HANDLER = new ChestMenu.AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
};

private static final MenuClickHandler CLICK_HANDLER = (p, s, i, a) -> false;

public static @Nonnull ItemStack getBackground() {
Expand All @@ -69,6 +84,10 @@ private ChestMenuUtils() {}
return CLICK_HANDLER;
}

public static @Nonnull ChestMenu.AdvancedMenuClickHandler getDefaultOutputHandler() {
return OUTPUT_HANDLER;
}

public static @Nonnull ItemStack getBackButton(@Nonnull Player p, String... lore) {
return new CustomItemStack(BACK_BUTTON, "&7\u21E6 " + Slimefun.getLocalization().getMessage(p, "guide.back.title"), lore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

Expand All @@ -37,8 +35,6 @@
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand Down Expand Up @@ -115,18 +111,7 @@ protected void constructMenu(BlockMenuPreset preset) {
preset.addItem(22, new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler());

for (int i : getOutputSlots()) {
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
});
preset.addMenuClickHandler(i, ChestMenuUtils.getDefaultOutputHandler());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.items.CustomItemStack;
Expand All @@ -35,8 +34,6 @@
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
Expand Down Expand Up @@ -122,18 +119,7 @@ private void constructMenu(BlockMenuPreset preset) {
}

for (int i : getOutputSlots()) {
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {

@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
return false;
}

@Override
public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) {
return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR;
}
});
preset.addMenuClickHandler(i, ChestMenuUtils.getDefaultOutputHandler());
}

preset.addItem(22, new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler());
Expand Down

0 comments on commit 9a64617

Please sign in to comment.