Add hopper input

This commit is contained in:
SeanOMik 2021-06-29 00:50:30 -04:00
parent 3021a8ba65
commit 351dc772f6
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
9 changed files with 202 additions and 22 deletions

View File

@ -11,7 +11,7 @@
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel> <bytecodeTargetLevel>
<module name="EnergeticStorage" target="1.8" /> <module name="EnergeticStorage" target="1.8" />
<module name="energeticstorage" target="8" /> <module name="energeticstorage" target="1.8" />
</bytecodeTargetLevel> </bytecodeTargetLevel>
</component> </component>
</project> </project>

View File

@ -4,7 +4,7 @@
<groupId>net.seanomik</groupId> <groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId> <artifactId>energeticstorage</artifactId>
<name>EnergeticStorage</name> <name>EnergeticStorage</name>
<version>0.7-SNAPSHOT</version> <version>0.7.1-SNAPSHOT</version>
<build> <build>
<defaultGoal>clean package</defaultGoal> <defaultGoal>clean package</defaultGoal>
<resources> <resources>
@ -59,10 +59,6 @@
<id>codemc-repo</id> <id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url> <url>https://repo.codemc.org/repository/maven-public/</url>
</repository> </repository>
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
</repositories> </repositories>
<pluginRepositories> <pluginRepositories>
<pluginRepository> <pluginRepository>

View File

@ -6,7 +6,7 @@
<groupId>net.seanomik</groupId> <groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId> <artifactId>energeticstorage</artifactId>
<version>0.7-SNAPSHOT</version> <version>0.7.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>EnergeticStorage</name> <name>EnergeticStorage</name>
@ -84,10 +84,10 @@
<url>https://repo.codemc.org/repository/maven-public/</url> <url>https://repo.codemc.org/repository/maven-public/</url>
<layout>default</layout> <layout>default</layout>
</repository> </repository>
<repository> <!--<repository>
<id>codemc-snapshots</id> <id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url> <url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository> </repository>-->
</repositories> </repositories>
<dependencies> <dependencies>
@ -102,6 +102,8 @@
<artifactId>item-nbt-api</artifactId> <artifactId>item-nbt-api</artifactId>
<version>2.8.0</version> <version>2.8.0</version>
</dependency> </dependency>
<!-- This is currently compiled by myself with some fixes for anything older than Java 16.
Its compiled from SeanOMik/AnvilGUI on github -->
<dependency> <dependency>
<groupId>net.wesjd</groupId> <groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId> <artifactId>anvilgui</artifactId>

View File

