Initial commit.

This commit is contained in:
天クマ 2025-03-13 15:19:52 -03:00
commit ac997c23d3
15 changed files with 379 additions and 0 deletions

View 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;
}
}