Files
src
Code
2 135
src/main/java/com/kodari/autocompressboxpvp/AutoCompressBoxPvP.java
package com.kodari.autocompressboxpvp;
import java.util.Arrays;
import java.util.List;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public final class AutoCompressBoxPvP extends JavaPlugin implements Listener {
private static final Material COMPASS = material("COMPASS");
private static final List<Trade> TRADES = Arrays.asList(
new Trade("Auto-Compress EE.UU.", "RED_CONCRETE", "&c&lConcreto Rojo Comprimido", "&7Material de EE.UU. - BoxPvP"),
new Trade("Auto-Compress Argentina", "YELLOW_CONCRETE", "&e&lConcreto Amarillo Comprimido", "&7Material de Argentina - BoxPvP"),
new Trade("Auto-Compress Chile", "BLUE_CONCRETE", "&1&lConcreto Azul Comprimido", "&7Material de Chile - BoxPvP"),
new Trade("Auto-Compress Peru", "CALCITE", "&f&lCalcita Comprimida", "&7Material de Perú - BoxPvP"),
new Trade("Auto-Compress Colombia", "LIGHT_BLUE_GLAZED_TERRACOTTA", "&3&lTerracota Glazed Azul Comprimida", "&7Material de Colombia - BoxPvP"),
new Trade("Auto-Compress Francia", "BLUE_TERRACOTTA", "&9&lTerracota Azul Comprimida", "&7Material de Francia - BoxPvP"),
new Trade("Auto-Compress Puerto Rico", "RED_TERRACOTTA", "&4&lTerracota Roja Comprimida", "&7Material de Puerto Rico - BoxPvP"),
new Trade("Auto-Compress Bolivia", "YELLOW_TERRACOTTA", "&6&lTerracota Amarilla Comprimida", "&7Material de Bolivia - BoxPvP"),
new Trade("Auto-Compress Venezuela", "YELLOW_GLAZED_TERRACOTTA", "&e&lTerracota Glazed Amarilla Comprimida", "&7Material de Venezuela - BoxPvP"),
new Trade("Auto-Compress Espana", "RED_GLAZED_TERRACOTTA", "&c&lTerracota Glazed Roja Comprimida", "&7Material de España - BoxPvP"),
new Trade("Auto-Compress Mexico", "BROWN_GLAZED_TERRACOTTA", "&8&lTerracota Glazed Marron Comprimida", "&7Material de México - BoxPvP"),
new Trade("Auto-Compress Portugal", "GREEN_TERRACOTTA", "&2&lTerracota Verde Comprimida", "&7Material de Portugal - BoxPvP"),
new Trade("Auto-Compress Ecuador", "PACKED_ICE", "&b&lHielo Compactado Comprimido", "&7Material de Ecuador - BoxPvP"),
new Trade("Auto-Compress Brasil", "LIME_CONCRETE", "&a&lConcreto Lima Comprimido", "&7Material de Brasil - BoxPvP")
);
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
Material brokenMaterial = event.getBlock().getType();
Bukkit.getScheduler().runTaskLater(this, () -> {
if (!player.isOnline()) {
return;
}
PlayerInventory inventory = player.getInventory();
for (Trade trade : TRADES) {
if (trade.material != brokenMaterial || !hasCompass(player, trade.keyword)) {
continue;
}
ItemStack normalBlocks = new ItemStack(trade.material, 64);
if (!inventory.containsAtLeast(normalBlocks, 64)) {
continue;
}
if (inventory.removeItem(normalBlocks).isEmpty()) {
giveCompressedBlock(player, trade);
}
return;
}
}, 4L);
}
private boolean hasCompass(Player player, String keyword) {
for (ItemStack item : player.getInventory().getContents()) {
if (item == null || item.getType() != COMPASS || !item.hasItemMeta()) {
continue;
}
ItemMeta meta = item.getItemMeta();
if (meta != null && meta.hasDisplayName()) {
String plainName = ChatColor.stripColor(meta.getDisplayName());
if (plainName != null && plainName.contains(keyword)) {
return true;
}
}
}
return false;
}
private void giveCompressedBlock(Player player, Trade trade) {
ItemStack compressed = new ItemStack(trade.material);
ItemMeta meta = compressed.getItemMeta();
if (meta != null) {
meta.setDisplayName(color(trade.displayName));
meta.setLore(Arrays.asList(color(trade.lore)));
compressed.setItemMeta(meta);
}
for (ItemStack leftover : player.getInventory().addItem(compressed).values()) {
player.getWorld().dropItemNaturally(player.getLocation(), leftover);
}
}
private static String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
private static Material material(String name) {
Material material = XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElse(null);
if (material == null) {
throw new IllegalArgumentException("Material no válido: " + name);
}
return material;
}
private static final class Trade {
private final String keyword;
private final Material material;
private final String displayName;
private final String lore;
private Trade(String keyword, String material, String displayName, String lore) {
this.keyword = keyword;
this.material = AutoCompressBoxPvP.material(material);
this.displayName = displayName;
this.lore = lore;
}
}
}