mirror of
https://github.com/garbagemule/MobArena.git
synced 2024-11-23 02:55:46 +01:00
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:
parent
9594bd6126
commit
a2e235e77a
@ -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);
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user