diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 32af5e9..9c41e5e 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -11,7 +11,7 @@ - + \ No newline at end of file diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 3ab5378..57b71b1 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ net.seanomik energeticstorage EnergeticStorage - 0.7-SNAPSHOT + 0.7.1-SNAPSHOT clean package @@ -59,10 +59,6 @@ codemc-repo https://repo.codemc.org/repository/maven-public/ - - codemc-snapshots - https://repo.codemc.io/repository/maven-snapshots/ - diff --git a/pom.xml b/pom.xml index b4923e0..83808b2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.seanomik energeticstorage - 0.7-SNAPSHOT + 0.7.1-SNAPSHOT jar EnergeticStorage @@ -84,10 +84,10 @@ https://repo.codemc.org/repository/maven-public/ default - + @@ -102,6 +102,8 @@ item-nbt-api 2.8.0 + net.wesjd anvilgui diff --git a/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java b/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java index 73ab769..343389c 100644 --- a/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java +++ b/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java @@ -2,27 +2,37 @@ package net.seanomik.energeticstorage; import net.seanomik.energeticstorage.commands.ESGiveCommand; import net.seanomik.energeticstorage.commands.ESReloadCommand; +import net.seanomik.energeticstorage.files.ConfigFile; import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.listeners.BlockBreakListener; import net.seanomik.energeticstorage.listeners.BlockPlaceListener; import net.seanomik.energeticstorage.listeners.PlayerInteractListener; +import net.seanomik.energeticstorage.objects.ESSystem; +import net.seanomik.energeticstorage.tasks.HopperTask; import net.seanomik.energeticstorage.utils.ItemRecipies; import net.seanomik.energeticstorage.utils.Reference; import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.type.Chest; +import org.bukkit.block.data.type.Hopper; +import org.bukkit.craftbukkit.libs.jline.internal.Nullable; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.world.WorldSaveEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitTask; + +import java.util.*; -// @TODO: Add more config options -/* - * Change Log: - * - Fix /esgive command runner from getting kicked with "Illegal Characters" error. - * - Fix systems that were placed on the walls. - */ public final class EnergeticStorage extends JavaPlugin implements Listener { private static EnergeticStorage plugin; + private static HopperTask hopperTask; @Override public void onEnable() { @@ -32,9 +42,15 @@ public final class EnergeticStorage extends JavaPlugin implements Listener { registerListener(); ItemRecipies.registerRecipes(); + ConfigFile.getConfig().saveDefaultConfig(); PlayersFile.getConfig().saveDefaultConfig(); Reference.ES_SYSTEMS = PlayersFile.getAllSystems(); + + if (ConfigFile.isHopperInputEnabled()) { + hopperTask = new HopperTask(); + hopperTask.runTaskTimerAsynchronously(this, 0L, 8L); + } } private void registerCommands() { @@ -59,10 +75,10 @@ public final class EnergeticStorage extends JavaPlugin implements Listener { }*/ @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - Player player = event.getPlayer(); - - //cachePlayersSystems(player); + public void onWorldSaveEvent(WorldSaveEvent event) { + for (Map.Entry> systemEntry : Reference.ES_SYSTEMS.entrySet()) { + PlayersFile.savePlayerSystems(systemEntry.getValue()); + } } @Override @@ -73,4 +89,12 @@ public final class EnergeticStorage extends JavaPlugin implements Listener { public static EnergeticStorage getPlugin() { return plugin; } + + public static HopperTask getHopperTask() { + return hopperTask; + } + + public static void setHopperTask(HopperTask hopperTask) { + EnergeticStorage.hopperTask = hopperTask; + } } diff --git a/src/main/java/net/seanomik/energeticstorage/files/ConfigFile.java b/src/main/java/net/seanomik/energeticstorage/files/ConfigFile.java index 9e5297f..006216c 100644 --- a/src/main/java/net/seanomik/energeticstorage/files/ConfigFile.java +++ b/src/main/java/net/seanomik/energeticstorage/files/ConfigFile.java @@ -1,6 +1,8 @@ package net.seanomik.energeticstorage.files; import net.seanomik.energeticstorage.EnergeticStorage; +import net.seanomik.energeticstorage.tasks.HopperTask; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.configuration.file.YamlConfiguration; @@ -59,6 +61,16 @@ public class ConfigFile extends YamlConfiguration { public void reloadConfig() { try { super.load(this.configFile); + + // Check if hopper input is not enabled and if the hopperTask is running. If it is, disable it. + // else, we enable the task. + if (!isHopperInputEnabled() && !EnergeticStorage.getHopperTask().isCancelled()) { + EnergeticStorage.getHopperTask().cancel(); + } else if (isHopperInputEnabled() && EnergeticStorage.getHopperTask().isCancelled()) { + EnergeticStorage.setHopperTask(new HopperTask()); + EnergeticStorage.getHopperTask().runTaskTimerAsynchronously(EnergeticStorage.getPlugin(), 0L, 8L); + //EnergeticStorage.setHopperTask(new HopperTask().runTaskTimerAsynchronously(EnergeticStorage.getPlugin(), 0L, 8L)); + } } catch (Exception e) { e.printStackTrace(); @@ -84,6 +96,8 @@ public class ConfigFile extends YamlConfiguration { public static int getMaxTypes() { return getConfig().getInt("driveMaxTypes"); } + + public static boolean isHopperInputEnabled(){return !getConfig().contains("allowHopperInput") || getConfig().getBoolean("allowHopperInput");} } \ No newline at end of file diff --git a/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java b/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java index 55c0c7f..4fe39de 100644 --- a/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java +++ b/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java @@ -210,11 +210,18 @@ public class ESDriveGUI implements InventoryHolder, Listener { NBTItem clickedNBT = new NBTItem(cursor); if (clickedNBT.hasKey("ES_Drive") && clickedNBT.getBoolean("ES_Drive")) { - event.setCancelled(false); List drives = esSystem.getESDrives(); - drives.add(new ESDrive(cursor)); + if (drives.contains(null)) { + drives.set(drives.indexOf(null), new ESDrive(cursor)); + } else { + drives.add(new ESDrive(cursor)); + } esSystem.setESDrives(drives); + initializeItems(player, esSystem); + + event.setCancelled(true); + cursor.setAmount(0); } } break; diff --git a/src/main/java/net/seanomik/energeticstorage/listeners/PlayerInteractListener.java b/src/main/java/net/seanomik/energeticstorage/listeners/PlayerInteractListener.java index 1e97b8e..c3ccf2d 100644 --- a/src/main/java/net/seanomik/energeticstorage/listeners/PlayerInteractListener.java +++ b/src/main/java/net/seanomik/energeticstorage/listeners/PlayerInteractListener.java @@ -17,7 +17,7 @@ public class PlayerInteractListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { - if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getHand() == EquipmentSlot.HAND) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getHand() == EquipmentSlot.HAND && !event.getPlayer().isSneaking()) { if (event.getClickedBlock().getType() == Material.PLAYER_HEAD || event.getClickedBlock().getType() == Material.PLAYER_WALL_HEAD) { Block block = event.getClickedBlock(); Player player = event.getPlayer(); diff --git a/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java b/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java new file mode 100644 index 0000000..9e778ab --- /dev/null +++ b/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java @@ -0,0 +1,136 @@ +package net.seanomik.energeticstorage.tasks; + +import net.seanomik.energeticstorage.EnergeticStorage; +import net.seanomik.energeticstorage.files.PlayersFile; +import net.seanomik.energeticstorage.objects.ESSystem; +import net.seanomik.energeticstorage.utils.Reference; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.craftbukkit.libs.jline.internal.Nullable; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.*; + +public class HopperTask extends BukkitRunnable { + private boolean isHopperInputting(org.bukkit.material.Hopper hopper, BlockFace hopperRelative) { + BlockFace hopperFacing = hopper.getFacing(); + + switch (hopperRelative) { + case UP: + return hopperFacing == BlockFace.DOWN; + case DOWN: + return hopperFacing == BlockFace.UP; + case EAST: + return hopperFacing == BlockFace.WEST; + case WEST: + return hopperFacing == BlockFace.EAST; + case SOUTH: + return hopperFacing == BlockFace.NORTH; + case NORTH: + return hopperFacing == BlockFace.SOUTH; + } + + return false; + } + + private Map getRelativeHoppers(Block block) { + // relative face, block + Map hoppers = new HashMap<>(); + + Block upBlock = block.getRelative(BlockFace.UP); + Block downBlock = block.getRelative(BlockFace.DOWN); + Block eastBlock = block.getRelative(BlockFace.EAST); + Block westBlock = block.getRelative(BlockFace.WEST); + Block northBlock = block.getRelative(BlockFace.NORTH); + Block southBlock = block.getRelative(BlockFace.SOUTH); + + if (upBlock.getType() == Material.HOPPER) { + hoppers.put(BlockFace.UP, upBlock); + } + + if (downBlock.getType() == Material.HOPPER) { + hoppers.put(BlockFace.DOWN, downBlock); + } + + if (eastBlock.getType() == Material.HOPPER) { + hoppers.put(BlockFace.EAST, eastBlock); + } + + if (westBlock.getType() == Material.HOPPER) { + hoppers.put(BlockFace.WEST, westBlock); + } + + if (northBlock.getType() == Material.HOPPER) { + hoppers.put(BlockFace.NORTH, northBlock); + } + + if (southBlock.getType() == Material.HOPPER) { + hoppers.put(BlockFace.SOUTH, southBlock); + } + + return hoppers; + } + + @Nullable + private ItemStack getFirstItemStack(Inventory inventory) { + ItemStack[] items = inventory.getContents(); + for (ItemStack item : items) { + if (item != null && item.getType() != Material.AIR) { + return item; + } + } + + return null; + } + + @Override + public void run() { + for (Map.Entry> systemEntry : Reference.ES_SYSTEMS.entrySet()) { + for (ESSystem system : systemEntry.getValue()) { + Block systemBlock = system.getLocation().getBlock(); + + // Get all relative hoppers touching the system. + Map hoppers = getRelativeHoppers(systemBlock); + for (Map.Entry entry : hoppers.entrySet()) { + Bukkit.getScheduler().runTask(EnergeticStorage.getPlugin(), () -> { + + try { + org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) entry.getValue().getState(); + org.bukkit.material.Hopper hopperType = (org.bukkit.material.Hopper) hopper.getData(); + // Check if the hopper is facing towards the system + if (isHopperInputting(hopperType, entry.getKey())) { + + // Find the first non-null item in the hopper inventory and add it + ItemStack firstItem = getFirstItemStack(hopper.getInventory()); + if (firstItem != null) { + ItemStack clonedItem = firstItem.clone(); + clonedItem.setAmount(1); + + firstItem.setAmount(firstItem.getAmount() - 1); + + system.addItem(clonedItem); + } + } + } catch (ClassCastException exception) { + // Ignore exception. These exceptions are only thrown in rare occasions + // that the hopper is destroyed during during this task. + } + }); + } + } + } + } + + @Override + public synchronized void cancel() throws IllegalStateException { + for (Map.Entry> systemEntry : Reference.ES_SYSTEMS.entrySet()) { + PlayersFile.savePlayerSystems(systemEntry.getValue()); + } + + Bukkit.getScheduler().cancelTask(this.getTaskId()); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 191249a..295bef8 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1 +1,2 @@ driveMaxTypes: 128 +allowHopperInput: true \ No newline at end of file