Don't depend directly on Vault in MoneyThingParser.

It turns out that the method reference on MobArena#getEconomy() in ThingManager is a tight enough dependency on Vault's Economy interface that it results in a NoClassDefFoundError if Vault isn't present.

By resorting to a more "naive" approach of resolving the Economy instance from the main plugin class on every parse call in MoneyThingParser, the NoClassDefFoundError is avoided along with the load/enable ordering issue that was fixed with the lazy-fetching in commit 2fcb20b2ae.

This reverts 2fcb20b2ae and partly 4c34a183c7.

Fixes #463
This commit is contained in:
Andreas Troelsen 2018-05-12 12:11:12 +02:00
parent 8dbee5d4b5
commit ce392bddc3
3 changed files with 12 additions and 9 deletions

View File

@ -1,17 +1,15 @@
package com.garbagemule.MobArena.things;
import net.milkbowl.vault.economy.Economy;
import java.util.function.Supplier;
import com.garbagemule.MobArena.MobArena;
class MoneyThingParser implements ThingParser {
private static final String PREFIX_LONG = "money:";
private static final String PREFIX_SHORT = "$";
private Supplier<Economy> economy;
private MobArena plugin;
MoneyThingParser(Supplier<Economy> economy) {
this.economy = economy;
MoneyThingParser(MobArena plugin) {
this.plugin = plugin;
}
@Override
@ -20,7 +18,7 @@ class MoneyThingParser implements ThingParser {
if (money == null) {
return null;
}
return new MoneyThing(economy.get(), Double.parseDouble(money));
return new MoneyThing(plugin.getEconomy(), Double.parseDouble(money));
}
private String trimPrefix(String s) {

View File

@ -12,7 +12,7 @@ public class ThingManager implements ThingParser {
public ThingManager(MobArena plugin, ItemStackThingParser parser) {
parsers = new ArrayList<>();
parsers.add(new CommandThingParser());
parsers.add(new MoneyThingParser(plugin::getEconomy));
parsers.add(new MoneyThingParser(plugin));
items = parser;
}

View File

@ -5,7 +5,9 @@ import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.garbagemule.MobArena.MobArena;
import net.milkbowl.vault.economy.Economy;
import org.junit.Before;
import org.junit.Rule;
@ -21,8 +23,11 @@ public class MoneyThingParserTest {
@Before
public void setup() {
MobArena plugin = mock(MobArena.class);
Economy economy = mock(Economy.class);
subject = new MoneyThingParser(() -> economy);
when(plugin.getEconomy()).thenReturn(economy);
subject = new MoneyThingParser(plugin);
}
@Test