Use Things API for class armor.

Swaps out the hard dependecy on ItemStack for class armor, such that the ThingManager - and thus every ThingParser registered within - has a chance at providing "armor" for a class.

To distinguish armor from items, ArenaMasterImpl prepends "armor:" or slot-specific prefixes to these values before passing them to the ThingManager. This makes it possible to distinguish between items and armor on the thing parser end if applicable - it doesn't make sense for money and command parsers, but they will just end up skipping these values.

The ItemStackThingParser is given a bit of an overhaul. It now depends internally on ItemStack parsers, which can be registered via the ThingManager. This allows custom plugins to hook into the ItemStack-specific part of the ItemStackThingParser to avoid having to duplicate code. Plugins that just want to provide ItemStacks can use this parser approach, and plugins that want to provide more abstract Things can use the Thing parser approach.
This commit is contained in:
Andreas Troelsen
2018-04-24 14:07:42 +02:00
parent 8014420c1b
commit 37de1e66e9
13 changed files with 507 additions and 75 deletions
@@ -0,0 +1,119 @@
package com.garbagemule.MobArena.things;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
public class ItemStackThingParserTest {
private ItemStackThingParser subject;
@Before
public void setup() {
subject = new ItemStackThingParser();
}
@Test
public void emptyStringReturnsNull() {
ItemStackThing result = subject.parse("");
assertThat(result, is(nullValue()));
}
@Test
public void gibberishReturnsNull() {
ItemStackThing result = subject.parse("I can't believe you've done this");
assertThat(result, is(nullValue()));
}
@Test
public void fiveDirt() {
ItemStackThing result = subject.parse("dirt:5");
// ItemStack's equals() method does naughty things, so verify manually
ItemStack stack = result.getItemStack();
assertThat(stack.getType(), equalTo(Material.DIRT));
assertThat(stack.getAmount(), equalTo(5));
}
@Test
public void bareLederhosenReturnsGenericItemStackThing() {
ItemStackThing result = subject.parse("leather_leggings");
assertThat(result.getClass(), equalTo(ItemStackThing.class));
}
@Test
public void prefixedIronChestplateReturnsLeggingsThing() {
ItemStackThing result = subject.parse("leggings:leather_leggings");
assertThat(result.getClass(), equalTo(LeggingsThing.class));
}
@Test
public void genericArmorPrefixGuessesChestplateThing() {
ItemStackThing result = subject.parse("armor:iron_chestplate");
assertThat(result.getClass(), equalTo(ChestplateThing.class));
}
@Test
public void returnsNonNullCustomParserResult() {
String input = "something:soft";
ItemStack stack = new ItemStack(Material.SPONGE, 3);
ItemStackParser parser = mock(ItemStackParser.class);
when(parser.parse(input)).thenReturn(stack);
subject.register(parser);
ItemStackThing result = subject.parse(input);
assertThat(result.getItemStack(), equalTo(stack));
}
@Test
public void customParsersInvokedInOrder() {
String input = "unicorns and rainbows";
ItemStackParser first = mock(ItemStackParser.class);
ItemStackParser second = mock(ItemStackParser.class);
subject.register(first);
subject.register(second);
subject.parse(input);
InOrder order = inOrder(first, second);
order.verify(first).parse(input);
order.verify(second).parse(input);
}
@Test
public void customParsersInvokedUntilNonNullResult() {
String input = "unicorns and rainbows";
ItemStackParser first = mock(ItemStackParser.class);
ItemStackParser second = mock(ItemStackParser.class);
ItemStackParser third = mock(ItemStackParser.class);
ItemStack stack = new ItemStack(Material.GLOWSTONE, 9);
when(second.parse(input)).thenReturn(stack);
subject.register(first);
subject.register(second);
subject.register(third);
subject.parse(input);
verify(first).parse(input);
verifyZeroInteractions(third);
}
}