use of org.spongepowered.api.event.Listener in project TotalEconomy by Erigitic.
the class TEJobManager method onPlayerKillEntity.
/**
* Used for the break option in jobs. Will check if the job has the break node and if it does it will check if the
* block that was broken is present in the config of the player's job. If it is, it will grab the job exp reward as
* well as the pay.
*
* @param event DestructEntityEvent.Death
*/
@Listener
public void onPlayerKillEntity(DestructEntityEvent.Death event) {
Optional<EntityDamageSource> optDamageSource = event.getCause().first(EntityDamageSource.class);
if (optDamageSource.isPresent()) {
EntityDamageSource damageSource = optDamageSource.get();
Entity killer = damageSource.getSource();
Entity victim = event.getTargetEntity();
if (!(killer instanceof Player)) {
// If a projectile was shot to kill an entity, this will grab the player who shot it
Optional<UUID> damageCreator = damageSource.getSource().getCreator();
if (damageCreator.isPresent())
killer = Sponge.getServer().getPlayer(damageCreator.get()).get();
}
if (killer instanceof Player) {
Player player = (Player) killer;
UUID playerUUID = player.getUniqueId();
String victimName = victim.getType().getName();
String playerJob = getPlayerJob(player);
Optional<TEJob> optPlayerJob = getJob(playerJob, true);
if (optPlayerJob.isPresent()) {
Optional<TEActionReward> reward = Optional.empty();
List<String> sets = optPlayerJob.get().getSets();
for (String s : sets) {
Optional<TEJobSet> optSet = getJobSet(s);
if (!optSet.isPresent()) {
logger.warn("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
continue;
}
reward = optSet.get().getRewardFor("kill", victimName);
}
if (reward.isPresent()) {
int expAmount = reward.get().getExpReward();
BigDecimal payAmount = reward.get().getMoneyReward();
boolean notify = accountConfig.getNode(playerUUID.toString(), "jobnotifications").getBoolean();
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
if (notify) {
notifyPlayer(player, payAmount);
}
addExp(player, expAmount);
playerAccount.deposit(totalEconomy.getDefaultCurrency(), payAmount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
checkForLevel(player);
}
}
}
}
}
use of org.spongepowered.api.event.Listener in project TotalEconomy by Erigitic.
the class TotalEconomy method preInit.
@Listener
public void preInit(GamePreInitializationEvent event) {
setupConfig();
defaultCurrency = new TECurrency(Text.of(config.getNode("currency", "currency-singular").getValue()), Text.of(config.getNode("currency", "currency-plural").getValue()), Text.of(config.getNode("currency", "symbol").getValue()), 2, true, config.getNode("currency", "prefix-symbol").getBoolean());
loadJobs = config.getNode("features", "jobs", "enable").getBoolean();
loadSalary = config.getNode("features", "jobs", "salary").getBoolean();
databaseActive = config.getNode("database", "enable").getBoolean();
jobPermissions = config.getNode("features", "jobs", "permissions").getBoolean();
jobNotifications = config.getNode("features", "jobs", "notifications").getBoolean();
loadMoneyCap = config.getNode("features", "moneycap", "enable").getBoolean();
if (databaseActive) {
databaseUrl = config.getNode("database", "url").getString();
databaseUser = config.getNode("database", "user").getString();
databasePassword = config.getNode("database", "password").getString();
sqlHandler = new SQLHandler(this);
}
saveInterval = config.getNode("save-interval").getInt(30);
accountManager = new AccountManager(this);
game.getServiceManager().setProvider(this, EconomyService.class, accountManager);
//Only setup job stuff if config is set to load jobs
if (loadJobs) {
teJobManager = new TEJobManager(this);
}
if (loadMoneyCap == true) {
moneyCap = BigDecimal.valueOf(config.getNode("features", "moneycap", "amount").getFloat()).setScale(2, BigDecimal.ROUND_DOWN);
}
}
use of org.spongepowered.api.event.Listener in project TotalEconomy by Erigitic.
the class TotalEconomy method onPlayerJoin.
@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event) {
Player player = event.getTargetEntity();
accountManager.getOrCreateAccount(player.getUniqueId());
}
use of org.spongepowered.api.event.Listener in project TotalEconomy by Erigitic.
the class TEJobManager method onPlayerFish.
/**
* Used for the catch option in jobs. Will check if the job has the catch node and if it does it will check if the
* item that was caught is present in the config of the player's job. If it is, it will grab the job exp reward as
* well as the pay.
*
* @param event FishingEvent.Stop
*/
@Listener
public void onPlayerFish(FishingEvent.Stop event) {
if (event.getCause().first(Player.class).isPresent()) {
// no transaction, so execution can stop
if (event.getItemStackTransaction().size() == 0) {
return;
}
Transaction<ItemStackSnapshot> itemTransaction = event.getItemStackTransaction().get(0);
ItemStack itemStack = itemTransaction.getFinal().createStack();
Player player = event.getCause().first(Player.class).get();
UUID playerUUID = player.getUniqueId();
String playerJob = getPlayerJob(player);
Optional<TEJob> optPlayerJob = getJob(playerJob, true);
if (optPlayerJob.isPresent()) {
if (itemStack.get(FishData.class).isPresent()) {
FishData fishData = itemStack.get(FishData.class).get();
String fishName = fishData.type().get().getName();
Optional<TEActionReward> reward = Optional.empty();
List<String> sets = optPlayerJob.get().getSets();
for (String s : sets) {
Optional<TEJobSet> optSet = getJobSet(s);
if (!optSet.isPresent()) {
logger.warn("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
continue;
}
reward = optSet.get().getRewardFor("catch", fishName);
}
if (reward.isPresent()) {
int expAmount = reward.get().getExpReward();
BigDecimal payAmount = reward.get().getMoneyReward();
boolean notify = accountConfig.getNode(playerUUID.toString(), "jobnotifications").getBoolean();
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
if (notify) {
notifyPlayer(player, payAmount);
}
addExp(player, expAmount);
playerAccount.deposit(totalEconomy.getDefaultCurrency(), payAmount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
checkForLevel(player);
}
}
}
}
}
use of org.spongepowered.api.event.Listener in project HuskyCrates-Sponge by codeHusky.
the class HuskyCrates method entityInteract.
@Listener
public void entityInteract(InteractEntityEvent.Secondary.MainHand event) {
//System.out.println(event.getTargetEntity().toContainer().get(DataQuery.of("UnsafeData","crateID")));
if (event.getCause().root() instanceof Player) {
Player plr = (Player) event.getCause().root();
if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent() && plr.hasPermission("huskycrates.wand")) {
ItemStack hand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
if (hand.getItem() == ItemTypes.BLAZE_ROD) {
if (hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
if (!crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
//System.out.println(event.getTargetEntity().getLocation().getBlockPosition());
event.getTargetEntity().offer(Keys.AI_ENABLED, false);
event.getTargetEntity().offer(Keys.IS_SILENT, true);
crateUtilities.physicalCrates.put(event.getTargetEntity().getLocation(), new PhysicalCrate(event.getTargetEntity().getLocation(), hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString(), this));
crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
updatePhysicalCrates();
} else {
event.getTargetEntity().offer(Keys.AI_ENABLED, true);
event.getTargetEntity().offer(Keys.IS_SILENT, false);
crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).as.remove();
crateUtilities.physicalCrates.remove(event.getTargetEntity().getLocation());
updatePhysicalCrates();
}
event.setCancelled(true);
return;
}
}
}
if (crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
String crateType = crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).vc.id;
VirtualCrate vc = crateUtilities.getVirtualCrate(crateType);
crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
//crateUtilities.recognizeChest(te.getLocation());
event.setCancelled(true);
if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
ItemStack inhand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
if (inhand.getItem() == vc.getKeyType()) {
if (inhand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
String id = inhand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString();
if (id.equals(crateType)) {
if (!plr.hasPermission("huskycrates.tester")) {
if (inhand.getQuantity() == 1)
plr.setItemInHand(HandTypes.MAIN_HAND, null);
else {
ItemStack tobe = inhand.copy();
tobe.setQuantity(tobe.getQuantity() - 1);
plr.setItemInHand(HandTypes.MAIN_HAND, tobe);
}
}
Task.Builder upcoming = scheduler.createTaskBuilder();
upcoming.execute(() -> {
crateUtilities.launchCrateForPlayer(crateType, plr, this);
}).delayTicks(1).submit(this);
return;
}
}
}
}
plr.playSound(SoundTypes.BLOCK_ANVIL_LAND, event.getTargetEntity().getLocation().getPosition(), 0.3);
try {
plr.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(vc.langData.formatter(vc.langData.prefix + vc.langData.noKeyMessage, null, plr, vc, null)));
} catch (Exception e) {
plr.sendMessage(Text.of(TextColors.RED, "Critical crate failure, contact the administrator. (Admins, check console!)"));
e.printStackTrace();
}
}
}
}
Aggregations