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.
This commit is contained in:
Andreas Troelsen 2018-06-11 23:46:01 +02:00
parent 9594bd6126
commit a2e235e77a
5 changed files with 67 additions and 1 deletions

View File

@ -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);

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}