Fore the sake of inception add in experimental money support in mcore through Vault.

This commit is contained in:
Olof Larsson 2013-04-19 18:25:47 +02:00
parent 2b7b45b7d3
commit e8e8de834b
17 changed files with 556 additions and 46 deletions

View File

@ -20,6 +20,7 @@ import com.massivecraft.mcore.adapter.PlayerInventoryAdapter;
import com.massivecraft.mcore.adapter.UUIDAdapter; import com.massivecraft.mcore.adapter.UUIDAdapter;
import com.massivecraft.mcore.cmd.CmdMcore; import com.massivecraft.mcore.cmd.CmdMcore;
import com.massivecraft.mcore.integration.protocollib.ProtocolLibFeatures; import com.massivecraft.mcore.integration.protocollib.ProtocolLibFeatures;
import com.massivecraft.mcore.integration.vault.VaultFeatures;
import com.massivecraft.mcore.mixin.ScheduledTeleportEngine; import com.massivecraft.mcore.mixin.ScheduledTeleportEngine;
import com.massivecraft.mcore.mixin.SenderIdMixinDefault; import com.massivecraft.mcore.mixin.SenderIdMixinDefault;
import com.massivecraft.mcore.mixin.TeleportMixinCauseEngine; import com.massivecraft.mcore.mixin.TeleportMixinCauseEngine;
@ -28,7 +29,9 @@ import com.massivecraft.mcore.ps.PSAdapter;
import com.massivecraft.mcore.store.Coll; import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.Db; import com.massivecraft.mcore.store.Db;
import com.massivecraft.mcore.store.MStore; import com.massivecraft.mcore.store.MStore;
import com.massivecraft.mcore.usys.Aspect;
import com.massivecraft.mcore.usys.AspectColl; import com.massivecraft.mcore.usys.AspectColl;
import com.massivecraft.mcore.usys.Multiverse;
import com.massivecraft.mcore.usys.MultiverseColl; import com.massivecraft.mcore.usys.MultiverseColl;
import com.massivecraft.mcore.usys.cmd.CmdUsys; import com.massivecraft.mcore.usys.cmd.CmdUsys;
import com.massivecraft.mcore.util.PlayerUtil; import com.massivecraft.mcore.util.PlayerUtil;
@ -90,6 +93,12 @@ public class MCore extends MPlugin
public CmdUsys cmdUsys; public CmdUsys cmdUsys;
public CmdMcore cmdMcore; public CmdMcore cmdMcore;
// Aspects
private Aspect moneyAspect;
public Aspect getMoneyAspect() { return this.moneyAspect; }
public Multiverse getMoneyMultiverse() { return this.getMoneyAspect().getMultiverse(); }
// Runnables
private Runnable collTickTask = new Runnable() private Runnable collTickTask = new Runnable()
{ {
public void run() public void run()
@ -138,6 +147,13 @@ public class MCore extends MPlugin
AspectColl.get().init(); AspectColl.get().init();
MCoreConfColl.get().init(); MCoreConfColl.get().init();
// Init aspects
this.moneyAspect = AspectColl.get().get("mcore_money", true);
this.moneyAspect.register();
this.moneyAspect.setDesc(
"<i>The aspect used for how much money a player has"
);
// Register commands // Register commands
this.cmdUsys = new CmdUsys(); this.cmdUsys = new CmdUsys();
this.cmdUsys.register(this, true); this.cmdUsys.register(this, true);
@ -146,7 +162,10 @@ public class MCore extends MPlugin
this.cmdMcore.register(this, true); this.cmdMcore.register(this, true);
// Integration // Integration
this.integrate(ProtocolLibFeatures.get()); this.integrate(
ProtocolLibFeatures.get(),
VaultFeatures.get()
);
/* /*
test(""); test("");

View File

@ -0,0 +1,32 @@
package com.massivecraft.mcore.integration.vault;
import com.massivecraft.mcore.integration.IntegrationFeaturesAbstract;
import com.massivecraft.mcore.money.MoneyMixinVault;
public class VaultFeatures extends IntegrationFeaturesAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static VaultFeatures i = new VaultFeatures();
public static VaultFeatures get() { return i; }
private VaultFeatures() { super("Vault"); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void activate()
{
MoneyMixinVault.get().activate();
}
@Override
public void deactivate()
{
MoneyMixinVault.get().deactivate();
}
}

View File

@ -0,0 +1,108 @@
package com.massivecraft.mcore.money;
import com.massivecraft.mcore.util.MUtil;
public class Money
{
// -------------------------------------------- //
// MIXIN
// -------------------------------------------- //
private static MoneyMixin mixin = MoneyMixinDefault.get();
public static MoneyMixin mixin() { return mixin; };
public static void mixin(MoneyMixin newMixin) { mixin = newMixin; }
// -------------------------------------------- //
// EXTRACT
// -------------------------------------------- //
public static String universe(Object universe)
{
String ret = MUtil.extract(String.class, "moneyUniverse", universe);
if (ret == null) throw new IllegalArgumentException("extraction of universe from object failed");
return ret;
}
public static String accountId(Object accountId)
{
String ret = MUtil.extract(String.class, "accountId", accountId);
if (ret == null) throw new IllegalArgumentException("extraction of accountId from object failed");
return ret;
}
// -------------------------------------------- //
// ENABLED
// -------------------------------------------- //
public static boolean enabled(Object universe)
{
return mixin.enabled(universe(universe));
}
public static boolean disabled(Object universe)
{
return !enabled(universe);
}
// -------------------------------------------- //
// FORMAT AND NAME
// -------------------------------------------- //
public static String format(Object universe, double amount)
{
return mixin.format(universe(universe), amount);
}
public static String singular(Object universe)
{
return mixin.singular(universe(universe));
}
public static String plural(Object universe)
{
return mixin.plural(universe(universe));
}
// -------------------------------------------- //
// EXISTS AND CREATE
// -------------------------------------------- //
public static boolean exists(Object universe, Object accountId)
{
return mixin.exists(universe(universe), accountId(accountId));
}
public static boolean create(Object universe, Object accountId)
{
return mixin.create(universe(universe), accountId(accountId));
}
// -------------------------------------------- //
// GET AND SET
// -------------------------------------------- //
public static double get(Object universe, Object accountId)
{
return mixin.get(universe(universe), accountId(accountId));
}
public static boolean set(Object universe, Object accountId, double amount)
{
return mixin.set(universe(universe), accountId(accountId), amount);
}
// -------------------------------------------- //
// MODIFY
// -------------------------------------------- //
public static boolean add(Object universe, Object accountId, double amount)
{
return mixin.add(universe(universe), accountId(accountId), amount);
}
public static boolean subtract(Object universe, Object accountId, double amount)
{
return mixin.subtract(universe(universe), accountId(accountId), amount);
}
}

View File

@ -0,0 +1,40 @@
package com.massivecraft.mcore.money;
public interface MoneyMixin
{
// -------------------------------------------- //
// ENABLED
// -------------------------------------------- //
public boolean enabled(String universe);
// -------------------------------------------- //
// FORMAT AND NAME
// -------------------------------------------- //
public String format(String universe, double amount);
public String singular(String universe);
public String plural(String universe);
// -------------------------------------------- //
// EXISTS AND CREATE
// -------------------------------------------- //
public boolean exists(String universe, String accountId);
public boolean create(String universe, String accountId);
// -------------------------------------------- //
// GET AND SET
// -------------------------------------------- //
public double get(String universe, String accountId);
public boolean set(String universe, String accountId, double amount);
// -------------------------------------------- //
// MODIFY
// -------------------------------------------- //
public boolean add(String universe, String accountId, double amount);
public boolean subtract(String universe, String accountId, double amount);
}

View File

@ -0,0 +1,6 @@
package com.massivecraft.mcore.money;
public abstract class MoneyMixinAbstract implements MoneyMixin
{
}

View File

@ -0,0 +1,92 @@
package com.massivecraft.mcore.money;
public class MoneyMixinDefault extends MoneyMixinAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final MoneyMixinDefault i = new MoneyMixinDefault();
public static MoneyMixinDefault get() { return i; }
// -------------------------------------------- //
// ENABLED AND DISABLED
// -------------------------------------------- //
@Override
public boolean enabled(String universe)
{
return false;
}
// -------------------------------------------- //
// FORMAT AND NAME
// -------------------------------------------- //
@Override
public String format(String universe, double amount)
{
return String.valueOf(amount);
}
@Override
public String singular(String universe)
{
return "singular";
}
@Override
public String plural(String universe)
{
return "plural";
}
// -------------------------------------------- //
// EXISTS AND CREATE
// -------------------------------------------- //
@Override
public boolean exists(String universe, String accountId)
{
return false;
}
@Override
public boolean create(String universe, String accountId)
{
return false;
}
// -------------------------------------------- //
// GET AND SET
// -------------------------------------------- //
@Override
public double get(String universe, String accountId)
{
return 0D;
}
@Override
public boolean set(String universe, String accountId, double amount)
{
return false;
}
// -------------------------------------------- //
// MODIFY
// -------------------------------------------- //
@Override
public boolean add(String universe, String accountId, double amount)
{
return false;
}
@Override
public boolean subtract(String universe, String accountId, double amount)
{
return false;
}
}

View File

@ -0,0 +1,130 @@
package com.massivecraft.mcore.money;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import net.milkbowl.vault.economy.Economy;
public class MoneyMixinVault extends MoneyMixinAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final MoneyMixinVault i = new MoneyMixinVault();
public static MoneyMixinVault get() { return i; }
// -------------------------------------------- //
// ACTIVATE & DEACTIVATE
// -------------------------------------------- //
public void activate()
{
RegisteredServiceProvider<Economy> rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
this.economy = rsp.getProvider();
Money.mixin(this);
}
public void deactivate()
{
Money.mixin(MoneyMixinDefault.get());
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Economy economy = null;
// -------------------------------------------- //
// ENABLED AND DISABLED
// -------------------------------------------- //
@Override
public boolean enabled(String universe)
{
return this.economy.isEnabled();
}
// -------------------------------------------- //
// FORMAT AND NAME
// -------------------------------------------- //
@Override
public String format(String universe, double amount)
{
return this.economy.format(amount);
}
@Override
public String singular(String universe)
{
return this.economy.currencyNameSingular();
}
@Override
public String plural(String universe)
{
return this.economy.currencyNamePlural();
}
// -------------------------------------------- //
// EXISTS AND CREATE
// -------------------------------------------- //
@Override
public boolean exists(String universe, String accountId)
{
return this.economy.hasAccount(accountId, universe);
}
@Override
public boolean create(String universe, String accountId)
{
return this.economy.createPlayerAccount(accountId, universe);
}
// -------------------------------------------- //
// GET AND SET
// -------------------------------------------- //
@Override
public double get(String universe, String accountId)
{
return this.economy.getBalance(accountId, universe);
}
@Override
public boolean set(String universe, String accountId, double amount)
{
double current = get(universe, accountId);
return add(universe, accountId, amount - current);
}
// -------------------------------------------- //
// MODIFY
// -------------------------------------------- //
@Override
public boolean add(String universe, String accountId, double amount)
{
if (amount < 0)
{
return subtract(universe, accountId, -amount);
}
return economy.depositPlayer(accountId, universe, amount).transactionSuccess();
}
@Override
public boolean subtract(String universe, String accountId, double amount)
{
if (amount < 0)
{
return add(universe, accountId, -amount);
}
return economy.withdrawPlayer(accountId, universe, amount).transactionSuccess();
}
}

View File

@ -58,9 +58,9 @@ public class Multiverse extends Entity<Multiverse>
// UNIVERSE // UNIVERSE
// -------------------------------------------- // // -------------------------------------------- //
public boolean containsUniverse(String worldName) public boolean containsUniverse(String universe)
{ {
return this.getUniverses().contains(worldName); return this.getUniverses().contains(universe);
} }
public Set<String> newUniverse(String universe) public Set<String> newUniverse(String universe)

View File

@ -49,6 +49,7 @@ import com.massivecraft.mcore.InternalListener;
import com.massivecraft.mcore.MCore; import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.WorldNameSetEngine; import com.massivecraft.mcore.WorldNameSetEngine;
import com.massivecraft.mcore.util.extractor.Extractor; import com.massivecraft.mcore.util.extractor.Extractor;
import com.massivecraft.mcore.util.extractor.ExtractorMoneyUniverse;
import com.massivecraft.mcore.util.extractor.ExtractorPlayer; import com.massivecraft.mcore.util.extractor.ExtractorPlayer;
import com.massivecraft.mcore.util.extractor.ExtractorPlayerName; import com.massivecraft.mcore.util.extractor.ExtractorPlayerName;
import com.massivecraft.mcore.util.extractor.ExtractorSender; import com.massivecraft.mcore.util.extractor.ExtractorSender;
@ -572,5 +573,8 @@ public class MUtil
registerExtractor(World.class, "world", ExtractorWorld.get()); registerExtractor(World.class, "world", ExtractorWorld.get());
registerExtractor(String.class, "worldName", ExtractorWorldName.get()); registerExtractor(String.class, "worldName", ExtractorWorldName.get());
registerExtractor(String.class, "moneyUniverse", ExtractorMoneyUniverse.get());
registerExtractor(String.class, "accountId", ExtractorPlayerName.get());
} }
} }

View File

@ -25,8 +25,11 @@ import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.event.vehicle.VehicleEvent; import org.bukkit.event.vehicle.VehicleEvent;
import org.bukkit.event.vehicle.VehicleExitEvent; import org.bukkit.event.vehicle.VehicleExitEvent;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.SenderEntity; import com.massivecraft.mcore.store.SenderEntity;
import com.massivecraft.mcore.usys.Multiverse;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.SenderUtil;
public class ExtractorLogic public class ExtractorLogic
@ -96,7 +99,6 @@ public class ExtractorLogic
if (o == null) return null; if (o == null) return null;
if (o instanceof String) return (String)o; if (o instanceof String) return (String)o;
if (o instanceof SenderEntity) return ((SenderEntity<?>)o).getId(); if (o instanceof SenderEntity) return ((SenderEntity<?>)o).getId();
if (o instanceof SenderEntity) return ((SenderEntity<?>)o).getId();
CommandSender sender = senderFromObject(o); CommandSender sender = senderFromObject(o);
if (sender == null) return null; if (sender == null) return null;
return SenderUtil.getSenderId(sender); return SenderUtil.getSenderId(sender);
@ -160,4 +162,35 @@ public class ExtractorLogic
if (world == null) return null; if (world == null) return null;
return world.getName(); return world.getName();
} }
// -------------------------------------------- //
// MONEY UNIVERSE
// -------------------------------------------- //
public static String moneyUniverse(String o)
{
Multiverse m = MCore.get().getMoneyMultiverse();
if (m.containsUniverse(o)) return o;
return m.getUniverseForWorldName(o);
}
public static String moneyUniverse(com.massivecraft.mcore.store.Entity<?> o)
{
return o.getUniverse();
}
public static String moneyUniverseFromObject(Object o)
{
if (o instanceof String) return moneyUniverse((String)o);
if (o instanceof com.massivecraft.mcore.store.Entity) return moneyUniverse((com.massivecraft.mcore.store.Entity<?>)o);
String worldName = MUtil.extract(String.class, "worldName", o);
if (worldName != null)
{
return MCore.get().getMoneyMultiverse().getUniverseForWorldName(worldName);
}
return null;
}
} }

View File

@ -0,0 +1,22 @@
package com.massivecraft.mcore.util.extractor;
public class ExtractorMoneyUniverse implements Extractor
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorMoneyUniverse i = new ExtractorMoneyUniverse();
public static ExtractorMoneyUniverse get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override
public Object extract(Object o)
{
return ExtractorLogic.moneyUniverseFromObject(o);
}
}

View File

@ -2,17 +2,21 @@ package com.massivecraft.mcore.util.extractor;
public class ExtractorPlayer implements Extractor public class ExtractorPlayer implements Extractor
{ {
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorPlayer i = new ExtractorPlayer();
public static ExtractorPlayer get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override @Override
public Object extract(Object o) public Object extract(Object o)
{ {
return ExtractorLogic.playerFromObject(o); return ExtractorLogic.playerFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorPlayer i = new ExtractorPlayer();
public static ExtractorPlayer get() { return i; }
} }

View File

@ -2,17 +2,21 @@ package com.massivecraft.mcore.util.extractor;
public class ExtractorPlayerName implements Extractor public class ExtractorPlayerName implements Extractor
{ {
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorPlayerName i = new ExtractorPlayerName();
public static ExtractorPlayerName get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override @Override
public Object extract(Object o) public Object extract(Object o)
{ {
return ExtractorLogic.playerNameFromObject(o); return ExtractorLogic.playerNameFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorPlayerName i = new ExtractorPlayerName();
public static ExtractorPlayerName get() { return i; }
} }

View File

@ -2,17 +2,21 @@ package com.massivecraft.mcore.util.extractor;
public class ExtractorSender implements Extractor public class ExtractorSender implements Extractor
{ {
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorSender i = new ExtractorSender();
public static ExtractorSender get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override @Override
public Object extract(Object o) public Object extract(Object o)
{ {
return ExtractorLogic.senderFromObject(o); return ExtractorLogic.senderFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorSender i = new ExtractorSender();
public static ExtractorSender get() { return i; }
} }

View File

@ -2,17 +2,21 @@ package com.massivecraft.mcore.util.extractor;
public class ExtractorSenderId implements Extractor public class ExtractorSenderId implements Extractor
{ {
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorSenderId i = new ExtractorSenderId();
public static ExtractorSenderId get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override @Override
public Object extract(Object o) public Object extract(Object o)
{ {
return ExtractorLogic.senderIdFromObject(o); return ExtractorLogic.senderIdFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorSenderId i = new ExtractorSenderId();
public static ExtractorSenderId get() { return i; }
} }

View File

@ -2,17 +2,21 @@ package com.massivecraft.mcore.util.extractor;
public class ExtractorWorld implements Extractor public class ExtractorWorld implements Extractor
{ {
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorWorld i = new ExtractorWorld();
public static ExtractorWorld get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override @Override
public Object extract(Object o) public Object extract(Object o)
{ {
return ExtractorLogic.worldFromObject(o); return ExtractorLogic.worldFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorWorld i = new ExtractorWorld();
public static ExtractorWorld get() { return i; }
} }

View File

@ -2,17 +2,21 @@ package com.massivecraft.mcore.util.extractor;
public class ExtractorWorldName implements Extractor public class ExtractorWorldName implements Extractor
{ {
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ExtractorWorldName i = new ExtractorWorldName();
public static ExtractorWorldName get() { return i; }
// -------------------------------------------- //
// OVERRIDE: EXTRACTOR
// -------------------------------------------- //
@Override @Override
public Object extract(Object o) public Object extract(Object o)
{ {
return ExtractorLogic.worldNameFromObject(o); return ExtractorLogic.worldNameFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorWorldName i = new ExtractorWorldName();
public static ExtractorWorldName get() { return i; }
} }