From a2e235e77a806760b4e8e9dd532add94cf5393fd Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Mon, 11 Jun 2018 23:46:01 +0200 Subject: [PATCH] Add support for potion effects in the Things API. This commit introduces the PotionEffectThing and associated parser. The parser simply delegates to the existing PotionEffectParser utility class the same way the ItemStackThing parser does it. To distinguish potion effects from other things, the parser requires a prefix of "effect:". As with the initial Thing API commit, this commit also adds an overload to the existing parser class to avoid spamming the console with meaningless warnings. --- .../com/garbagemule/MobArena/ArenaImpl.java | 3 ++ .../MobArena/things/PotionEffectThing.java | 28 +++++++++++++++++++ .../things/PotionEffectThingParser.java | 28 +++++++++++++++++++ .../MobArena/things/ThingManager.java | 1 + .../MobArena/util/PotionEffectParser.java | 8 +++++- 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/garbagemule/MobArena/things/PotionEffectThing.java create mode 100644 src/main/java/com/garbagemule/MobArena/things/PotionEffectThingParser.java diff --git a/src/main/java/com/garbagemule/MobArena/ArenaImpl.java b/src/main/java/com/garbagemule/MobArena/ArenaImpl.java index 006e274..853a067 100644 --- a/src/main/java/com/garbagemule/MobArena/ArenaImpl.java +++ b/src/main/java/com/garbagemule/MobArena/ArenaImpl.java @@ -1162,6 +1162,9 @@ public class ArenaImpl implements Arena } InventoryManager.clearInventory(p); + p.getActivePotionEffects().stream() + .map(PotionEffect::getType) + .forEach(p::removePotionEffect); arenaPlayer.setArenaClass(arenaClass); arenaClass.grantItems(p); diff --git a/src/main/java/com/garbagemule/MobArena/things/PotionEffectThing.java b/src/main/java/com/garbagemule/MobArena/things/PotionEffectThing.java new file mode 100644 index 0000000..23ea968 --- /dev/null +++ b/src/main/java/com/garbagemule/MobArena/things/PotionEffectThing.java @@ -0,0 +1,28 @@ +package com.garbagemule.MobArena.things; + +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; + +class PotionEffectThing implements Thing { + private final PotionEffect effect; + + PotionEffectThing(PotionEffect effect) { + this.effect = effect; + } + + @Override + public boolean giveTo(Player player) { + return player.addPotionEffect(effect, true); + } + + @Override + public boolean takeFrom(Player player) { + player.removePotionEffect(effect.getType()); + return true; + } + + @Override + public boolean heldBy(Player player) { + return player.hasPotionEffect(effect.getType()); + } +} diff --git a/src/main/java/com/garbagemule/MobArena/things/PotionEffectThingParser.java b/src/main/java/com/garbagemule/MobArena/things/PotionEffectThingParser.java new file mode 100644 index 0000000..2d77396 --- /dev/null +++ b/src/main/java/com/garbagemule/MobArena/things/PotionEffectThingParser.java @@ -0,0 +1,28 @@ +package com.garbagemule.MobArena.things; + +import com.garbagemule.MobArena.util.PotionEffectParser; +import org.bukkit.potion.PotionEffect; + +class PotionEffectThingParser implements ThingParser { + private static final String PREFIX = "effect:"; + + @Override + public PotionEffectThing parse(String s) { + String value = trimPrefix(s); + if (value == null) { + return null; + } + PotionEffect effect = PotionEffectParser.parsePotionEffect(value, false); + if (effect == null) { + return null; + } + return new PotionEffectThing(effect); + } + + private String trimPrefix(String s) { + if (s.startsWith(PREFIX)) { + return s.substring(PREFIX.length()); + } + return null; + } +} diff --git a/src/main/java/com/garbagemule/MobArena/things/ThingManager.java b/src/main/java/com/garbagemule/MobArena/things/ThingManager.java index 4dc72df..71c4428 100644 --- a/src/main/java/com/garbagemule/MobArena/things/ThingManager.java +++ b/src/main/java/com/garbagemule/MobArena/things/ThingManager.java @@ -13,6 +13,7 @@ public class ThingManager implements ThingParser { parsers = new ArrayList<>(); parsers.add(new CommandThingParser()); parsers.add(new MoneyThingParser(plugin)); + parsers.add(new PotionEffectThingParser()); items = parser; } diff --git a/src/main/java/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/main/java/com/garbagemule/MobArena/util/PotionEffectParser.java index 666d823..5f694c2 100644 --- a/src/main/java/com/garbagemule/MobArena/util/PotionEffectParser.java +++ b/src/main/java/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -29,6 +29,10 @@ public class PotionEffectParser } public static PotionEffect parsePotionEffect(String p) { + return parsePotionEffect(p, true); + } + + public static PotionEffect parsePotionEffect(String p, boolean logFailure) { if (p == null || p.isEmpty()) return null; @@ -48,7 +52,9 @@ public class PotionEffectParser } if (result == null) { - Bukkit.getLogger().warning("[MobArena] Failed to parse potion effect: " + p); + if (logFailure) { + Bukkit.getLogger().warning("[MobArena] Failed to parse potion effect: " + p); + } return null; }