Initial commit.
This commit is contained in:
commit
ac997c23d3
15 changed files with 379 additions and 0 deletions
BIN
gd/rf/adrianvictor/freezer/ConfigUtil.class
Normal file
BIN
gd/rf/adrianvictor/freezer/ConfigUtil.class
Normal file
Binary file not shown.
135
gd/rf/adrianvictor/freezer/ConfigUtil.java
Normal file
135
gd/rf/adrianvictor/freezer/ConfigUtil.java
Normal file
|
|
@ -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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* <b>Note:</b> 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.
|
||||
* <ul>
|
||||
* <li>Creates parent directories if they do not exist.</li>
|
||||
* <li>Copies the default configuration file from the plugin's resources if the configuration file does not exist.</li>
|
||||
* <li>Attempts to load the configuration by calling the superclass' {@code load()} method.</li>
|
||||
* <li>Logs errors if the configuration file cannot be loaded.</li>
|
||||
* </ul>
|
||||
*/
|
||||
@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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
BIN
gd/rf/adrianvictor/freezer/Freezer.class
Normal file
BIN
gd/rf/adrianvictor/freezer/Freezer.class
Normal file
Binary file not shown.
117
gd/rf/adrianvictor/freezer/Freezer.java
Normal file
117
gd/rf/adrianvictor/freezer/Freezer.java
Normal file
|
|
@ -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<Material> itensToRot;
|
||||
List<Material> itensToTurnInto;
|
||||
List<Material> 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<Integer> itensToRotID = this.getPluginConfig().getIntList("rottableItens", null);
|
||||
List<Integer> itensToTurnIntoID = this.getPluginConfig().getIntList("turnInto", null);
|
||||
List<Integer> 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;
|
||||
}
|
||||
}
|
||||
BIN
gd/rf/adrianvictor/freezer/FreezerBlockListener.class
Normal file
BIN
gd/rf/adrianvictor/freezer/FreezerBlockListener.class
Normal file
Binary file not shown.
24
gd/rf/adrianvictor/freezer/FreezerBlockListener.java
Normal file
24
gd/rf/adrianvictor/freezer/FreezerBlockListener.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
BIN
gd/rf/adrianvictor/freezer/FreezerPlayerListener.class
Normal file
BIN
gd/rf/adrianvictor/freezer/FreezerPlayerListener.class
Normal file
Binary file not shown.
35
gd/rf/adrianvictor/freezer/FreezerPlayerListener.java
Normal file
35
gd/rf/adrianvictor/freezer/FreezerPlayerListener.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
BIN
gd/rf/adrianvictor/freezer/Logger.class
Normal file
BIN
gd/rf/adrianvictor/freezer/Logger.class
Normal file
Binary file not shown.
16
gd/rf/adrianvictor/freezer/Logger.java
Normal file
16
gd/rf/adrianvictor/freezer/Logger.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue