From 1ea911437b3b85cd265dedbf5c49abbbc91404ec Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 11:33:20 +0200 Subject: [PATCH 1/4] Add Missile Info in lore Balance Support/Missile items with Map-Config --- src/de/steamwar/misslewars/Config.java | 4 + .../steamwar/misslewars/FightScoreboard.java | 2 +- src/de/steamwar/misslewars/items/Missile.java | 114 +++++++++--------- .../misslewars/items/SpecialItem.java | 43 +++++-- 4 files changed, 100 insertions(+), 63 deletions(-) diff --git a/src/de/steamwar/misslewars/Config.java b/src/de/steamwar/misslewars/Config.java index 7807c6e..d98c96a 100644 --- a/src/de/steamwar/misslewars/Config.java +++ b/src/de/steamwar/misslewars/Config.java @@ -48,6 +48,8 @@ public class Config { public static final int ShieldFlyTime; public static final int EndTime; + public static final double MissileChance; + static{ File configfile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml"); if(!configfile.exists()){ @@ -62,6 +64,8 @@ public class Config { ShieldFlyTime = config.getInt("ShieldFlyTime"); EndTime = config.getInt("EndTime"); + MissileChance = config.getDouble("MissileChance"); + ConfigurationSection arena = config.getConfigurationSection("Arena"); assert arena != null; ArenaMinX = arena.getInt("MinX"); diff --git a/src/de/steamwar/misslewars/FightScoreboard.java b/src/de/steamwar/misslewars/FightScoreboard.java index a9e5f93..1cdcde5 100644 --- a/src/de/steamwar/misslewars/FightScoreboard.java +++ b/src/de/steamwar/misslewars/FightScoreboard.java @@ -55,7 +55,7 @@ class FightScoreboard { Bukkit.getOnlinePlayers().forEach(player -> player.setScoreboard(scoreboard)); - }, 0, 200); + }, 0, 50); } static Scoreboard getScoreboard() { diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index a1434d5..dff93c8 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -22,68 +22,49 @@ 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.extension.input.ParserContext; 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.function.mask.BlockTypeMask; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.world.World; -import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockType; +import com.sk89q.worldedit.world.block.BlockTypes; +import de.steamwar.misslewars.Config; import de.steamwar.misslewars.MissileWars; -import io.netty.handler.logging.LogLevel; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.*; -import java.util.logging.Level; public class Missile extends SpecialItem { private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0)); - - private static Set tnt = new HashSet<>(); - - static { - try { - tnt = WorldEdit.getInstance().getBlockFactory().parseFromListInput("tnt", new ParserContext()); - } catch (Exception e) { - //Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e.getCause()); - } - } + private static final BlockType TNT = BlockTypes.TNT; + private static final int GRAPH_SIZE = 7; + private static final Random r = new Random(); + public static final List specialMissiles = new ArrayList<>(); private final Clipboard clipboard; private final ItemStack item; + private final int tntCount; - private Missile(File missileFile){ + private Missile(File missileFile, boolean special) { String[] strings = missileFile.getName().split("\\."); String name = strings[0]; String material = strings[1]; - if (!material.endsWith("_SPAWN_EGG")) { - material += "_SPAWN_EGG"; - } + if (!material.endsWith("_SPAWN_EGG")) material += "_SPAWN_EGG"; Material itemType = Material.valueOf(material); - List lore = new ArrayList<>(); - if (strings.length > 3) { - try { - lore.add(graph(Integer.parseInt(strings[2]), 7) + " §7Speed"); - lore.add(graph(Integer.parseInt(strings[3]), 7) + " §7Size"); - // lore.add("§7TNT §8: " + count()); - } catch (NumberFormatException e) { - - } - } - ClipboardFormat format = ClipboardFormats.findByFile(missileFile); try { assert format != null; @@ -92,19 +73,41 @@ public class Missile extends SpecialItem { throw new SecurityException("Corrupt missile"); } - item = new ItemStack(itemType, 1); - ItemMeta meta = item.getItemMeta(); - assert meta != null; - meta.setDisplayName("§c" + name); - meta.setLore(lore); - item.setItemMeta(meta); + List lore = new ArrayList<>(); + lore(lore, strings, 2, " §7Speed"); + lore(lore, strings, 3, " §7Size"); + int count; + try { + count = count(); + } catch (Exception e) { + count = 0; + } + tntCount = count; + lore.add("§7TNT §8: §e" + tntCount); + + item = createItem(itemType, "§c" + name, 1, lore, special); + if (special) specialMissiles.add(name); + } + + private void lore(List lore, String[] args, int index, String tag) { + if (args.length > index) { + try { + lore.add(graph(Integer.parseInt(args[index]), GRAPH_SIZE) + tag); + return; + } catch (NumberFormatException e) { + // Ignored + } + } + lore.add(graph(0, GRAPH_SIZE) + tag); } private String graph(int index, int size) { if (index > size) index = size; StringBuilder st = new StringBuilder(); st.append("§8[§e"); - st.append(repeat(index)); + if (index > 0) { + st.append(repeat(index)); + } st.append("§7"); st.append(repeat(size - index)); st.append("§8]"); @@ -121,19 +124,18 @@ public class Missile extends SpecialItem { } private int count() { - if (tnt.isEmpty()) { - return 0; - } - return WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1).countBlocks(clipboard.getRegion(), tnt); + EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); + BlockTypeMask blockTypeMask = new BlockTypeMask(clipboard, TNT); + return e.countBlocks(clipboard.getRegion(), blockTypeMask); } @Override - public ItemStack getItem(){ + public ItemStack getItem() { return item; } @Override - public boolean handleUse(Player p){ + public boolean handleUse(Player p) { BlockVector3 dimensions = clipboard.getDimensions(); Location location = p.getLocation(); BlockVector3 v = BlockVector3.ZERO; @@ -141,11 +143,11 @@ public class Missile extends SpecialItem { AffineTransform aT = new AffineTransform(); double yaw = (p.getLocation().getYaw() + 360f) % 360; - if(yaw > 45 && yaw <= 135) { + if (yaw > 45 && yaw <= 135) { aT = aT.rotateY(270); - }else if(yaw > 135 && yaw <= 225) { + } else if (yaw > 135 && yaw <= 225) { aT = aT.rotateY(180); - }else if(yaw > 225 && yaw <= 315) { + } else if (yaw > 225 && yaw <= 315) { aT = aT.rotateY(90); } @@ -161,16 +163,20 @@ public class Missile extends SpecialItem { return true; } - public static void init(){ - File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missiles"); - if(!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()){ + @Override + public boolean isMissile() { + return true; + } + + public static void init() { + File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "default-missiles"); + 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); } } + } diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index bf131e6..3b22372 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -19,8 +19,11 @@ package de.steamwar.misslewars.items; +import de.steamwar.misslewars.Config; 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; @@ -30,34 +33,58 @@ public abstract class SpecialItem { private static final Random random = new Random(); - private static List items = new ArrayList<>(); + private static List supportItems = new ArrayList<>(); + private static List missileItems = new ArrayList<>(); - SpecialItem(){ - items.add(this); + SpecialItem() { + if (this.isMissile()) { + missileItems.add(this); + return; + } + supportItems.add(this); } public abstract ItemStack getItem(); public abstract boolean handleUse(Player p); + public boolean isMissile() { + return false; + } 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.addUnsafeEnchantment(Enchantment.PROTECTION_FALL, 1); return item; } - public static boolean handleUse(ItemStack item, Player player){ - for(SpecialItem specialItem : items){ - if(item.isSimilar(specialItem.getItem())){ + public static boolean handleUse(ItemStack item, Player player) { + for (SpecialItem missileItem : missileItems) { + if (item.isSimilar(missileItem.getItem())) { + return missileItem.handleUse(player); + } + } + for (SpecialItem specialItem : supportItems) { + if (item.isSimilar(specialItem.getItem())) { return specialItem.handleUse(player); } } return false; } - public static ItemStack getRandomItem(){ - return items.get(random.nextInt(items.size())).getItem(); + public static ItemStack getRandomItem() { + if (random.nextDouble() > Config.MissileChance) { + return supportItems.get(random.nextInt(supportItems.size())).getItem(); + } else { + return missileItems.get(random.nextInt(missileItems.size())).getItem(); + } } } -- 2.39.5 From 8140ad40727c481b6e1de0a252d17ea8a0866477 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 11:42:41 +0200 Subject: [PATCH 2/4] Add all Missiles back --- src/de/steamwar/misslewars/items/Missile.java | 21 ++++++++++++++----- .../misslewars/items/SpecialItem.java | 8 +++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index dff93c8..c0e9417 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -52,13 +52,12 @@ public class Missile extends SpecialItem { private static final BlockType TNT = BlockTypes.TNT; private static final int GRAPH_SIZE = 7; private static final Random r = new Random(); - public static final List specialMissiles = new ArrayList<>(); private final Clipboard clipboard; private final ItemStack item; private final int tntCount; - private Missile(File missileFile, boolean special) { + private Missile(File missileFile) { String[] strings = missileFile.getName().split("\\."); String name = strings[0]; String material = strings[1]; @@ -85,8 +84,7 @@ public class Missile extends SpecialItem { tntCount = count; lore.add("§7TNT §8: §e" + tntCount); - item = createItem(itemType, "§c" + name, 1, lore, special); - if (special) specialMissiles.add(name); + item = createItem(itemType, "§c" + name, 1, lore, false); } private void lore(List lore, String[] args, int index, String tag) { @@ -175,7 +173,20 @@ public class Missile extends SpecialItem { } for (File missileFile : Objects.requireNonNull(missileFolder.listFiles())) { if (!missileFile.canRead() || !missileFile.isFile()) continue; - new Missile(missileFile, false); + new Missile(missileFile); + } + + otherInit(); + } + + public static void otherInit() { + File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missiles"); + 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); } } diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index 3b22372..9152eaa 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -39,9 +39,9 @@ public abstract class SpecialItem { SpecialItem() { if (this.isMissile()) { missileItems.add(this); - return; + } else { + supportItems.add(this); } - supportItems.add(this); } public abstract ItemStack getItem(); @@ -54,7 +54,7 @@ public abstract class SpecialItem { return createItem(material, name, amount, new ArrayList<>(), false); } - public ItemStack createItem(Material material, String name, int amount, List lore, boolean special) { + public ItemStack createItem(Material material, String name, int amount, List lore, boolean enchanted) { ItemStack item = new ItemStack(material, amount); ItemMeta meta = item.getItemMeta(); assert meta != null; @@ -62,7 +62,7 @@ public abstract class SpecialItem { meta.setLore(lore); meta.setDisplayName(name); item.setItemMeta(meta); - if (special) item.addUnsafeEnchantment(Enchantment.PROTECTION_FALL, 1); + if (enchanted) item.addUnsafeEnchantment(Enchantment.PROTECTION_FALL, 1); return item; } -- 2.39.5 From 8bcc1ec3bb819536ea3c54a5521016894e5ba17a Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 20:55:08 +0200 Subject: [PATCH 3/4] Remove Missile Random --- src/de/steamwar/misslewars/items/Missile.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index c0e9417..4d44798 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -33,7 +33,6 @@ import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; -import de.steamwar.misslewars.Config; import de.steamwar.misslewars.MissileWars; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -44,14 +43,16 @@ import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Random; 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; -- 2.39.5 From 152df40858d10231775d5286da99ec23ca7acb88 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 20:56:23 +0200 Subject: [PATCH 4/4] Remove Missile Random --- src/de/steamwar/misslewars/items/Missile.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index 4d44798..00eb458 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -46,7 +46,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.Random; public class Missile extends SpecialItem { -- 2.39.5