commit ac997c23d32c8b5153230f24d4b698a3547fc7a7 Author: tenkuma Date: Thu Mar 13 15:19:52 2025 -0300 Initial commit. diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..287ce19 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..e2ba937 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Freezer.jar_Decompiler.com + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..58630c0 --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..52be8a0 --- /dev/null +++ b/config.yml @@ -0,0 +1,17 @@ +message: "You should have refrigerated your food..." +# ID of the items that will rot in the unrefrigerated chest +rottableItens: + - 319 + - 320 + - 354 + - 260 + - 297 + - 357 +# ID of the blocks that will refrigerate the chest +coldMaterials: + - 79 + - 80 +# ID of the itens to turn the rotten food into +turnInto: + - 39 + - 40 \ No newline at end of file diff --git a/gd/rf/adrianvictor/freezer/ConfigUtil.class b/gd/rf/adrianvictor/freezer/ConfigUtil.class new file mode 100644 index 0000000..5b15382 Binary files /dev/null and b/gd/rf/adrianvictor/freezer/ConfigUtil.class differ diff --git a/gd/rf/adrianvictor/freezer/ConfigUtil.java b/gd/rf/adrianvictor/freezer/ConfigUtil.java new file mode 100644 index 0000000..b4c2ecb --- /dev/null +++ b/gd/rf/adrianvictor/freezer/ConfigUtil.java @@ -0,0 +1,135 @@ +package gd.rf.adrianvictor.freezer; + +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.config.Configuration; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; + +/** + * Utility class for managing plugin configuration files. + *

+ * This class extends {@link Configuration} to provide custom methods for loading, saving, and managing + * configuration files. It automatically handles the creation of parent directories and copies default configuration + * files from the plugin's resources if they do not exist. + *