@ -2,27 +2,37 @@ package net.seanomik.energeticstorage;
import net.seanomik.energeticstorage.commands.ESGiveCommand; import net.seanomik.energeticstorage.commands.ESGiveCommand;
import net.seanomik.energeticstorage.commands.ESReloadCommand; import net.seanomik.energeticstorage.commands.ESReloadCommand;
import net.seanomik.energeticstorage.files.ConfigFile;
import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.files.PlayersFile;
import net.seanomik.energeticstorage.listeners.BlockBreakListener; import net.seanomik.energeticstorage.listeners.BlockBreakListener;
import net.seanomik.energeticstorage.listeners.BlockPlaceListener; import net.seanomik.energeticstorage.listeners.BlockPlaceListener;
import net.seanomik.energeticstorage.listeners.PlayerInteractListener; 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.ItemRecipies;
import net.seanomik.energeticstorage.utils.Reference; import net.seanomik.energeticstorage.utils.Reference;
import org.bukkit.Bukkit; 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.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; 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.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 { public final class EnergeticStorage extends JavaPlugin implements Listener {
private static EnergeticStorage plugin; private static EnergeticStorage plugin;
private static HopperTask hopperTask;
@Override @Override
public void onEnable() { public void onEnable() {
@ -32,9 +42,15 @@ public final class EnergeticStorage extends JavaPlugin implements Listener {
registerListener(); registerListener();
ItemRecipies.registerRecipes(); ItemRecipies.registerRecipes();
ConfigFile.getConfig().saveDefaultConfig();
PlayersFile.getConfig().saveDefaultConfig(); PlayersFile.getConfig().saveDefaultConfig();
Reference.ES_SYSTEMS = PlayersFile.getAllSystems(); Reference.ES_SYSTEMS = PlayersFile.getAllSystems();
if (ConfigFile.isHopperInputEnabled()) {
hopperTask = new HopperTask();
hopperTask.runTaskTimerAsynchronously(this, 0L, 8L);
}
} }
private void registerCommands() { private void registerCommands() {
@ -59,10 +75,10 @@ public final class EnergeticStorage extends JavaPlugin implements Listener {
}*/ }*/
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onWorldSaveEvent(WorldSaveEvent event) {
Player player = event.getPlayer(); for (Map.Entry<UUID, List<ESSystem>> systemEntry : Reference.ES_SYSTEMS.entrySet()) {
PlayersFile.savePlayerSystems(systemEntry.getValue());
//cachePlayersSystems(player); }
} }
@Override @Override
@ -73,4 +89,12 @@ public final class EnergeticStorage extends JavaPlugin implements Listener {
public static EnergeticStorage getPlugin() { public static EnergeticStorage getPlugin() {
return plugin; return plugin;
} }
public static HopperTask getHopperTask() {
return hopperTask;
}
public static void setHopperTask(HopperTask hopperTask) {
EnergeticStorage.hopperTask = hopperTask;
}
} }

View File

@ -1,6 +1,8 @@
package net.seanomik.energeticstorage.files; package net.seanomik.energeticstorage.files;
import net.seanomik.energeticstorage.EnergeticStorage; import net.seanomik.energeticstorage.EnergeticStorage;
import net.seanomik.energeticstorage.tasks.HopperTask;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -59,6 +61,16 @@ public class ConfigFile extends YamlConfiguration {
public void reloadConfig() { public void reloadConfig() {
try { try {
super.load(this.configFile); 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) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -84,6 +96,8 @@ public class ConfigFile extends YamlConfiguration {
public static int getMaxTypes() { public static int getMaxTypes() {
return getConfig().getInt("driveMaxTypes"); return getConfig().getInt("driveMaxTypes");
} }
public static boolean isHopperInputEnabled(){return !getConfig().contains("allowHopperInput") || getConfig().getBoolean("allowHopperInput");}
} }

View File

@ -210,11 +210,18 @@ public class ESDriveGUI implements InventoryHolder, Listener {
NBTItem clickedNBT = new NBTItem(cursor); NBTItem clickedNBT = new NBTItem(cursor);
if (clickedNBT.hasKey("ES_Drive") && clickedNBT.getBoolean("ES_Drive")) { if (clickedNBT.hasKey("ES_Drive") && clickedNBT.getBoolean("ES_Drive")) {
event.setCancelled(false);
List<ESDrive> drives = esSystem.getESDrives(); List<ESDrive> drives = esSystem.getESDrives();
if (drives.contains(null)) {
drives.set(drives.indexOf(null), new ESDrive(cursor));
} else {
drives.add(new ESDrive(cursor)); drives.add(new ESDrive(cursor));
}
esSystem.setESDrives(drives); esSystem.setESDrives(drives);
initializeItems(player, esSystem);
event.setCancelled(true);
cursor.setAmount(0);
} }
} }
break; break;

View File

@ -17,7 +17,7 @@ public class PlayerInteractListener implements Listener {
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { 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) { if (event.getClickedBlock().getType() == Material.PLAYER_HEAD || event.getClickedBlock().getType() == Material.PLAYER_WALL_HEAD) {
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
Player player = event.getPlayer(); Player player = event.getPlayer();

View File

@ -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<BlockFace, Block> getRelativeHoppers(Block block) {
// relative face, block
Map<BlockFace, Block> 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<UUID, List<ESSystem>> systemEntry : Reference.ES_SYSTEMS.entrySet()) {
for (ESSystem system : systemEntry.getValue()) {
Block systemBlock = system.getLocation().getBlock();
// Get all relative hoppers touching the system.
Map<BlockFace, Block> hoppers = getRelativeHoppers(systemBlock);
for (Map.Entry<BlockFace, Block> 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<UUID, List<ESSystem>> systemEntry : Reference.ES_SYSTEMS.entrySet()) {
PlayersFile.savePlayerSystems(systemEntry.getValue());
}
Bukkit.getScheduler().cancelTask(this.getTaskId());
}
}

View File

@ -1 +1,2 @@
driveMaxTypes: 128 driveMaxTypes: 128
allowHopperInput: true