Add support for picking nothing.

This commit introduces the strange concept of a singleton ThingPicker
that only ever picks `null`. The purpose of this picker is to allow for
a type of "loot table" experience similar to that found in other games.
An example would be a piece of equipment that only has a 50 % chance of
dropping. With the current state of MobArena, it would be necessary to
something conjure up a CommandThing or something to emulate nothingness,
but now there is native support for it with the `nothing` keyword.

The nullability of rewards also has the side effect that we got to clean
up the MASpawnThread `addReward` method a bit.

Closes #638
This commit is contained in:
Andreas Troelsen
2020-08-22 17:36:30 +02:00
parent 994ebaff81
commit d30bd96a2a
5 changed files with 39 additions and 7 deletions
@@ -392,13 +392,8 @@ public class MASpawnThread implements Runnable
private void addReward(ThingPicker picker) {
for (Player p : arena.getPlayersInArena()) {
Thing reward = picker.pick();
rewardManager.addReward(p, reward);
if (reward == null) {
arena.getMessenger().tell(p, "ERROR! Problem with rewards. Notify server host!");
plugin.getLogger().warning("Could not add null reward. Please check the config-file!");
}
else {
if (reward != null) {
rewardManager.addReward(p, reward);
arena.getMessenger().tell(p, Msg.WAVE_REWARD, reward.toString());
}
}
@@ -16,6 +16,7 @@ import com.garbagemule.MobArena.metrics.VaultChart;
import com.garbagemule.MobArena.signs.ArenaSign;
import com.garbagemule.MobArena.signs.SignBootstrap;
import com.garbagemule.MobArena.signs.SignListeners;
import com.garbagemule.MobArena.things.NothingPickerParser;
import com.garbagemule.MobArena.things.RandomThingPickerParser;
import com.garbagemule.MobArena.things.ThingGroupPickerParser;
import com.garbagemule.MobArena.things.ThingManager;
@@ -71,6 +72,7 @@ public class MobArena extends JavaPlugin
pickman = new ThingPickerManager(thingman);
pickman.register(new ThingGroupPickerParser(pickman));
pickman.register(new RandomThingPickerParser(pickman, random));
pickman.register(new NothingPickerParser());
}
public void onEnable() {
@@ -0,0 +1,21 @@
package com.garbagemule.MobArena.things;
class NothingPicker implements ThingPicker {
private static final NothingPicker instance = new NothingPicker();
@Override
public Thing pick() {
return null;
}
@Override
public String toString() {
return "nothing";
}
public static NothingPicker getInstance() {
return instance;
}
}
@@ -0,0 +1,13 @@
package com.garbagemule.MobArena.things;
public class NothingPickerParser implements ThingPickerParser {
@Override
public ThingPicker parse(String s) {
if (!s.equalsIgnoreCase("nothing")) {
return null;
}
return NothingPicker.getInstance();
}
}