use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.
the class TEJobManager method onSignInteract.
/**
* Called when a player clicks a sign. If the clicked sign is a "Job Changing" sign then the player's job will
* be changed on click.
*
* @param event InteractBlockEvent
*/
@Listener
public void onSignInteract(InteractBlockEvent event) {
if (event.getCause().first(Player.class).isPresent()) {
Player player = event.getCause().first(Player.class).get();
if (event.getTargetBlock().getLocation().isPresent()) {
Optional<TileEntity> tileEntityOpt = event.getTargetBlock().getLocation().get().getTileEntity();
if (tileEntityOpt.isPresent()) {
TileEntity tileEntity = tileEntityOpt.get();
if (tileEntity instanceof Sign) {
Sign sign = (Sign) tileEntity;
Optional<SignData> data = sign.getOrCreate(SignData.class);
if (data.isPresent()) {
SignData signData = data.get();
Text lineOneText = signData.lines().get(0);
Text lineTwoText = signData.lines().get(1);
String lineOne = lineOneText.toPlain();
String lineTwo = lineTwoText.toPlain().toLowerCase();
if (lineOne.equals("[TEJobs]")) {
if (jobExists(lineTwo)) {
if (setJob(player, lineTwo)) {
player.sendMessage(Text.of(TextColors.GRAY, "Job changed to: ", TextColors.GOLD, lineTwo));
} else {
player.sendMessage(Text.of(TextColors.RED, "[TE] Failed to set job. Contact your administrator."));
}
} else {
player.sendMessage(Text.of(TextColors.RED, "[TE] Sorry, this job does not exist"));
}
}
}
}
}
}
}
}
use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.
the class TEJobManager method onPlayerPlaceBlock.
/**
* Used for the place option in jobs. Will check if the job has the place node and if it does it will check if the
* block that was placed 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 ChangeBlockEvent.Place
*/
@Listener
public void onPlayerPlaceBlock(ChangeBlockEvent.Place event) {
if (event.getCause().first(Player.class).isPresent()) {
Player player = event.getCause().first(Player.class).get();
UUID playerUUID = player.getUniqueId();
String playerJob = getPlayerJob(player);
Optional<TEJob> optPlayerJob = getJob(playerJob, true);
String blockName = event.getTransactions().get(0).getFinal().getState().getType().getName();
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("place", blockName);
}
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.entity.living.player.Player 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.entity.living.player.Player 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.entity.living.player.Player in project TotalEconomy by Erigitic.
the class PayCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
String strAmount = (String) args.getOne("amount").get();
Player recipient = (Player) args.getOne("player").get();
if (src instanceof Player) {
Player sender = (Player) src;
if (sender.getUniqueId().equals(recipient.getUniqueId())) {
throw new CommandException(Text.of("You cannot pay yourself!"));
}
Pattern amountPattern = Pattern.compile("^[+]?(\\d*\\.)?\\d+$");
Matcher m = amountPattern.matcher(strAmount);
if (m.matches()) {
BigDecimal amount = new BigDecimal(strAmount).setScale(2, BigDecimal.ROUND_DOWN);
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
TransferResult transferResult = playerAccount.transfer(recipientAccount, totalEconomy.getDefaultCurrency(), amount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
if (transferResult.getResult() == ResultType.SUCCESS) {
sender.sendMessage(Text.of(TextColors.GRAY, "You have sent ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " to ", TextColors.GOLD, recipient.getName(), TextColors.GRAY, "."));
recipient.sendMessage(Text.of(TextColors.GRAY, "You have received ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " from ", TextColors.GOLD, sender.getName(), TextColors.GRAY, "."));
return CommandResult.success();
} else if (transferResult.getResult() == ResultType.ACCOUNT_NO_FUNDS) {
throw new CommandException(Text.of("Insufficient funds!"));
}
} else {
throw new CommandException(Text.of("Invalid amount! Must be a positive number!"));
}
}
return CommandResult.empty();
}
Aggregations