+ * Note: This class allows for flexible management of multiple configuration files, specified by their file name. + */ +public class ConfigUtil extends Configuration { + + private final File configFile; + private final String pluginName; + + /** + * Constructs a new instance of {@code ConfigUtil}. + * + * @param plugin the plugin instance using this configuration utility + * @param fileName the name of the configuration file to manage (e.g., "config.yml", "settings.yml") + */ + public ConfigUtil(JavaPlugin plugin, String fileName) { + super(new File(plugin.getDataFolder(), fileName)); + this.configFile = new File(plugin.getDataFolder(), fileName); + this.pluginName = plugin.getDescription().getName(); + } + + /** + * Loads the configuration file. + *

+ */ + @Override + public void load() { + createParentDirectories(); + + if (!configFile.exists()) { + copyDefaultConfig(); + } + + try { + super.load(); + } catch (Exception e) { + Logger.severe(String.format("[%s] Failed to load config '%s': %s", pluginName, configFile.getName(), e.getMessage())); + } + } + + /** + * Creates the parent directories for the configuration file if they do not exist. + *

+ * Logs an error if the directories cannot be created. + */ + private void createParentDirectories() { + try { + Files.createDirectories(configFile.getParentFile().toPath()); + } catch (IOException e) { + Logger.severe(String.format("[%s] Failed to generate default config directory: %s", pluginName, e.getMessage())); + } + } + + /** + * Copies the default configuration file from the plugin's resources to the target location. + *

+ * This method looks for a file in the plugin's resources with the same name as the configuration file being managed. + * If found, it copies this file to the plugin's data folder. + *

+ * Logs an error if the default configuration file cannot be found or copied. + */ + private void copyDefaultConfig() { + // Adjust the path to ensure it's correct for your JAR structure + String resourcePath = "/" + configFile.getName(); + + try (InputStream input = getClass().getResourceAsStream(resourcePath)) { + if (input == null) { + Logger.severe(String.format("[%s] Default config '%s' wasn't found.", pluginName, configFile.getName())); + return; + } + + Files.copy(input, configFile.toPath()); + Logger.info(String.format("[%s] Default config '%s' generated successfully.", pluginName, configFile.getName())); + } catch (IOException e) { + Logger.severe(String.format("[%s] Failed to generate default config '%s': %s", pluginName, configFile.getName(), e.getMessage())); + } + } + + /** + * Loads the configuration file and logs the result. + *

+ * Logs a message indicating whether the configuration was loaded successfully. + */ + public void loadConfig() { + try { + this.load(); + Logger.info(String.format("[%s] Config '%s' loaded successfully.", pluginName, configFile.getName())); + } catch (Exception e) { + Logger.severe(String.format("[%s] Failed to load config '%s': %s", pluginName, configFile.getName(), e.getMessage())); + } + } + + /** + * Saves the configuration file and logs the result. + *

+ * Logs a message indicating whether the configuration was saved successfully. + */ + public void saveConfig() { + try { + this.save(); + Logger.info(String.format("[%s] Config '%s' saved successfully.", pluginName, configFile.getName())); + } catch (Exception e) { + Logger.severe(String.format("[%s] Failed to save config '%s': %s", pluginName, configFile.getName(), e.getMessage())); + } + } + + /** + * Returns the configuration file managed by this utility. + * + * @return the configuration file + */ + public File getConfig() { + return configFile; + } +} \ No newline at end of file diff --git a/gd/rf/adrianvictor/freezer/Freezer.class b/gd/rf/adrianvictor/freezer/Freezer.class new file mode 100644 index 0000000..877abac Binary files /dev/null and b/gd/rf/adrianvictor/freezer/Freezer.class differ diff --git a/gd/rf/adrianvictor/freezer/Freezer.java b/gd/rf/adrianvictor/freezer/Freezer.java new file mode 100644 index 0000000..d3c163a --- /dev/null +++ b/gd/rf/adrianvictor/freezer/Freezer.java @@ -0,0 +1,117 @@ +package gd.rf.adrianvictor.freezer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Chest; +import org.bukkit.entity.Player; +import org.bukkit.event.Event.Priority; +import org.bukkit.event.Event.Type; +import org.bukkit.event.block.BlockListener; +import org.bukkit.event.player.PlayerListener; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +public class Freezer extends JavaPlugin { + BlockListener blockListener = new FreezerBlockListener(this); + PlayerListener playerListener = new FreezerPlayerListener(this); + ConfigUtil config; + List itensToRot; + List itensToTurnInto; + List coldMaterials; + String message; + + @Override + public void onDisable() { + Logger.info(this + " is now disabled!"); + } + + @Override + public void onEnable() { + config = new ConfigUtil(this, "config.yml"); + config.loadConfig(); + message = this.getPluginConfig().getString("message", "You should have refrigerated your food..."); + + itensToRot = new ArrayList<>(); + itensToTurnInto = new ArrayList<>(); + coldMaterials = new ArrayList<>(); + List itensToRotID = this.getPluginConfig().getIntList("rottableItens", null); + List itensToTurnIntoID = this.getPluginConfig().getIntList("turnInto", null); + List coldMaterialsID = this.getPluginConfig().getIntList("coldMaterials", null); + + for (int itemID : itensToRotID) { + itensToRot.add(Material.getMaterial(itemID)); + } + + for (int itemID : itensToTurnIntoID) { + itensToTurnInto.add(Material.getMaterial(itemID)); + } + + for (int itemID : coldMaterialsID) { + coldMaterials.add(Material.getMaterial(itemID)); + } + + this.getServer().getPluginManager().registerEvent(Type.BLOCK_BREAK, this.blockListener, Priority.Highest, this); + this.getServer().getPluginManager().registerEvent(Type.PLAYER_INTERACT, this.playerListener, Priority.Highest, this); + Logger.info(this + " is now enabled!"); + } + + public void rottenFoodFromBlock(Block block, Player player) { + if (block.getType() == Material.CHEST) { + Chest chest = (Chest)block.getState(); + Inventory inventory = chest.getInventory(); + ItemStack[] stack = inventory.getContents(); + Boolean displayedWarning = false; + + for(int i = 0; i < stack.length; ++i) { + if (stack[i] != null && itensToRot.contains(stack[i].getType())) { + Random rand = new Random(); + int randNumber = rand.nextInt(6); + if (randNumber == 0) { + randNumber = rand.nextInt(itensToTurnInto.size()); + if (randNumber == 0) { + stack[i].setType(itensToTurnInto.get(randNumber)); + } + + if (!displayedWarning) { + player.sendMessage(ChatColor.GREEN + message); + Logger.info(player.getDisplayName() + " let his food rot."); + displayedWarning = true; + } + } + } + } + + } + } + + public boolean isBlockFrozen(Block block) { + Boolean hasColdBlock = false; + for (Material coldMaterial : coldMaterials) { + if (block.getRelative(BlockFace.DOWN).getType() == coldMaterial || block.getRelative(BlockFace.UP).getType() == coldMaterial || block.getRelative(BlockFace.EAST).getType() == coldMaterial|| block.getRelative(BlockFace.WEST).getType() == coldMaterial || block.getRelative(BlockFace.NORTH).getType() == coldMaterial || block.getRelative(BlockFace.SOUTH).getType() == coldMaterial) { + hasColdBlock = true; + } + } + return hasColdBlock; + } + + public Block getNeighbourChest(Block chest) { + Block[] neighbours = new Block[]{chest.getRelative(BlockFace.NORTH), chest.getRelative(BlockFace.SOUTH), chest.getRelative(BlockFace.WEST), chest.getRelative(BlockFace.EAST)}; + + for(int i = 0; i < 4; ++i) { + if (neighbours[i] != null && neighbours[i].getType() == Material.CHEST) { + return neighbours[i]; + } + } + + return null; + } + public ConfigUtil getPluginConfig() { + return config; + } +} diff --git a/gd/rf/adrianvictor/freezer/FreezerBlockListener.class b/gd/rf/adrianvictor/freezer/FreezerBlockListener.class new file mode 100644 index 0000000..fac0976 Binary files /dev/null and b/gd/rf/adrianvictor/freezer/FreezerBlockListener.class differ diff --git a/gd/rf/adrianvictor/freezer/FreezerBlockListener.java b/gd/rf/adrianvictor/freezer/FreezerBlockListener.java new file mode 100644 index 0000000..981ec94 --- /dev/null +++ b/gd/rf/adrianvictor/freezer/FreezerBlockListener.java @@ -0,0 +1,24 @@ +package gd.rf.adrianvictor.freezer; + +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockListener; + +public class FreezerBlockListener extends BlockListener { + private Freezer plugin; + + public FreezerBlockListener(Freezer plugin) { + this.plugin = plugin; + } + + public void onBlockBreak(BlockBreakEvent event) { + Block block = event.getBlock(); + Player player = event.getPlayer(); + if (block != null && block.getType() == Material.CHEST && !this.plugin.isBlockFrozen(block)) { + this.plugin.rottenFoodFromBlock(block, player); + } + + } +} diff --git a/gd/rf/adrianvictor/freezer/FreezerPlayerListener.class b/gd/rf/adrianvictor/freezer/FreezerPlayerListener.class new file mode 100644 index 0000000..a322920 Binary files /dev/null and b/gd/rf/adrianvictor/freezer/FreezerPlayerListener.class differ diff --git a/gd/rf/adrianvictor/freezer/FreezerPlayerListener.java b/gd/rf/adrianvictor/freezer/FreezerPlayerListener.java new file mode 100644 index 0000000..3986228 --- /dev/null +++ b/gd/rf/adrianvictor/freezer/FreezerPlayerListener.java @@ -0,0 +1,35 @@ +package gd.rf.adrianvictor.freezer; + +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerListener; + +public class FreezerPlayerListener extends PlayerListener { + private Freezer plugin; + + public FreezerPlayerListener(Freezer plugin) { + this.plugin = plugin; + } + + public void onPlayerInteract(PlayerInteractEvent event) { + Block block = event.getClickedBlock(); + Player player = event.getPlayer(); + if (event.getAction() == Action.RIGHT_CLICK_BLOCK && block != null && block.getType() == Material.CHEST) { + Block secondChest = this.plugin.getNeighbourChest(block); + if (!this.plugin.isBlockFrozen(block)) { + if (secondChest != null) { + if (!this.plugin.isBlockFrozen(secondChest) && !this.plugin.isBlockFrozen(block)) { + this.plugin.rottenFoodFromBlock(block, player); + this.plugin.rottenFoodFromBlock(secondChest, player); + } + } else { + this.plugin.rottenFoodFromBlock(block, player); + } + } + } + + } +} diff --git a/gd/rf/adrianvictor/freezer/Logger.class b/gd/rf/adrianvictor/freezer/Logger.class new file mode 100644 index 0000000..4ac56b3 Binary files /dev/null and b/gd/rf/adrianvictor/freezer/Logger.class differ diff --git a/gd/rf/adrianvictor/freezer/Logger.java b/gd/rf/adrianvictor/freezer/Logger.java new file mode 100644 index 0000000..c32ef7b --- /dev/null +++ b/gd/rf/adrianvictor/freezer/Logger.java @@ -0,0 +1,16 @@ +package gd.rf.adrianvictor.freezer; +import static org.bukkit.Bukkit.getServer; + +public class Logger { + public static void info(String message) { + getServer().getLogger().info(message); + } + + public static void warning(String message) { + getServer().getLogger().warning(message); + } + + public static void severe(String message) { + getServer().getLogger().severe(message); + } +} diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..d1b4dbd --- /dev/null +++ b/plugin.yml @@ -0,0 +1,9 @@ +author: tenkuma +database: false +description: You'd better refrigerate your meat... Reborn by tenkuma +generator: http://dinnerbone.com/minecraft/tools/pluginator/ +main: gd.rf.adrianvictor.freezer.Freezer +name: teFreezer +startup: postworld +url: https://adrianvictor.rf.gd +version: '1.0'