pdglobal
4/8/2018 - 11:40 PM

gistfile1.txt

package mobmoney;

import mobmoney.Mobmoney;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.entity.damage.source.DamageSources;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.game.state.GameStartingServerEvent;
import org.spongepowered.api.event.item.inventory.ChangeInventoryEvent;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.service.economy.transaction.TransactionResult;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import com.flowpowered.math.vector.Vector3d;

@Plugin(id = Mobmoney.PLUGIN_ID, name = Mobmoney.PLUGIN_NAME, version = Mobmoney.PLUGIN_VERSION)
public class Mobmoney {

	public static final String PLUGIN_ID = "mobmoney";
	public static final String PLUGIN_NAME = "Mobmoney";
	public static final String PLUGIN_VERSION = "SNAPSHOT-01";
	
	Map<Location<World>, Double> values = new HashMap<>();

	@Listener
	public void onEnable(GameStartingServerEvent event) {
		// your code
	}
	
	@Listener
	public void onPickup(ChangeInventoryEvent.Pickup.Pre event, @First Player player) {
	    Item item = event.getTargetEntity();
	    if (item.getItemType().equals(ItemTypes.GOLD_NUGGET)) {
	    List<Entry<Location<World>, Double>> list = this.values.entrySet().stream()
	    .filter(e -> e.getKey().getExtent().getUniqueId().equals(item.getWorld().getUniqueId()))
	    .filter(e -> e.getKey().getPosition().distance(item.getLocation().getPosition()) <= 2).collect(Collectors.toList());
	    list.forEach(e -> {
	    	try {
				depositToPlayer(player.getUniqueId(), new BigDecimal(e.getValue()), event.getCause());
				item.damage(1, DamageSources.VOID);
				player.sendMessage(Text.builder(e.getValue()+" credits added to your balance.").color(TextColors.BLUE).build());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
	    });
	    }
	}

	@Listener
	public void onKill(DamageEntityEvent event) {
		if (!event.willCauseDeath()) {
			return;
		}
		ItemType type = ItemTypes.GOLD_NUGGET;
		ItemStackSnapshot stack = type.getTemplate();

		Entity death = event.getTargetEntity();
		Vector3d deathPos = death.getLocation().getPosition();
		Item item = (Item) death.getWorld().createEntity(EntityTypes.ITEM, deathPos);
		item.offer(Keys.REPRESENTED_ITEM, stack);
		death.getWorld().spawnEntity(item);
		this.values.put(item.getLocation(), Math.ceil(death.get(Keys.MAX_HEALTH).get() / 2));
	}

	public static TransactionResult depositToPlayer(UUID uuid, BigDecimal amount, Cause cause) throws IOException {
		return getEcoAccount(uuid).deposit(getService().getDefaultCurrency(), amount, cause);
	}
	
	private static EconomyService getService() throws IOException{
        Optional<EconomyService> opService = Sponge.getServiceManager().provide(EconomyService.class);
		if(!opService.isPresent()) {
			throw new IOException("No Eco plugin detected");
		}
		EconomyService service = opService.get();
		return service;
	}
	
	private static UniqueAccount getEcoAccount(UUID uuid) throws IOException {
		EconomyService service = getService();
		Optional<UniqueAccount> opAccount = service.getOrCreateAccount(uuid);
		if(!opAccount.isPresent()) {
			throw new IOException("Could not find/create account");
		}
		return opAccount.get();
	}
}