src/main/java/vpd/bowandaero12/furnacelv/items/ItemHandler.java gelöscht
This commit is contained in:
@@ -1,206 +0,0 @@
|
||||
package vpd.bowandaero12.furnacelv.items;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import vpd.bowandaero12.furnacelv.FurnaceLevels;
|
||||
|
||||
public class ItemHandler implements Listener {
|
||||
private FurnaceLevels plugin;
|
||||
|
||||
private Map<Location, Integer> furnaceTrack;
|
||||
|
||||
public ItemHandler(FurnaceLevels plugin) {
|
||||
this.furnaceTrack = new HashMap<>();
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public Map<Location, Integer> getFurnaceTrack() {
|
||||
return this.furnaceTrack;
|
||||
}
|
||||
|
||||
public void registerFurnaces() {
|
||||
for (String s : this.plugin.configs.getFData().getStringList("furnaces")) {
|
||||
String[] coordsSplit = s.split(",");
|
||||
Location loc = new Location(Bukkit.getServer().getWorld(coordsSplit[0]), Double.parseDouble(coordsSplit[1]), Double.parseDouble(coordsSplit[2]), Double.parseDouble(coordsSplit[3]));
|
||||
int lv = Integer.parseInt(coordsSplit[4]);
|
||||
this.furnaceTrack.put(loc, Integer.valueOf(lv));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockPlace(BlockPlaceEvent e) {
|
||||
if (!this.plugin.configs.getConfig().getBoolean("use-economy") &&
|
||||
e.getItemInHand().isSimilar(customItem())) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (e.getBlock().getType() != Material.FURNACE)
|
||||
return;
|
||||
Inventory inv = Bukkit.createInventory(null, 27);
|
||||
inv.removeItem(new ItemStack[] { customItem(), customItem() });
|
||||
Block block = e.getBlockPlaced();
|
||||
Location loc = block.getLocation();
|
||||
ItemStack item = e.getItemInHand();
|
||||
String displayname = item.getItemMeta().getDisplayName();
|
||||
List<String> lore = item.getItemMeta().getLore();
|
||||
List<String> furnaceList = this.plugin.configs.getFData().getStringList("furnaces");
|
||||
int lv = 0;
|
||||
if (item.getItemMeta().hasDisplayName()) {
|
||||
if (isLevel(item, 1, addColor("&7Level 1 Furnace"))) {
|
||||
lv = 1;
|
||||
} else if (isLevel(item, 2, addColor("&fLevel 2 Furnace"))) {
|
||||
lv = 2;
|
||||
} else if (isLevel(item, 3, addColor("&aLevel 3 Furnace"))) {
|
||||
lv = 3;
|
||||
} else if (isLevel(item, 4, addColor("&9Level 4 Furnace"))) {
|
||||
lv = 4;
|
||||
} else if (isLevel(item, 5, addColor("&5Level 5 Furnace"))) {
|
||||
lv = 5;
|
||||
} else {
|
||||
e.getPlayer().sendMessage(this.plugin.lvmenu.getLangMsg("furnace-placed", Integer.valueOf(lv)));
|
||||
return;
|
||||
}
|
||||
furnaceList.add(serialize(loc, lv));
|
||||
this.furnaceTrack.put(loc, Integer.valueOf(lv));
|
||||
this.plugin.configs.getFData().set("furnaces", furnaceList);
|
||||
this.plugin.configs.saveData();
|
||||
}
|
||||
e.getPlayer().sendMessage(this.plugin.lvmenu.getLangMsg("furnace-placed", Integer.valueOf(lv)));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockBreak(BlockBreakEvent e) {
|
||||
if (e.getBlock().getType() != Material.FURNACE)
|
||||
return;
|
||||
Location loc = e.getBlock().getLocation();
|
||||
if (this.furnaceTrack.get(loc) != null) {
|
||||
if (this.plugin.configs.getConfig().getBoolean("refund-cost")) {
|
||||
int refund = 0;
|
||||
for (int i = 1; i <= ((Integer)this.furnaceTrack.get(loc)).intValue(); i++)
|
||||
refund += this.plugin.configs.getConfig().getInt("upgrades.level-" + i + ".cost");
|
||||
if (this.plugin.vaultEco != null) {
|
||||
this.plugin.vaultEco.depositPlayer((OfflinePlayer)e.getPlayer(), refund);
|
||||
} else {
|
||||
ItemStack item = customItem();
|
||||
item.setAmount(refund);
|
||||
e.getPlayer().getInventory().addItem(new ItemStack[] { item });
|
||||
}
|
||||
} else {
|
||||
e.setDropItems(false);
|
||||
ItemStack furnaceItem = new ItemStack(Material.FURNACE);
|
||||
ItemMeta meta = furnaceItem.getItemMeta();
|
||||
int lv = ((Integer)this.furnaceTrack.get(loc)).intValue();
|
||||
if (lv == 0) {
|
||||
List<String> list = getFData().getStringList("furnaces");
|
||||
list.remove(serialize(loc, ((Integer)this.furnaceTrack.get(loc)).intValue()));
|
||||
getFData().set("furnaces", list);
|
||||
this.plugin.configs.saveData();
|
||||
this.furnaceTrack.remove(loc);
|
||||
return;
|
||||
}
|
||||
setLangLore(meta, "level-" + lv + ".lore").setDisplayName(addColor(getLang().getString("level-" + lv + ".name")));
|
||||
furnaceItem.setItemMeta(meta);
|
||||
if (e.getPlayer().getGameMode() == GameMode.CREATIVE)
|
||||
return;
|
||||
Material mainHand = getItemInHand(e.getPlayer()).getType();
|
||||
if (mainHand.toString().contains("_PICKAXE")) {
|
||||
e.getBlock().getWorld().dropItemNaturally(loc.add(0.5D, 0.5D, 0.5D), furnaceItem);
|
||||
loc.add(-0.5D, -0.5D, -0.5D);
|
||||
}
|
||||
}
|
||||
List<String> furnaceList = getFData().getStringList("furnaces");
|
||||
furnaceList.remove(serialize(loc, ((Integer)this.furnaceTrack.get(loc)).intValue()));
|
||||
getFData().set("furnaces", furnaceList);
|
||||
this.plugin.configs.saveData();
|
||||
this.furnaceTrack.remove(loc);
|
||||
}
|
||||
}
|
||||
|
||||
private FileConfiguration getLang() {
|
||||
return this.plugin.configs.getLang();
|
||||
}
|
||||
|
||||
private FileConfiguration getFData() {
|
||||
return this.plugin.configs.getFData();
|
||||
}
|
||||
|
||||
String serialize(Location loc, int level) {
|
||||
if (loc == null)
|
||||
throw new IllegalArgumentException();
|
||||
return loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + level;
|
||||
}
|
||||
|
||||
private boolean isLevel(ItemStack item, int level, String defaultname) {
|
||||
String name;
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
List<String> lore = new ArrayList<>();
|
||||
if (meta.hasLore())
|
||||
lore = meta.getLore();
|
||||
if (!meta.hasDisplayName())
|
||||
return (level == 0);
|
||||
String displayname = meta.getDisplayName();
|
||||
if (getLang().isSet("level-" + level + ".name")) {
|
||||
name = addColor(getLang().getString("level-" + level + ".name"));
|
||||
} else {
|
||||
name = defaultname;
|
||||
}
|
||||
return (displayname.equals(name) && lore.equals(getLang().getStringList("level-" + level + ".lore")));
|
||||
}
|
||||
|
||||
private ItemMeta setLangLore(ItemMeta meta, String path) {
|
||||
List<String> lore = new ArrayList<>();
|
||||
for (String s : getLang().getStringList(path))
|
||||
lore.add(addColor(s));
|
||||
meta.setLore(lore);
|
||||
return meta;
|
||||
}
|
||||
|
||||
private String addColor(String string) {
|
||||
return ChatColor.translateAlternateColorCodes('&', string);
|
||||
}
|
||||
|
||||
ItemStack customItem() {
|
||||
String mat;
|
||||
ConfigurationSection iteminfo = this.plugin.configs.getConfig().getConfigurationSection("item");
|
||||
String[] split = iteminfo.getString("material-type").split(":");
|
||||
if (split.length == 1) {
|
||||
mat = split[0].toUpperCase();
|
||||
} else {
|
||||
mat = split[1].toUpperCase();
|
||||
}
|
||||
Material material = Material.valueOf(mat);
|
||||
String displayname = ChatColor.translateAlternateColorCodes('&', iteminfo.getString("displayname"));
|
||||
List<String> lore = new ArrayList<>();
|
||||
for (String s : iteminfo.getStringList("lore"))
|
||||
lore.add(ChatColor.translateAlternateColorCodes('&', s));
|
||||
ItemStack item = this.plugin.lvmenu.createItem(material, displayname, lore);
|
||||
if (!this.plugin.getServer().getVersion().contains("1.13"))
|
||||
item.getData().setData((byte)iteminfo.getInt("data"));
|
||||
return item;
|
||||
}
|
||||
|
||||
private ItemStack getItemInHand(Player p) {
|
||||
if (this.plugin.getServer().getVersion().contains("1.8"))
|
||||
return p.getItemInHand();
|
||||
return p.getInventory().getItemInMainHand();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user