From 6d8ed1f23e6fe4b449247f4b1fd6f0b3c68b5e65 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 01:51:49 -0600 Subject: [PATCH 01/23] Add dropped potions support to spawners Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 11 +++++ .../pgm/spawner/objects/SpawnablePotion.java | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index b20458a356..38f98b8b1e 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.logging.Logger; import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; @@ -24,6 +25,7 @@ import tc.oc.pgm.regions.RegionModule; import tc.oc.pgm.regions.RegionParser; import tc.oc.pgm.spawner.objects.SpawnableItem; +import tc.oc.pgm.spawner.objects.SpawnablePotion; import tc.oc.pgm.util.xml.InvalidXMLException; import tc.oc.pgm.util.xml.XMLUtils; @@ -87,6 +89,15 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) objects.add(item); } + for (Element spawnable : + XMLUtils.getChildren(element, "potion", "potions", "effect", "effects")) { + PotionEffect potionEffect = XMLUtils.parsePotionEffect(spawnable); + List thrownPotion = new ArrayList<>(); + thrownPotion.add(potionEffect); + SpawnablePotion potion = new SpawnablePotion(thrownPotion, numericID); + objects.add(potion); + } + SpawnerDefinition spawnerDefinition = new SpawnerDefinition( objects, diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java new file mode 100644 index 0000000000..bc3626fd25 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -0,0 +1,47 @@ +package tc.oc.pgm.spawner.objects; + +import java.util.List; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.ThrownPotion; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.potion.PotionEffect; +import tc.oc.pgm.api.PGM; +import tc.oc.pgm.api.match.Match; +import tc.oc.pgm.spawner.Spawnable; +import tc.oc.pgm.spawner.Spawner; + +public class SpawnablePotion implements Spawnable { + private ItemStack potionItem; + private String METADATA_VALUE; + + public SpawnablePotion(List potion, int spawnerID) { + this.METADATA_VALUE = Integer.toString(spawnerID); + ItemStack potionItem = new ItemStack(Material.POTION); + PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); + for (PotionEffect effect : potion) { + int level = effect.getAmplifier(); + int duration = effect.getDuration(); + // type, level, overwrite + potionMeta.addCustomEffect(new PotionEffect((effect.getType()), 20 * duration, level), false); + } + potionItem.setItemMeta(potionMeta); + this.potionItem = potionItem; + } + + @Override + public void spawn(Location location, Match match) { + ThrownPotion thrownPotion = location.getWorld().spawn(location, ThrownPotion.class); + thrownPotion.setItem(potionItem); + thrownPotion.getEffects(); + thrownPotion.setMetadata( + Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), METADATA_VALUE)); + } + + @Override + public int getSpawnCount() { + return 0; + } +} From 05bbe97eaad22bfcc5a468821a63ba447f4cd768 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 02:24:44 -0600 Subject: [PATCH 02/23] Make Splash potions have multiple effects, simplify code Signed-off-by: Patrick --- core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java | 5 ++++- .../java/tc/oc/pgm/spawner/objects/SpawnablePotion.java | 9 +++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 38f98b8b1e..860617996a 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -89,11 +89,14 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) objects.add(item); } + List thrownPotion = new ArrayList<>(); + // All listed effects for a spawner will be included in one splash potion for (Element spawnable : XMLUtils.getChildren(element, "potion", "potions", "effect", "effects")) { PotionEffect potionEffect = XMLUtils.parsePotionEffect(spawnable); - List thrownPotion = new ArrayList<>(); thrownPotion.add(potionEffect); + } + if (!thrownPotion.isEmpty()) { SpawnablePotion potion = new SpawnablePotion(thrownPotion, numericID); objects.add(potion); } diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index bc3626fd25..9833c954c5 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -22,10 +22,8 @@ public SpawnablePotion(List potion, int spawnerID) { ItemStack potionItem = new ItemStack(Material.POTION); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { - int level = effect.getAmplifier(); - int duration = effect.getDuration(); - // type, level, overwrite - potionMeta.addCustomEffect(new PotionEffect((effect.getType()), 20 * duration, level), false); + potionMeta.addCustomEffect( + new PotionEffect((effect.getType()), effect.getDuration(), effect.getAmplifier()), false); } potionItem.setItemMeta(potionMeta); this.potionItem = potionItem; @@ -35,13 +33,12 @@ public SpawnablePotion(List potion, int spawnerID) { public void spawn(Location location, Match match) { ThrownPotion thrownPotion = location.getWorld().spawn(location, ThrownPotion.class); thrownPotion.setItem(potionItem); - thrownPotion.getEffects(); thrownPotion.setMetadata( Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), METADATA_VALUE)); } @Override public int getSpawnCount() { - return 0; + return potionItem.getAmount(); } } From 24870589bff63fd320e85c634bdcf9f3a3a29976 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 14:54:48 -0600 Subject: [PATCH 03/23] Simplify code Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 17 +++++++---------- .../oc/pgm/spawner/objects/SpawnableItem.java | 4 ++-- .../oc/pgm/spawner/objects/SpawnablePotion.java | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 860617996a..04c31a6e06 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -52,7 +52,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) KitParser kitParser = factory.getKits(); FilterParser filterParser = factory.getFilters(); - int numericID = 0; + int numericId = 0; for (Element element : XMLUtils.flattenElements(doc.getRootElement(), "spawners", "spawner")) { Region spawnRegion = regionParser.parseRequiredRegionProperty(element, "spawn-region"); @@ -85,20 +85,17 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) XMLUtils.getChildren( element, "item")) { // TODO Add more types of spawnables once entity parser is built ItemStack stack = kitParser.parseItem(spawnable, false); - SpawnableItem item = new SpawnableItem(stack, numericID); + SpawnableItem item = new SpawnableItem(stack, numericId); objects.add(item); } List thrownPotion = new ArrayList<>(); // All listed effects for a spawner will be included in one splash potion - for (Element spawnable : - XMLUtils.getChildren(element, "potion", "potions", "effect", "effects")) { - PotionEffect potionEffect = XMLUtils.parsePotionEffect(spawnable); - thrownPotion.add(potionEffect); + for (Element spawnable : XMLUtils.getChildren(element, "potion")) { + thrownPotion.add(XMLUtils.parsePotionEffect(spawnable)); } if (!thrownPotion.isEmpty()) { - SpawnablePotion potion = new SpawnablePotion(thrownPotion, numericID); - objects.add(potion); + objects.add(new SpawnablePotion(thrownPotion, numericId)); } SpawnerDefinition spawnerDefinition = @@ -111,10 +108,10 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) minDelay, maxDelay, maxEntities, - numericID); + numericId); factory.getFeatures().addFeature(element, spawnerDefinition); spawnerModule.spawnerDefinitions.add(spawnerDefinition); - numericID++; + numericId++; } return spawnerModule.spawnerDefinitions.isEmpty() ? null : spawnerModule; diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java index ae5edd4f98..ce081d6fd8 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java @@ -14,9 +14,9 @@ public class SpawnableItem implements Spawnable { private ItemStack stack; private String METADATA_VALUE; - public SpawnableItem(ItemStack stack, int spawnerID) { + public SpawnableItem(ItemStack stack, int spawnerId) { this.stack = stack; - this.METADATA_VALUE = Integer.toString(spawnerID); + this.METADATA_VALUE = Integer.toString(spawnerId); } @Override diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 9833c954c5..a9de8643b0 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -17,8 +17,8 @@ public class SpawnablePotion implements Spawnable { private ItemStack potionItem; private String METADATA_VALUE; - public SpawnablePotion(List potion, int spawnerID) { - this.METADATA_VALUE = Integer.toString(spawnerID); + public SpawnablePotion(List potion, int spawnerId) { + this.METADATA_VALUE = Integer.toString(spawnerId); ItemStack potionItem = new ItemStack(Material.POTION); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { From dc2fbaa29f623e13f21bca237f530ac1919c5eba Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 16:40:21 -0600 Subject: [PATCH 04/23] Use inside for parsing Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 04c31a6e06..5fe9a5b902 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -26,7 +26,9 @@ import tc.oc.pgm.regions.RegionParser; import tc.oc.pgm.spawner.objects.SpawnableItem; import tc.oc.pgm.spawner.objects.SpawnablePotion; +import tc.oc.pgm.util.TimeUtils; import tc.oc.pgm.util.xml.InvalidXMLException; +import tc.oc.pgm.util.xml.Node; import tc.oc.pgm.util.xml.XMLUtils; public class SpawnerModule implements MapModule { @@ -90,11 +92,39 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) } List thrownPotion = new ArrayList<>(); - // All listed effects for a spawner will be included in one splash potion for (Element spawnable : XMLUtils.getChildren(element, "potion")) { - thrownPotion.add(XMLUtils.parsePotionEffect(spawnable)); - } - if (!thrownPotion.isEmpty()) { + // if mentions amplifier or duration attribute, merge it with lower + Duration duration = null; + Integer amplifier = null; + if (spawnable.getAttribute("duration") != null) { + duration = XMLUtils.parseSecondDuration(Node.fromAttr(spawnable)); + } + if (spawnable.getAttribute("amplifier") != null) { + amplifier = XMLUtils.parseNumber(spawnable, Integer.class); + } + for (Element effectEl : XMLUtils.getChildren(element.getChild("effect"))) { + Attribute presetAttribute = effectEl.getAttribute("duration"); + Attribute presetAmplifier = effectEl.getAttribute("amplifier"); + PotionEffect presetPotion = XMLUtils.parsePotionEffect(effectEl); + if (duration == null && amplifier == null) { + thrownPotion.add(XMLUtils.parsePotionEffect(effectEl)); + } + // if duration is mentioned and amplifier is not mentioned in + else if (duration != null && amplifier == null) { + PotionEffect potionEffect = + new PotionEffect( + presetPotion.getType(), + (int) TimeUtils.toTicks(duration), + presetPotion.getAmplifier()); + thrownPotion.add(potionEffect); + } + // if duration is not mentioned and amplifier is mentioned in + else if (duration == null) { + PotionEffect potionEffect = + new PotionEffect(presetPotion.getType(), presetPotion.getDuration(), amplifier); + thrownPotion.add(potionEffect); + } + } objects.add(new SpawnablePotion(thrownPotion, numericId)); } From f779edd424c9584631da10b0c255b95b70cd9522 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 17:55:33 -0600 Subject: [PATCH 05/23] Add evaluation Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 5fe9a5b902..19d1643db1 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -97,32 +97,47 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) Duration duration = null; Integer amplifier = null; if (spawnable.getAttribute("duration") != null) { - duration = XMLUtils.parseSecondDuration(Node.fromAttr(spawnable)); + duration = XMLUtils.parseSecondDuration(Node.fromAttr(spawnable), "duration"); } if (spawnable.getAttribute("amplifier") != null) { amplifier = XMLUtils.parseNumber(spawnable, Integer.class); } - for (Element effectEl : XMLUtils.getChildren(element.getChild("effect"))) { - Attribute presetAttribute = effectEl.getAttribute("duration"); + for (Element effectEl : XMLUtils.getChildren(spawnable, "effect")) { + Attribute presetDuration = effectEl.getAttribute("duration"); Attribute presetAmplifier = effectEl.getAttribute("amplifier"); PotionEffect presetPotion = XMLUtils.parsePotionEffect(effectEl); - if (duration == null && amplifier == null) { + if (duration == null && amplifier == null && presetDuration != null) { thrownPotion.add(XMLUtils.parsePotionEffect(effectEl)); - } - // if duration is mentioned and amplifier is not mentioned in - else if (duration != null && amplifier == null) { - PotionEffect potionEffect = - new PotionEffect( - presetPotion.getType(), - (int) TimeUtils.toTicks(duration), - presetPotion.getAmplifier()); - thrownPotion.add(potionEffect); - } - // if duration is not mentioned and amplifier is mentioned in - else if (duration == null) { - PotionEffect potionEffect = - new PotionEffect(presetPotion.getType(), presetPotion.getDuration(), amplifier); - thrownPotion.add(potionEffect); + } else if (duration != null) { + // if mentions duration and does not mention amplifier + if (presetAmplifier == null) { + PotionEffect potionEffect = + new PotionEffect( + presetPotion.getType(), + (int) TimeUtils.toTicks(duration), + presetPotion.getAmplifier()); + thrownPotion.add(potionEffect); + } + // if does not have duration and has amplifier + else if (presetDuration == null) { + PotionEffect potionEffect = + new PotionEffect( + presetPotion.getType(), + (int) TimeUtils.toTicks(duration), + XMLUtils.parseNumber(presetAmplifier, Integer.class)); + thrownPotion.add(potionEffect); + } + // if has neither attributes + else if (amplifier != null) { + PotionEffect potionEffect = + new PotionEffect( + presetPotion.getType(), (int) TimeUtils.toTicks(duration), amplifier); + thrownPotion.add(potionEffect); + } + } else { + throw new InvalidXMLException( + " must have a duration assigned to it by a duration attribute or ", + effectEl); } } objects.add(new SpawnablePotion(thrownPotion, numericId)); From c62727913198fd8d598d22ce042871ff3cd5a953 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 21:21:35 -0600 Subject: [PATCH 06/23] Fix evaluation of and Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/SpawnerModule.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 19d1643db1..b15a3eb0c0 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -97,10 +97,10 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) Duration duration = null; Integer amplifier = null; if (spawnable.getAttribute("duration") != null) { - duration = XMLUtils.parseSecondDuration(Node.fromAttr(spawnable), "duration"); + duration = XMLUtils.parseSecondDuration(Node.fromAttr(spawnable, "duration")); } if (spawnable.getAttribute("amplifier") != null) { - amplifier = XMLUtils.parseNumber(spawnable, Integer.class); + amplifier = XMLUtils.parseNumber(spawnable.getAttribute("amplifier"), Integer.class) - 1; } for (Element effectEl : XMLUtils.getChildren(spawnable, "effect")) { Attribute presetDuration = effectEl.getAttribute("duration"); @@ -110,16 +110,16 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) thrownPotion.add(XMLUtils.parsePotionEffect(effectEl)); } else if (duration != null) { // if mentions duration and does not mention amplifier - if (presetAmplifier == null) { + if (presetDuration != null && presetAmplifier == null) { PotionEffect potionEffect = new PotionEffect( presetPotion.getType(), - (int) TimeUtils.toTicks(duration), + (int) TimeUtils.toTicks(XMLUtils.parseSecondDuration(Node.fromAttr(effectEl, "duration"))), presetPotion.getAmplifier()); thrownPotion.add(potionEffect); } // if does not have duration and has amplifier - else if (presetDuration == null) { + else if (presetDuration == null && presetAmplifier != null) { PotionEffect potionEffect = new PotionEffect( presetPotion.getType(), @@ -128,7 +128,7 @@ else if (presetDuration == null) { thrownPotion.add(potionEffect); } // if has neither attributes - else if (amplifier != null) { + else if (presetDuration == null && amplifier != null) { PotionEffect potionEffect = new PotionEffect( presetPotion.getType(), (int) TimeUtils.toTicks(duration), amplifier); From 5a3e16b77d6d7afd1e7f346224bf6e1b4d50c9c0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 22:39:04 -0600 Subject: [PATCH 07/23] Simplify Code Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index b15a3eb0c0..2886e0687e 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -92,17 +92,18 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) } List thrownPotion = new ArrayList<>(); - for (Element spawnable : XMLUtils.getChildren(element, "potion")) { - // if mentions amplifier or duration attribute, merge it with lower + for (Element potionEl : XMLUtils.getChildren(element, "potion")) { + // if mentions amplifier or duration attribute, merge it with lower if + // not present Duration duration = null; Integer amplifier = null; - if (spawnable.getAttribute("duration") != null) { - duration = XMLUtils.parseSecondDuration(Node.fromAttr(spawnable, "duration")); + if (potionEl.getAttribute("duration") != null) { + duration = XMLUtils.parseSecondDuration(Node.fromAttr(potionEl, "duration")); } - if (spawnable.getAttribute("amplifier") != null) { - amplifier = XMLUtils.parseNumber(spawnable.getAttribute("amplifier"), Integer.class) - 1; + if (potionEl.getAttribute("amplifier") != null) { + amplifier = XMLUtils.parseNumber(potionEl.getAttribute("amplifier"), Integer.class); } - for (Element effectEl : XMLUtils.getChildren(spawnable, "effect")) { + for (Element effectEl : XMLUtils.getChildren(potionEl, "effect")) { Attribute presetDuration = effectEl.getAttribute("duration"); Attribute presetAmplifier = effectEl.getAttribute("amplifier"); PotionEffect presetPotion = XMLUtils.parsePotionEffect(effectEl); @@ -111,28 +112,20 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) } else if (duration != null) { // if mentions duration and does not mention amplifier if (presetDuration != null && presetAmplifier == null) { - PotionEffect potionEffect = - new PotionEffect( - presetPotion.getType(), - (int) TimeUtils.toTicks(XMLUtils.parseSecondDuration(Node.fromAttr(effectEl, "duration"))), - presetPotion.getAmplifier()); - thrownPotion.add(potionEffect); + addPotionEffect( + thrownPotion, + presetPotion, + XMLUtils.parseSecondDuration(Node.fromAttr(effectEl, "duration")), + potionEl.getAttribute("amplifier")); } // if does not have duration and has amplifier else if (presetDuration == null && presetAmplifier != null) { - PotionEffect potionEffect = - new PotionEffect( - presetPotion.getType(), - (int) TimeUtils.toTicks(duration), - XMLUtils.parseNumber(presetAmplifier, Integer.class)); - thrownPotion.add(potionEffect); + addPotionEffect(thrownPotion, presetPotion, duration, presetAmplifier); } // if has neither attributes else if (presetDuration == null && amplifier != null) { - PotionEffect potionEffect = - new PotionEffect( - presetPotion.getType(), (int) TimeUtils.toTicks(duration), amplifier); - thrownPotion.add(potionEffect); + addPotionEffect( + thrownPotion, presetPotion, duration, potionEl.getAttribute("amplifier")); } } else { throw new InvalidXMLException( @@ -161,5 +154,19 @@ else if (presetDuration == null && amplifier != null) { return spawnerModule.spawnerDefinitions.isEmpty() ? null : spawnerModule; } + + public void addPotionEffect( + List thrownPotion, + PotionEffect potion, + Duration duration, + Attribute amplifier) + throws InvalidXMLException { + PotionEffect potionEffect = + new PotionEffect( + potion.getType(), + (int) TimeUtils.toTicks(duration), + XMLUtils.parseNumber(amplifier, Integer.class) - 1); + thrownPotion.add(potionEffect); + } } } From a6a533cee0766b7c0399efe6e10c170d790f2526 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 2 May 2022 22:48:16 -0600 Subject: [PATCH 08/23] Use finals for SpawnableItem and SpawnablePotion Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/objects/SpawnableItem.java | 8 ++++---- .../java/tc/oc/pgm/spawner/objects/SpawnablePotion.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java index ce081d6fd8..c459ae7d48 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java @@ -11,18 +11,18 @@ public class SpawnableItem implements Spawnable { - private ItemStack stack; - private String METADATA_VALUE; + private final ItemStack stack; + private final String metadataValue; public SpawnableItem(ItemStack stack, int spawnerId) { this.stack = stack; - this.METADATA_VALUE = Integer.toString(spawnerId); + this.metadataValue = Integer.toString(spawnerId); } @Override public void spawn(Location location, Match match) { Item item = location.getWorld().dropItem(location, stack); - item.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), METADATA_VALUE)); + item.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), metadataValue)); } @Override diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index a9de8643b0..872ad7c32f 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -14,11 +14,11 @@ import tc.oc.pgm.spawner.Spawner; public class SpawnablePotion implements Spawnable { - private ItemStack potionItem; - private String METADATA_VALUE; + private final ItemStack potionItem; + private final String metadataValue; public SpawnablePotion(List potion, int spawnerId) { - this.METADATA_VALUE = Integer.toString(spawnerId); + this.metadataValue = Integer.toString(spawnerId); ItemStack potionItem = new ItemStack(Material.POTION); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { @@ -34,7 +34,7 @@ public void spawn(Location location, Match match) { ThrownPotion thrownPotion = location.getWorld().spawn(location, ThrownPotion.class); thrownPotion.setItem(potionItem); thrownPotion.setMetadata( - Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), METADATA_VALUE)); + Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), metadataValue)); } @Override From 3e405a96aa22f75d41feb05f6735853279941b9c Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 17:21:14 -0600 Subject: [PATCH 09/23] Refactor, convert spawner ID to string Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/SpawnerModule.java | 8 +++----- .../tc/oc/pgm/spawner/objects/SpawnableItem.java | 8 ++++---- .../tc/oc/pgm/spawner/objects/SpawnablePotion.java | 12 +++++------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 2886e0687e..a809317a20 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -83,11 +83,9 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) filterParser.parseFilterProperty(element, "filter", StaticFilter.ALLOW); List objects = new ArrayList<>(); - for (Element spawnable : - XMLUtils.getChildren( - element, "item")) { // TODO Add more types of spawnables once entity parser is built + for (Element spawnable : XMLUtils.getChildren(element, "item")) { ItemStack stack = kitParser.parseItem(spawnable, false); - SpawnableItem item = new SpawnableItem(stack, numericId); + SpawnableItem item = new SpawnableItem(stack, "spawner-" + numericId); objects.add(item); } @@ -133,7 +131,7 @@ else if (presetDuration == null && amplifier != null) { effectEl); } } - objects.add(new SpawnablePotion(thrownPotion, numericId)); + objects.add(new SpawnablePotion(thrownPotion, "spawner-" + numericId)); } SpawnerDefinition spawnerDefinition = diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java index c459ae7d48..84c91aeeb6 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnableItem.java @@ -12,17 +12,17 @@ public class SpawnableItem implements Spawnable { private final ItemStack stack; - private final String metadataValue; + private final String spawnerId; - public SpawnableItem(ItemStack stack, int spawnerId) { + public SpawnableItem(ItemStack stack, String spawnerId) { this.stack = stack; - this.metadataValue = Integer.toString(spawnerId); + this.spawnerId = spawnerId; } @Override public void spawn(Location location, Match match) { Item item = location.getWorld().dropItem(location, stack); - item.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), metadataValue)); + item.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), spawnerId)); } @Override diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 872ad7c32f..2a40ac9f1e 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -15,15 +15,14 @@ public class SpawnablePotion implements Spawnable { private final ItemStack potionItem; - private final String metadataValue; + private final String spawnerId; - public SpawnablePotion(List potion, int spawnerId) { - this.metadataValue = Integer.toString(spawnerId); + public SpawnablePotion(List potion, String spawnerId) { + this.spawnerId = spawnerId; ItemStack potionItem = new ItemStack(Material.POTION); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { - potionMeta.addCustomEffect( - new PotionEffect((effect.getType()), effect.getDuration(), effect.getAmplifier()), false); + potionMeta.addCustomEffect(effect, false); } potionItem.setItemMeta(potionMeta); this.potionItem = potionItem; @@ -33,8 +32,7 @@ public SpawnablePotion(List potion, int spawnerId) { public void spawn(Location location, Match match) { ThrownPotion thrownPotion = location.getWorld().spawn(location, ThrownPotion.class); thrownPotion.setItem(potionItem); - thrownPotion.setMetadata( - Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), metadataValue)); + thrownPotion.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), spawnerId)); } @Override From 48eae5c78bfcf3420aab34101798f7b2cdad70a2 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 18:57:12 -0600 Subject: [PATCH 10/23] Use AtomicInteger for numericID, rearrange SpawnerDefinition Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerDefinition.java | 6 +++--- .../main/java/tc/oc/pgm/spawner/SpawnerModule.java | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java index b3345b913a..5548a93c36 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java @@ -19,6 +19,7 @@ public class SpawnerDefinition implements FeatureDefinition { public final int numericID; public SpawnerDefinition( + int numericID, List objects, Region spawnRegion, Region playerRegion, @@ -26,8 +27,8 @@ public SpawnerDefinition( Duration delay, Duration minDelay, Duration maxDelay, - int maxEntities, - int numericID) { + int maxEntities) { + this.numericID = numericID; this.spawnRegion = spawnRegion; this.playerRegion = playerRegion; this.maxEntities = maxEntities; @@ -36,6 +37,5 @@ public SpawnerDefinition( this.delay = delay; this.objects = objects; this.playerFilter = playerFilter; - this.numericID = numericID; } } diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index a809317a20..32fd90a2b0 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; @@ -53,8 +54,8 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) RegionParser regionParser = factory.getRegions(); KitParser kitParser = factory.getKits(); FilterParser filterParser = factory.getFilters(); + AtomicInteger numericId = new AtomicInteger(0); - int numericId = 0; for (Element element : XMLUtils.flattenElements(doc.getRootElement(), "spawners", "spawner")) { Region spawnRegion = regionParser.parseRequiredRegionProperty(element, "spawn-region"); @@ -85,7 +86,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) List objects = new ArrayList<>(); for (Element spawnable : XMLUtils.getChildren(element, "item")) { ItemStack stack = kitParser.parseItem(spawnable, false); - SpawnableItem item = new SpawnableItem(stack, "spawner-" + numericId); + SpawnableItem item = new SpawnableItem(stack, "spawner-" + numericId.get()); objects.add(item); } @@ -131,11 +132,12 @@ else if (presetDuration == null && amplifier != null) { effectEl); } } - objects.add(new SpawnablePotion(thrownPotion, "spawner-" + numericId)); + objects.add(new SpawnablePotion(thrownPotion, "spawner-" + numericId.get())); } SpawnerDefinition spawnerDefinition = new SpawnerDefinition( + numericId.getAndIncrement(), objects, spawnRegion, playerRegion, @@ -143,11 +145,9 @@ else if (presetDuration == null && amplifier != null) { delay, minDelay, maxDelay, - maxEntities, - numericId); + maxEntities); factory.getFeatures().addFeature(element, spawnerDefinition); spawnerModule.spawnerDefinitions.add(spawnerDefinition); - numericId++; } return spawnerModule.spawnerDefinitions.isEmpty() ? null : spawnerModule; From 3212cb2cdaeef78a5f4e1615b7644ef46ec5b738 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 19:03:06 -0600 Subject: [PATCH 11/23] Clone potionItem Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 2a40ac9f1e..4876557e4f 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -31,7 +31,7 @@ public SpawnablePotion(List potion, String spawnerId) { @Override public void spawn(Location location, Match match) { ThrownPotion thrownPotion = location.getWorld().spawn(location, ThrownPotion.class); - thrownPotion.setItem(potionItem); + thrownPotion.setItem(potionItem.clone()); thrownPotion.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), spawnerId)); } From e9567063f06f41b5c28bd5135780bb858a1958b2 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 21:05:21 -0600 Subject: [PATCH 12/23] Parse potion effects properly Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 64 +++---------------- 1 file changed, 10 insertions(+), 54 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 32fd90a2b0..3a2cd8fbf7 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -22,12 +22,14 @@ import tc.oc.pgm.filters.FilterModule; import tc.oc.pgm.filters.FilterParser; import tc.oc.pgm.filters.StaticFilter; +import tc.oc.pgm.flag.post.SinglePost; import tc.oc.pgm.kits.KitParser; import tc.oc.pgm.regions.RegionModule; import tc.oc.pgm.regions.RegionParser; import tc.oc.pgm.spawner.objects.SpawnableItem; import tc.oc.pgm.spawner.objects.SpawnablePotion; import tc.oc.pgm.util.TimeUtils; +import tc.oc.pgm.util.xml.InheritingElement; import tc.oc.pgm.util.xml.InvalidXMLException; import tc.oc.pgm.util.xml.Node; import tc.oc.pgm.util.xml.XMLUtils; @@ -90,49 +92,17 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) objects.add(item); } - List thrownPotion = new ArrayList<>(); + ImmutableList.Builder chBuilder = ImmutableList.builder(); for (Element potionEl : XMLUtils.getChildren(element, "potion")) { - // if mentions amplifier or duration attribute, merge it with lower if - // not present - Duration duration = null; - Integer amplifier = null; - if (potionEl.getAttribute("duration") != null) { - duration = XMLUtils.parseSecondDuration(Node.fromAttr(potionEl, "duration")); + for (Element potionChild : potionEl.getChildren("effect")) { + chBuilder.add(XMLUtils.parsePotionEffect(new InheritingElement(potionChild))); } - if (potionEl.getAttribute("amplifier") != null) { - amplifier = XMLUtils.parseNumber(potionEl.getAttribute("amplifier"), Integer.class); + ImmutableList potionChildren = chBuilder.build(); + // This should never happen because of default values set by parsePotionEffect + if (potionChildren.isEmpty()) { + throw new InvalidXMLException("Expected child effects, but found none", element); } - for (Element effectEl : XMLUtils.getChildren(potionEl, "effect")) { - Attribute presetDuration = effectEl.getAttribute("duration"); - Attribute presetAmplifier = effectEl.getAttribute("amplifier"); - PotionEffect presetPotion = XMLUtils.parsePotionEffect(effectEl); - if (duration == null && amplifier == null && presetDuration != null) { - thrownPotion.add(XMLUtils.parsePotionEffect(effectEl)); - } else if (duration != null) { - // if mentions duration and does not mention amplifier - if (presetDuration != null && presetAmplifier == null) { - addPotionEffect( - thrownPotion, - presetPotion, - XMLUtils.parseSecondDuration(Node.fromAttr(effectEl, "duration")), - potionEl.getAttribute("amplifier")); - } - // if does not have duration and has amplifier - else if (presetDuration == null && presetAmplifier != null) { - addPotionEffect(thrownPotion, presetPotion, duration, presetAmplifier); - } - // if has neither attributes - else if (presetDuration == null && amplifier != null) { - addPotionEffect( - thrownPotion, presetPotion, duration, potionEl.getAttribute("amplifier")); - } - } else { - throw new InvalidXMLException( - " must have a duration assigned to it by a duration attribute or ", - effectEl); - } - } - objects.add(new SpawnablePotion(thrownPotion, "spawner-" + numericId.get())); + objects.add(new SpawnablePotion(potionChildren, "spawner-" + numericId.get())); } SpawnerDefinition spawnerDefinition = @@ -152,19 +122,5 @@ else if (presetDuration == null && amplifier != null) { return spawnerModule.spawnerDefinitions.isEmpty() ? null : spawnerModule; } - - public void addPotionEffect( - List thrownPotion, - PotionEffect potion, - Duration duration, - Attribute amplifier) - throws InvalidXMLException { - PotionEffect potionEffect = - new PotionEffect( - potion.getType(), - (int) TimeUtils.toTicks(duration), - XMLUtils.parseNumber(amplifier, Integer.class) - 1); - thrownPotion.add(potionEffect); - } } } From fb039252242cc0b5c8fd2ab1768816dfe57111bc Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 21:08:37 -0600 Subject: [PATCH 13/23] Rename spawner Element to spawnerEl Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 3a2cd8fbf7..74f5518454 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -58,17 +58,17 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) FilterParser filterParser = factory.getFilters(); AtomicInteger numericId = new AtomicInteger(0); - for (Element element : + for (Element spawnerEl : XMLUtils.flattenElements(doc.getRootElement(), "spawners", "spawner")) { - Region spawnRegion = regionParser.parseRequiredRegionProperty(element, "spawn-region"); - Region playerRegion = regionParser.parseRequiredRegionProperty(element, "player-region"); - Attribute delayAttr = element.getAttribute("delay"); - Attribute minDelayAttr = element.getAttribute("min-delay"); - Attribute maxDelayAttr = element.getAttribute("max-delay"); + Region spawnRegion = regionParser.parseRequiredRegionProperty(spawnerEl, "spawn-region"); + Region playerRegion = regionParser.parseRequiredRegionProperty(spawnerEl, "player-region"); + Attribute delayAttr = spawnerEl.getAttribute("delay"); + Attribute minDelayAttr = spawnerEl.getAttribute("min-delay"); + Attribute maxDelayAttr = spawnerEl.getAttribute("max-delay"); if ((minDelayAttr != null || maxDelayAttr != null) && delayAttr != null) { throw new InvalidXMLException( - "Attribute 'minDelay' and 'maxDelay' cannot be combined with 'delay'", element); + "Attribute 'minDelay' and 'maxDelay' cannot be combined with 'delay'", spawnerEl); } Duration delay = XMLUtils.parseDuration(delayAttr, Duration.ofSeconds(10)); @@ -76,31 +76,30 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) Duration maxDelay = XMLUtils.parseDuration(maxDelayAttr, delay); if (maxDelay.compareTo(minDelay) <= 0 && minDelayAttr != null && maxDelayAttr != null) { - throw new InvalidXMLException("Max-delay must be longer than min-delay", element); + throw new InvalidXMLException("Max-delay must be longer than min-delay", spawnerEl); } int maxEntities = XMLUtils.parseNumber( - element.getAttribute("max-entities"), Integer.class, Integer.MAX_VALUE); + spawnerEl.getAttribute("max-entities"), Integer.class, Integer.MAX_VALUE); Filter playerFilter = - filterParser.parseFilterProperty(element, "filter", StaticFilter.ALLOW); + filterParser.parseFilterProperty(spawnerEl, "filter", StaticFilter.ALLOW); List objects = new ArrayList<>(); - for (Element spawnable : XMLUtils.getChildren(element, "item")) { - ItemStack stack = kitParser.parseItem(spawnable, false); + for (Element itemEl : XMLUtils.getChildren(spawnerEl, "item")) { + ItemStack stack = kitParser.parseItem(itemEl, false); SpawnableItem item = new SpawnableItem(stack, "spawner-" + numericId.get()); objects.add(item); } ImmutableList.Builder chBuilder = ImmutableList.builder(); - for (Element potionEl : XMLUtils.getChildren(element, "potion")) { + for (Element potionEl : XMLUtils.getChildren(spawnerEl, "potion")) { for (Element potionChild : potionEl.getChildren("effect")) { chBuilder.add(XMLUtils.parsePotionEffect(new InheritingElement(potionChild))); } ImmutableList potionChildren = chBuilder.build(); - // This should never happen because of default values set by parsePotionEffect if (potionChildren.isEmpty()) { - throw new InvalidXMLException("Expected child effects, but found none", element); + throw new InvalidXMLException("Expected child effects, but found none", spawnerEl); } objects.add(new SpawnablePotion(potionChildren, "spawner-" + numericId.get())); } @@ -116,7 +115,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) minDelay, maxDelay, maxEntities); - factory.getFeatures().addFeature(element, spawnerDefinition); + factory.getFeatures().addFeature(spawnerEl, spawnerDefinition); spawnerModule.spawnerDefinitions.add(spawnerDefinition); } From ff952832183dd4667773c8e94cd3be2da942ba74 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 21:09:15 -0600 Subject: [PATCH 14/23] Reformat code Signed-off-by: Patrick --- core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 74f5518454..6260c50774 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -22,16 +22,13 @@ import tc.oc.pgm.filters.FilterModule; import tc.oc.pgm.filters.FilterParser; import tc.oc.pgm.filters.StaticFilter; -import tc.oc.pgm.flag.post.SinglePost; import tc.oc.pgm.kits.KitParser; import tc.oc.pgm.regions.RegionModule; import tc.oc.pgm.regions.RegionParser; import tc.oc.pgm.spawner.objects.SpawnableItem; import tc.oc.pgm.spawner.objects.SpawnablePotion; -import tc.oc.pgm.util.TimeUtils; import tc.oc.pgm.util.xml.InheritingElement; import tc.oc.pgm.util.xml.InvalidXMLException; -import tc.oc.pgm.util.xml.Node; import tc.oc.pgm.util.xml.XMLUtils; public class SpawnerModule implements MapModule { From cd081d4149f787b4d06cbd533a68a4b8eb8fd96c Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 4 May 2022 23:48:02 -0600 Subject: [PATCH 15/23] Add damage value to determine potion color Signed-off-by: Patrick --- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 18 +++++++++++++++++- .../pgm/spawner/objects/SpawnablePotion.java | 7 ++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 6260c50774..ed0fa23db5 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -9,6 +9,7 @@ import java.util.logging.Logger; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionType; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; @@ -98,7 +99,22 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) if (potionChildren.isEmpty()) { throw new InvalidXMLException("Expected child effects, but found none", spawnerEl); } - objects.add(new SpawnablePotion(potionChildren, "spawner-" + numericId.get())); + int potionName = 0; + if (potionEl.getAttribute("damage") != null) { + potionName = XMLUtils.parseNumber(potionEl.getAttribute("damage"), Integer.class, 0); + } else { + for (PotionEffect potionEffect : potionChildren) { + // PotionType lists "true" potions, PotionEffectType lists all possible status effects + // (ie wither) + // Use the first listed PotionType for potion color + if (PotionType.getByEffect(potionEffect.getType()) != null) { + potionName = PotionType.getByEffect(potionEffect.getType()).getDamageValue(); + break; + } + } + } + objects.add( + new SpawnablePotion(potionChildren, potionName, "spawner-" + numericId.get())); } SpawnerDefinition spawnerDefinition = diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 4876557e4f..3c73950fe3 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -2,11 +2,11 @@ import java.util.List; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.entity.ThrownPotion; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.match.Match; @@ -17,9 +17,10 @@ public class SpawnablePotion implements Spawnable { private final ItemStack potionItem; private final String spawnerId; - public SpawnablePotion(List potion, String spawnerId) { + public SpawnablePotion(List potion, int potionName, String spawnerId) { this.spawnerId = spawnerId; - ItemStack potionItem = new ItemStack(Material.POTION); + // Potion "name" determines potion color + ItemStack potionItem = new ItemStack(new Potion(potionName).splash().toItemStack(1)); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { potionMeta.addCustomEffect(effect, false); From f0630e5be046b962cc9a4f08f7ac94b392248541 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 6 May 2022 03:08:28 -0600 Subject: [PATCH 16/23] Make spawnerID string Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/Spawner.java | 21 +++++++++---------- .../tc/oc/pgm/spawner/SpawnerDefinition.java | 6 +++--- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 6 +++--- .../pgm/spawner/objects/SpawnablePotion.java | 4 ++-- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/Spawner.java b/core/src/main/java/tc/oc/pgm/spawner/Spawner.java index 4da0c4fd15..d8f4c99fe4 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/Spawner.java +++ b/core/src/main/java/tc/oc/pgm/spawner/Spawner.java @@ -1,5 +1,6 @@ package tc.oc.pgm.spawner; +import java.util.Objects; import org.bukkit.Effect; import org.bukkit.Location; import org.bukkit.event.EventHandler; @@ -36,7 +37,6 @@ public class Spawner implements Listener, Tickable { public Spawner(SpawnerDefinition definition, Match match) { this.definition = definition; this.match = match; - this.lastTick = match.getTick().tick; this.players = new OnlinePlayerMapAdapter<>(PGM.get()); calculateDelay(); @@ -51,7 +51,6 @@ public void tick(Match match, Tick tick) { definition.spawnRegion.getRandom(match.getRandom()).toLocation(match.getWorld()); spawnable.spawn(location, match); match.getWorld().spigot().playEffect(location, Effect.FLAME, 0, 0, 0, 0.15f, 0, 0, 40, 64); - spawnedEntities = spawnedEntities + spawnable.getSpawnCount(); } calculateDelay(); @@ -102,18 +101,18 @@ public void onItemMerge(ItemMergeEvent event) { return; } - int entitySpawnerID = -1; - int targetSpawnerID = -1; + String entitySpawnerId = ""; + String targetSpawnerId = ""; if (event.getEntity().hasMetadata(METADATA_KEY)) { - entitySpawnerID = - MetadataUtils.getMetadata(event.getEntity(), METADATA_KEY, PGM.get()).asInt(); + entitySpawnerId = + MetadataUtils.getMetadata(event.getEntity(), METADATA_KEY, PGM.get()).toString(); } if (event.getTarget().hasMetadata(METADATA_KEY)) { - targetSpawnerID = - MetadataUtils.getMetadata(event.getTarget(), METADATA_KEY, PGM.get()).asInt(); + targetSpawnerId = + MetadataUtils.getMetadata(event.getTarget(), METADATA_KEY, PGM.get()).toString(); } // Cancel the merge if the items are from different PGM spawners - if (entitySpawnerID != targetSpawnerID) { + if (!entitySpawnerId.equals(targetSpawnerId)) { event.setCancelled(true); return; } @@ -121,8 +120,8 @@ public void onItemMerge(ItemMergeEvent event) { private void handleEntityRemoveEvent(Metadatable metadatable, int amount) { if (metadatable.hasMetadata(METADATA_KEY)) { - if (MetadataUtils.getMetadata(metadatable, METADATA_KEY, PGM.get()).asInt() - == definition.numericID) { + if (Objects.equals( + MetadataUtils.getMetadata(metadatable, METADATA_KEY, PGM.get()), definition.numericId)) { spawnedEntities -= amount; spawnedEntities = Math.max(0, spawnedEntities); } diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java index 5548a93c36..9475e305cd 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java @@ -10,16 +10,16 @@ @FeatureInfo(name = "spawner") public class SpawnerDefinition implements FeatureDefinition { + public final String numericId; public final Region spawnRegion; public final Region playerRegion; public final int maxEntities; public final Duration minDelay, maxDelay, delay; public final List objects; public final Filter playerFilter; - public final int numericID; public SpawnerDefinition( - int numericID, + String numericId, List objects, Region spawnRegion, Region playerRegion, @@ -28,7 +28,7 @@ public SpawnerDefinition( Duration minDelay, Duration maxDelay, int maxEntities) { - this.numericID = numericID; + this.numericId = numericId; this.spawnRegion = spawnRegion; this.playerRegion = playerRegion; this.maxEntities = maxEntities; diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index ed0fa23db5..7197f3bb14 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -104,8 +104,8 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) potionName = XMLUtils.parseNumber(potionEl.getAttribute("damage"), Integer.class, 0); } else { for (PotionEffect potionEffect : potionChildren) { - // PotionType lists "true" potions, PotionEffectType lists all possible status effects - // (ie wither) + // PotionType lists "true" potions, PotionEffectType "potionEffect.getType()" lists + // all possible status effects (ie wither, blindness, etc) // Use the first listed PotionType for potion color if (PotionType.getByEffect(potionEffect.getType()) != null) { potionName = PotionType.getByEffect(potionEffect.getType()).getDamageValue(); @@ -119,7 +119,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) SpawnerDefinition spawnerDefinition = new SpawnerDefinition( - numericId.getAndIncrement(), + "spawner-" + numericId.getAndIncrement(), objects, spawnRegion, playerRegion, diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 3c73950fe3..f1164e0443 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -17,10 +17,10 @@ public class SpawnablePotion implements Spawnable { private final ItemStack potionItem; private final String spawnerId; - public SpawnablePotion(List potion, int potionName, String spawnerId) { + public SpawnablePotion(List potion, int damageValue, String spawnerId) { this.spawnerId = spawnerId; // Potion "name" determines potion color - ItemStack potionItem = new ItemStack(new Potion(potionName).splash().toItemStack(1)); + ItemStack potionItem = new Potion(damageValue).splash().toItemStack(1); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { potionMeta.addCustomEffect(effect, false); From d610cbc61a0024f41182b723b5a01211f1d12eb3 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 9 May 2022 02:11:03 -0600 Subject: [PATCH 17/23] Rename variable Signed-off-by: Patrick --- core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 7197f3bb14..fb52c4cf12 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -99,22 +99,22 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) if (potionChildren.isEmpty()) { throw new InvalidXMLException("Expected child effects, but found none", spawnerEl); } - int potionName = 0; + int damageValue = 0; if (potionEl.getAttribute("damage") != null) { - potionName = XMLUtils.parseNumber(potionEl.getAttribute("damage"), Integer.class, 0); + damageValue = XMLUtils.parseNumber(potionEl.getAttribute("damage"), Integer.class, 0); } else { for (PotionEffect potionEffect : potionChildren) { // PotionType lists "true" potions, PotionEffectType "potionEffect.getType()" lists // all possible status effects (ie wither, blindness, etc) // Use the first listed PotionType for potion color if (PotionType.getByEffect(potionEffect.getType()) != null) { - potionName = PotionType.getByEffect(potionEffect.getType()).getDamageValue(); + damageValue = PotionType.getByEffect(potionEffect.getType()).getDamageValue(); break; } } } objects.add( - new SpawnablePotion(potionChildren, potionName, "spawner-" + numericId.get())); + new SpawnablePotion(potionChildren, damageValue, "spawner-" + numericId.get())); } SpawnerDefinition spawnerDefinition = From cdf5fa9ee9e89e80f42eff81cf120b7ac9b7029b Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 9 May 2022 02:21:51 -0600 Subject: [PATCH 18/23] Use NMSHacks to create EntityPotion Signed-off-by: Patrick --- .../tc/oc/pgm/spawner/objects/SpawnablePotion.java | 13 ++++++------- util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java | 11 +++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index f1164e0443..897cf0ba5d 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -2,16 +2,14 @@ import java.util.List; import org.bukkit.Location; -import org.bukkit.entity.ThrownPotion; +import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; -import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.match.Match; import tc.oc.pgm.spawner.Spawnable; -import tc.oc.pgm.spawner.Spawner; +import tc.oc.pgm.util.nms.NMSHacks; public class SpawnablePotion implements Spawnable { private final ItemStack potionItem; @@ -23,6 +21,7 @@ public SpawnablePotion(List potion, int damageValue, String spawne ItemStack potionItem = new Potion(damageValue).splash().toItemStack(1); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { + // overwrite = false potionMeta.addCustomEffect(effect, false); } potionItem.setItemMeta(potionMeta); @@ -31,9 +30,9 @@ public SpawnablePotion(List potion, int damageValue, String spawne @Override public void spawn(Location location, Match match) { - ThrownPotion thrownPotion = location.getWorld().spawn(location, ThrownPotion.class); - thrownPotion.setItem(potionItem.clone()); - thrownPotion.setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), spawnerId)); + NMSHacks.EntityPotion entityPotion = new NMSHacks.EntityPotion(location, potionItem); + // TODO set metadata when necessary + ((CraftWorld) location.getWorld()).getHandle().addEntity(entityPotion); } @Override diff --git a/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java b/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java index a39064a648..7d2c4f4fd8 100644 --- a/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java +++ b/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java @@ -1327,6 +1327,17 @@ protected Packet spawnPacket() { public void wear(Player viewer, int slot, ItemStack item) {} } + class EntityPotion extends net.minecraft.server.v1_8_R3.EntityPotion { + public EntityPotion(Location location, ItemStack potionItem) { + super( + ((CraftWorld) location.getWorld()).getHandle(), + location.getX(), + location.getY(), + location.getZ(), + CraftItemStack.asNMSCopy(potionItem)); + } + } + static void setFireworksExpectedLifespan(Firework firework, int ticks) { ((CraftFirework) firework).getHandle().expectedLifespan = ticks; } From f13edf6300b8d1f168d1880d4f625daccd428572 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 10 May 2022 18:44:43 -0600 Subject: [PATCH 19/23] Use NMSHacks properly Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java | 3 +-- util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 897cf0ba5d..60db148d4e 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -2,7 +2,6 @@ import java.util.List; import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.Potion; @@ -32,7 +31,7 @@ public SpawnablePotion(List potion, int damageValue, String spawne public void spawn(Location location, Match match) { NMSHacks.EntityPotion entityPotion = new NMSHacks.EntityPotion(location, potionItem); // TODO set metadata when necessary - ((CraftWorld) location.getWorld()).getHandle().addEntity(entityPotion); + entityPotion.spawn(); } @Override diff --git a/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java b/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java index 7d2c4f4fd8..f964c5c09a 100644 --- a/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java +++ b/util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java @@ -1336,6 +1336,10 @@ public EntityPotion(Location location, ItemStack potionItem) { location.getZ(), CraftItemStack.asNMSCopy(potionItem)); } + + public void spawn() { + world.addEntity(this); + } } static void setFireworksExpectedLifespan(Firework firework, int ticks) { From 04de451c95675a2801a5b13d3094f83a8cb66841 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 10 May 2022 19:38:21 -0600 Subject: [PATCH 20/23] Rework spawner IDs Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/Spawner.java | 42 ++++--------------- .../tc/oc/pgm/spawner/SpawnerDefinition.java | 10 ++--- .../java/tc/oc/pgm/spawner/SpawnerModule.java | 14 ++++--- 3 files changed, 22 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/Spawner.java b/core/src/main/java/tc/oc/pgm/spawner/Spawner.java index d8f4c99fe4..9935e219e5 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/Spawner.java +++ b/core/src/main/java/tc/oc/pgm/spawner/Spawner.java @@ -81,47 +81,23 @@ private boolean canSpawn() { @EventHandler(priority = EventPriority.HIGHEST) public void onItemMerge(ItemMergeEvent event) { - boolean entityTracked = false; - boolean targetTracked = false; - if (event.getEntity().hasMetadata(METADATA_KEY)) { - entityTracked = true; - } - if (event.getTarget().hasMetadata(METADATA_KEY)) { - targetTracked = true; - } - - // Do nothing if neither item is from a PGM Spawner - if (!entityTracked && !targetTracked) { - return; - } - - // Cancel the merge if only 1 of the items is from a PGM Spawner - if ((entityTracked && !targetTracked) || (!entityTracked && targetTracked)) { - event.setCancelled(true); - return; - } - - String entitySpawnerId = ""; - String targetSpawnerId = ""; - if (event.getEntity().hasMetadata(METADATA_KEY)) { - entitySpawnerId = + boolean entityTracked = event.getEntity().hasMetadata(METADATA_KEY); + boolean targetTracked = event.getTarget().hasMetadata(METADATA_KEY); + if (!entityTracked && !targetTracked) return; // None affected + if (entityTracked && targetTracked) { + String entitySpawnerId = MetadataUtils.getMetadata(event.getEntity(), METADATA_KEY, PGM.get()).toString(); - } - if (event.getTarget().hasMetadata(METADATA_KEY)) { - targetSpawnerId = + String targetSpawnerId = MetadataUtils.getMetadata(event.getTarget(), METADATA_KEY, PGM.get()).toString(); + if (entitySpawnerId.equals(targetSpawnerId)) return; // Same spawner, allow merge } - // Cancel the merge if the items are from different PGM spawners - if (!entitySpawnerId.equals(targetSpawnerId)) { - event.setCancelled(true); - return; - } + event.setCancelled(true); } private void handleEntityRemoveEvent(Metadatable metadatable, int amount) { if (metadatable.hasMetadata(METADATA_KEY)) { if (Objects.equals( - MetadataUtils.getMetadata(metadatable, METADATA_KEY, PGM.get()), definition.numericId)) { + MetadataUtils.getMetadata(metadatable, METADATA_KEY, PGM.get()), definition.getId())) { spawnedEntities -= amount; spawnedEntities = Math.max(0, spawnedEntities); } diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java index 9475e305cd..65408e48bb 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java @@ -2,15 +2,13 @@ import java.time.Duration; import java.util.List; -import tc.oc.pgm.api.feature.FeatureDefinition; import tc.oc.pgm.api.feature.FeatureInfo; import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.region.Region; +import tc.oc.pgm.features.SelfIdentifyingFeatureDefinition; @FeatureInfo(name = "spawner") -public class SpawnerDefinition implements FeatureDefinition { - - public final String numericId; +public class SpawnerDefinition extends SelfIdentifyingFeatureDefinition { public final Region spawnRegion; public final Region playerRegion; public final int maxEntities; @@ -19,7 +17,7 @@ public class SpawnerDefinition implements FeatureDefinition { public final Filter playerFilter; public SpawnerDefinition( - String numericId, + String id, List objects, Region spawnRegion, Region playerRegion, @@ -28,7 +26,7 @@ public SpawnerDefinition( Duration minDelay, Duration maxDelay, int maxEntities) { - this.numericId = numericId; + super(id); this.spawnRegion = spawnRegion; this.playerRegion = playerRegion; this.maxEntities = maxEntities; diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index fb52c4cf12..39ac97dd41 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -58,12 +58,17 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) for (Element spawnerEl : XMLUtils.flattenElements(doc.getRootElement(), "spawners", "spawner")) { + String id = spawnerEl.getAttributeValue("id"); Region spawnRegion = regionParser.parseRequiredRegionProperty(spawnerEl, "spawn-region"); Region playerRegion = regionParser.parseRequiredRegionProperty(spawnerEl, "player-region"); Attribute delayAttr = spawnerEl.getAttribute("delay"); Attribute minDelayAttr = spawnerEl.getAttribute("min-delay"); Attribute maxDelayAttr = spawnerEl.getAttribute("max-delay"); + if (spawnerEl.getAttributeValue("id") == null) { + id = "spawner-" + numericId.getAndIncrement(); + } + if ((minDelayAttr != null || maxDelayAttr != null) && delayAttr != null) { throw new InvalidXMLException( "Attribute 'minDelay' and 'maxDelay' cannot be combined with 'delay'", spawnerEl); @@ -86,12 +91,12 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) List objects = new ArrayList<>(); for (Element itemEl : XMLUtils.getChildren(spawnerEl, "item")) { ItemStack stack = kitParser.parseItem(itemEl, false); - SpawnableItem item = new SpawnableItem(stack, "spawner-" + numericId.get()); + SpawnableItem item = new SpawnableItem(stack, id); objects.add(item); } - ImmutableList.Builder chBuilder = ImmutableList.builder(); for (Element potionEl : XMLUtils.getChildren(spawnerEl, "potion")) { + ImmutableList.Builder chBuilder = ImmutableList.builder(); for (Element potionChild : potionEl.getChildren("effect")) { chBuilder.add(XMLUtils.parsePotionEffect(new InheritingElement(potionChild))); } @@ -113,13 +118,12 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) } } } - objects.add( - new SpawnablePotion(potionChildren, damageValue, "spawner-" + numericId.get())); + objects.add(new SpawnablePotion(potionChildren, damageValue, id)); } SpawnerDefinition spawnerDefinition = new SpawnerDefinition( - "spawner-" + numericId.getAndIncrement(), + id, objects, spawnRegion, playerRegion, From 3e5624eceba73311feb43690a25df642e6764caf Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 12 May 2022 02:35:16 -0600 Subject: [PATCH 21/23] Simplify EntityPotion spawning Signed-off-by: Patrick --- .../main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index 60db148d4e..a91ef7b056 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -29,9 +29,8 @@ public SpawnablePotion(List potion, int damageValue, String spawne @Override public void spawn(Location location, Match match) { - NMSHacks.EntityPotion entityPotion = new NMSHacks.EntityPotion(location, potionItem); + new NMSHacks.EntityPotion(location, potionItem).spawn(); // TODO set metadata when necessary - entityPotion.spawn(); } @Override From 682b7f3a71986d9c6c9cb12280b810ad8005a5fb Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 13 May 2022 01:38:33 -0600 Subject: [PATCH 22/23] Do pablo's suggestions Signed-off-by: Patrick --- .../tc/oc/pgm/spawner/SpawnerDefinition.java | 14 ++++++++++++++ .../java/tc/oc/pgm/spawner/SpawnerModule.java | 18 ++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java index 65408e48bb..dbe811072f 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerDefinition.java @@ -2,6 +2,8 @@ import java.time.Duration; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.Nullable; import tc.oc.pgm.api.feature.FeatureInfo; import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.region.Region; @@ -36,4 +38,16 @@ public SpawnerDefinition( this.objects = objects; this.playerFilter = playerFilter; } + + @Override + protected String getDefaultId() { + return super.makeDefaultId(); + } + + public static String makeDefaultId(@Nullable String name, AtomicInteger serial) { + return "--" + + makeTypeName(SpawnerDefinition.class) + + "-" + + (name != null ? makeId(name) : serial.getAndIncrement()); + } } diff --git a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java index 39ac97dd41..52366171db 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java +++ b/core/src/main/java/tc/oc/pgm/spawner/SpawnerModule.java @@ -54,7 +54,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) RegionParser regionParser = factory.getRegions(); KitParser kitParser = factory.getKits(); FilterParser filterParser = factory.getFilters(); - AtomicInteger numericId = new AtomicInteger(0); + AtomicInteger postIdSerial = new AtomicInteger(1); for (Element spawnerEl : XMLUtils.flattenElements(doc.getRootElement(), "spawners", "spawner")) { @@ -65,9 +65,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) Attribute minDelayAttr = spawnerEl.getAttribute("min-delay"); Attribute maxDelayAttr = spawnerEl.getAttribute("max-delay"); - if (spawnerEl.getAttributeValue("id") == null) { - id = "spawner-" + numericId.getAndIncrement(); - } + if (id == null) id = SpawnerDefinition.makeDefaultId("spawner", postIdSerial); if ((minDelayAttr != null || maxDelayAttr != null) && delayAttr != null) { throw new InvalidXMLException( @@ -96,19 +94,19 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) } for (Element potionEl : XMLUtils.getChildren(spawnerEl, "potion")) { - ImmutableList.Builder chBuilder = ImmutableList.builder(); + ImmutableList.Builder effectsBuilder = ImmutableList.builder(); for (Element potionChild : potionEl.getChildren("effect")) { - chBuilder.add(XMLUtils.parsePotionEffect(new InheritingElement(potionChild))); + effectsBuilder.add(XMLUtils.parsePotionEffect(new InheritingElement(potionChild))); } - ImmutableList potionChildren = chBuilder.build(); - if (potionChildren.isEmpty()) { + ImmutableList effects = effectsBuilder.build(); + if (effects.isEmpty()) { throw new InvalidXMLException("Expected child effects, but found none", spawnerEl); } int damageValue = 0; if (potionEl.getAttribute("damage") != null) { damageValue = XMLUtils.parseNumber(potionEl.getAttribute("damage"), Integer.class, 0); } else { - for (PotionEffect potionEffect : potionChildren) { + for (PotionEffect potionEffect : effects) { // PotionType lists "true" potions, PotionEffectType "potionEffect.getType()" lists // all possible status effects (ie wither, blindness, etc) // Use the first listed PotionType for potion color @@ -118,7 +116,7 @@ public SpawnerModule parse(MapFactory factory, Logger logger, Document doc) } } } - objects.add(new SpawnablePotion(potionChildren, damageValue, id)); + objects.add(new SpawnablePotion(effects, damageValue, id)); } SpawnerDefinition spawnerDefinition = From 1de5d6f188155e376bf8bb952f9cf202455395bb Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 13 May 2022 02:25:43 -0600 Subject: [PATCH 23/23] Set metadata to EntityPotion Signed-off-by: Patrick --- .../tc/oc/pgm/spawner/objects/SpawnablePotion.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java index a91ef7b056..f59e5753bf 100644 --- a/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java +++ b/core/src/main/java/tc/oc/pgm/spawner/objects/SpawnablePotion.java @@ -4,10 +4,13 @@ import org.bukkit.Location; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; +import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.match.Match; import tc.oc.pgm.spawner.Spawnable; +import tc.oc.pgm.spawner.Spawner; import tc.oc.pgm.util.nms.NMSHacks; public class SpawnablePotion implements Spawnable { @@ -20,7 +23,6 @@ public SpawnablePotion(List potion, int damageValue, String spawne ItemStack potionItem = new Potion(damageValue).splash().toItemStack(1); PotionMeta potionMeta = (PotionMeta) potionItem.getItemMeta(); for (PotionEffect effect : potion) { - // overwrite = false potionMeta.addCustomEffect(effect, false); } potionItem.setItemMeta(potionMeta); @@ -29,8 +31,11 @@ public SpawnablePotion(List potion, int damageValue, String spawne @Override public void spawn(Location location, Match match) { - new NMSHacks.EntityPotion(location, potionItem).spawn(); - // TODO set metadata when necessary + NMSHacks.EntityPotion entityPotion = new NMSHacks.EntityPotion(location, potionItem); + entityPotion + .getBukkitEntity() + .setMetadata(Spawner.METADATA_KEY, new FixedMetadataValue(PGM.get(), spawnerId)); + entityPotion.spawn(); } @Override