diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index a0277f9..a023222 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -22,17 +22,13 @@ package de.steamwar.misslewars.items; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.bukkit.BukkitWorld; -import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; -import com.sk89q.worldedit.extent.transform.BlockTransformExtent; import com.sk89q.worldedit.function.mask.BlockTypeMask; -import com.sk89q.worldedit.function.operation.ForwardExtentCopy; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.transform.AffineTransform; -import com.sk89q.worldedit.math.transform.Identity; import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockType; @@ -41,8 +37,8 @@ import de.steamwar.misslewars.MissileWars; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; -import org.bukkit.entity.TNTPrimed; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -52,17 +48,20 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.Random; import java.util.logging.Level; public class Missile extends SpecialItem { private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0)); private static final BlockType TNT = BlockTypes.TNT; + private static final int GRAPH_SIZE = 7; + private static final Random r = new Random(); private final Clipboard clipboard; private final ItemStack item; - private Missile(File missileFile){ + private Missile(File missileFile, boolean special) { String[] strings = missileFile.getName().split("\\."); String name = strings[0]; String material = strings[1]; @@ -78,37 +77,27 @@ public class Missile extends SpecialItem { } List lore = new ArrayList<>(); - if (strings.length > 2) { - try { - lore.add(graph(Integer.parseInt(strings[2]), 7) + " §7Speed"); - } catch (NumberFormatException e) { - lore.add(graph(0, 7) + " §7Speed"); - } - } else { - lore.add(graph(0, 7) + " §7Speed"); - } - if (strings.length > 3) { - try { - lore.add(graph(Integer.parseInt(strings[3]), 7) + " §7Size"); - } catch (NumberFormatException e) { - lore.add(graph(0, 7) + " §7Size"); - } - } else { - lore.add(graph(0, 7) + " §7Size"); - } + lore(lore, strings, 2, " §7Speed"); + lore(lore, strings, 3, " §7Size"); try { lore.add("§7TNT §8: §e" + count()); } catch (Exception e) { - Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e.getCause()); lore.add("§7TNT §8: §e?"); } - item = new ItemStack(itemType, 1); - ItemMeta meta = item.getItemMeta(); - assert meta != null; - meta.setDisplayName("§c" + name); - meta.setLore(lore); - item.setItemMeta(meta); + item = createItem(itemType, "§c" + name, 1, lore, special); + } + + private void lore(List lore, String[] args, int index, String tag) { + if (args.length > index) { + try { + lore.add(graph(Integer.parseInt(args[index]), 7) + tag); + return; + } catch (NumberFormatException e) { + // Ignored + } + } + lore.add(graph(0, 7) + tag); } private String graph(int index, int size) { @@ -175,12 +164,36 @@ public class Missile extends SpecialItem { public static void init(){ File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "default-missles"); - if(!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()){ + if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) { throw new SecurityException("Missiles could not be loaded"); } - for(File missileFile : Objects.requireNonNull(missileFolder.listFiles())){ - if(!missileFile.canRead() || !missileFile.isFile()) continue; - new Missile(missileFile); + for (File missileFile : Objects.requireNonNull(missileFolder.listFiles())) { + if (!missileFile.canRead() || !missileFile.isFile()) continue; + new Missile(missileFile, false); + } + } + + public static void randomInit() { + File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missles"); + if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) { + return; + } + File[] files = missileFolder.listFiles(); + if (files == null) { + return; + } + if (files.length < 4) { + return; + } + List pool = new ArrayList<>(files.length); + for (File missileFile : files) { + pool.add(missileFile); + } + + for (int i = 0; i < 4; i++) { + File missileFile = pool.remove(r.nextInt(pool.size())); + if (!missileFile.canRead() || !missileFile.isFile()) continue; + new Missile(missileFile, true); } } } diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index bf131e6..387f8d0 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -20,7 +20,9 @@ package de.steamwar.misslewars.items; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -40,11 +42,18 @@ public abstract class SpecialItem { public abstract boolean handleUse(Player p); public ItemStack createItem(Material material, String name, int amount) { + return createItem(material, name, amount, new ArrayList<>(), false); + } + + public ItemStack createItem(Material material, String name, int amount, List lore, boolean special) { ItemStack item = new ItemStack(material, amount); ItemMeta meta = item.getItemMeta(); assert meta != null; + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.setLore(lore); meta.setDisplayName(name); item.setItemMeta(meta); + if (special) item.addEnchantment(Enchantment.PROTECTION_FALL, 1); return item; }