Skip to content

Commit

Permalink
1.3.0 MoneyNotes and Money managing after death!
Browse files Browse the repository at this point in the history
Fixed:
- Removed @testonly from ColorUtils.java
- Changed "Best" code length from 12 to 10
- Changed onPlayerJoin event to joinEvent
Added:
- Auto-Updating config file WITH old values
Created:
- Entire new class ClickEvent.java which tracks for Player Interact Event and checks if player "deposited" clicked money note
- New class that is allowing to manipulate deaths' economy settings
- /moneynote class (/withdraw as alias) that can generate a note
- New class file that generate Moneynote Itemstack
  • Loading branch information
ThisKarolGajda committed Sep 11, 2021
1 parent 7ac2fcf commit 8a41a90
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 13 deletions.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.opkarol</groupId>
<artifactId>opeconomy</artifactId>
<version>1.2.1</version>
<version>1.3.0</version>
<packaging>jar</packaging>

<name>OpEconomy</name>
Expand Down Expand Up @@ -80,6 +80,10 @@
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>maven-central</id>
<url>https://oss.sonatype.org/content/groups/public</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -101,5 +105,10 @@
<version>2.10.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.tchristofferson</groupId>
<artifactId>ConfigUpdater</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2 changes: 0 additions & 2 deletions src/main/java/me/opkarol/opeconomy/Economy.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public void onLoad(){
public void onEnable() {
DatabaseLoader.onStart();
pluginController = new PluginController(getEconomy());
pluginController.checkUpdates();
pluginController.enableMetrics();
me.opkarol.opeconomy.redeem.DatabaseLoader.onStart();

}
Expand Down
65 changes: 61 additions & 4 deletions src/main/java/me/opkarol/opeconomy/PluginController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package me.opkarol.opeconomy;

import com.tchristofferson.configupdater.ConfigUpdater;
import me.opkarol.opeconomy.commands.*;
import me.opkarol.opeconomy.economy.Database;
import me.opkarol.opeconomy.events.ClickEvent;
import me.opkarol.opeconomy.events.DeathEvent;
import me.opkarol.opeconomy.events.JoinEvent;
import me.opkarol.opeconomy.misc.Metrics;
import me.opkarol.opeconomy.misc.OpEconomyExpansion;
import me.opkarol.opeconomy.misc.UpdateChecker;
import me.opkarol.opeconomy.notes.NoteItem;
import me.opkarol.opeconomy.utils.ColorUtils;
import me.opkarol.opeconomy.utils.TransactionUtils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Logger;
Expand All @@ -31,10 +38,13 @@ public PluginController(Economy economy1) {
}

public void onPluginStart() {
updateConfig();
loadConfigurationFile();
me.opkarol.opeconomy.balanceTop.Database.loadMap();
checkUpdates();
registerCommands();
registerEvents();
enableMetrics();
}

public void loadConfigurationFile() {
Expand Down Expand Up @@ -83,11 +93,32 @@ public void loadConfigurationFile() {
BalTopExecutor.setDontHavePermission(getMessageFromConfig("BalanceTop.Executor.dontHavePermission"));
BalTopExecutor.setBadUsage(getMessageFromConfig("BalanceTop.Executor.badUsage"));
BalTopExecutor.setLastArgumentNotNumber(getMessageFromConfig("BalanceTop.Executor.lastArgumentNotNumber"));
DeathEvent.setKillerGetsMoney((Boolean) getFromConfig("DeathSettings.killerGetsMoney"));
DeathEvent.setLostMoneyOnDeath((Double) getFromConfig("DeathSettings.lostMoneyOnDeath"));
DeathEvent.setLostPercentageOfMoneyOnDeath((Double) getFromConfig("DeathSettings.lostPercentageOfMoneyOnDeath"));
NoteItem.setDepositedMoney(getMessageFromConfig("Notes.messages.depositedMoney"));
NoteItem.setWithdrawnMoney(getMessageFromConfig("Notes.messages.withdrawnMoney"));
NoteItem.setBadUsage(getMessageFromConfig("Notes.messages.badUsage"));
NoteItem.setNotAvailableToConsole(getMessageFromConfig("Notes.messages.notAvailableToConsole"));
NoteItem.setLastArgumentNotNumber(getMessageFromConfig("Notes.messages.lastArgumentNotNumber"));
NoteItem.setDontHavePermission(getMessageFromConfig("Notes.messages.dontHavePermission"));
NoteItem.setMinimumNote((Double) getFromConfig("Notes.notesSettings.minimumNote"));
NoteItem.setMaximumNote((Double) getFromConfig("Notes.notesSettings.maximumNote"));
NoteItem.setDepositSound(getMessageFromConfig("Notes.sounds.depositSound"));
NoteItem.setWithdrawSound(getMessageFromConfig("Notes.sounds.withdrawSound"));
NoteItem.setName(getMessageFromConfig("Notes.notesItem.name"));
NoteItem.setLore((List<String>) getFromConfig("Notes.notesItem.lore"));
NoteItem.setMaterial(getMessageFromConfig("Notes.notesItem.material"));
NoteItem.setEnchanted((Boolean) getFromConfig("Notes.notesItem.enchanted"));
NoteItem.setHidden((Boolean) getFromConfig("Notes.notesItem.hidden"));

}

public void registerEvents() {
PluginManager manager = Bukkit.getPluginManager();
manager.registerEvents(new JoinEvent(), economy);
manager.registerEvents(new DeathEvent(), economy);
manager.registerEvents(new ClickEvent(), economy);
}

public void registerCommands() {
Expand All @@ -96,6 +127,7 @@ public void registerCommands() {
Objects.requireNonNull(economy.getCommand("opreload")).setExecutor(new ReloadExecutor());
Objects.requireNonNull(economy.getCommand("redeem")).setExecutor(new RedeemExecutor());
Objects.requireNonNull(economy.getCommand("balancetop")).setExecutor(new BalTopExecutor());
Objects.requireNonNull(economy.getCommand("moneynote")).setExecutor(new NoteExecutor());
}

public String getMessageFromConfig(String path) {
Expand All @@ -107,9 +139,10 @@ public int getValueFromConfig(String path) {
}

public Object getFromConfig(String path) {
return economy.getConfig().get(path); }
return economy.getConfig().get(path);
}

public void enableMetrics(){
public void enableMetrics() {
int pluginId = 12735;
Metrics metrics = new Metrics(Economy.getEconomy(), pluginId);
metrics.addCustomChart(new Metrics.SingleLineChart("money", () -> {
Expand All @@ -121,16 +154,40 @@ public void enableMetrics(){
}));
}

public void checkUpdates(){
public void checkUpdates() {
Logger logger = Economy.getEconomy().getLogger();

new UpdateChecker(Economy.getEconomy(), 95674).getVersion(version -> {
String versionString = Economy.getEconomy().getDescription().getVersion();
if (versionString.equalsIgnoreCase(version)) {
logger.info("There is not a new update available. Current version: " + versionString);
} else {
logger.info("There is a new update available. Current version: " + versionString + ", New version: " + version);
logger.warning("There is a new update available. Current version: " + versionString + ", New version: " + version + ". Download it now: https://www.spigotmc.org/resources/95674/");
}
});
}

public double checkConfigVersion() {
return (double) getFromConfig("version");
}

public void updateConfig() {
economy.saveDefaultConfig();
double version = 0.3;
if (version != checkConfigVersion() || checkConfigVersion() == 0) {

File configFile = new File(economy.getDataFolder(), "config.yml");

try {
economy.getConfig().set("version", version);
economy.saveConfig();
ConfigUpdater.update(economy, "config.yml", configFile, null);
economy.getLogger().warning("Configuration file was recreated with new objects and old values! Please check it out! Previous version was " + checkConfigVersion() + ", current version " + version);
} catch (IOException e) {
e.printStackTrace();
}

economy.reloadConfig();
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/me/opkarol/opeconomy/commands/NoteExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package me.opkarol.opeconomy.commands;

import me.clip.placeholderapi.PlaceholderAPI;
import me.opkarol.opeconomy.Economy;
import me.opkarol.opeconomy.economy.Database;
import me.opkarol.opeconomy.notes.NoteItem;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import static me.opkarol.opeconomy.utils.Utils.isntValidInteger;
import static me.opkarol.opeconomy.utils.Utils.returnMessageToSender;

public class NoteExecutor extends NoteItem implements CommandExecutor {

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!sender.hasPermission("opeconomy.command.moneynote") && !sender.isOp()) return returnMessageToSender(sender, getDontHavePermission());

if (!(sender instanceof Player)) return returnMessageToSender(sender, getNotAvailableToConsole());

if (args.length == 0) return returnMessageToSender(sender, getBadUsage());

String moneyToWithdrawString = args[0];
if(isntValidInteger(moneyToWithdrawString)) return returnMessageToSender(sender, getLastArgumentNotNumber());

int amount;
try {amount = Integer.parseInt(moneyToWithdrawString);} catch (NumberFormatException e) { return returnMessageToSender(sender, getLastArgumentNotNumber()); }

if (amount < 0) return returnMessageToSender(sender, getLastArgumentNotNumber());
Player player = (Player) sender;
if (amount > getMaximumNote() || amount < getMinimumNote()) return returnMessageToSender(sender, getLastArgumentNotNumber());

if (Database.getMoneyFromUUID(player.getUniqueId()) >= amount){
player.getInventory().addItem(NoteItem.getNote(amount, player));
Database.removePlayerMoney(player.getUniqueId(), amount);
playSound(player, Sound.valueOf(getWithdrawSound()));
return returnMessageToSender(sender, PlaceholderAPI.setPlaceholders(player, getWithdrawnMoney().replace("%money_withdrawn%", String.valueOf(amount))));
}

return returnMessageToSender(sender, getBadUsage());
}


}
46 changes: 46 additions & 0 deletions src/main/java/me/opkarol/opeconomy/events/ClickEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.opkarol.opeconomy.events;

import me.clip.placeholderapi.PlaceholderAPI;
import me.opkarol.opeconomy.economy.Database;
import me.opkarol.opeconomy.notes.NoteItem;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class ClickEvent extends NoteItem implements Listener {

@EventHandler(priority = EventPriority.HIGH)
public void clickEvent(@NotNull PlayerInteractEvent event){
ItemStack item = event.getPlayer().getInventory().getItemInHand();
if (!item.isSimilar(itemStack)) return;

int value;
try {
value = Objects.requireNonNull(item.getItemMeta()).getPersistentDataContainer().get(Objects.requireNonNull(NamespacedKey.fromString("opeconomy-money-note-key-dont-leak-pls")), PersistentDataType.INTEGER);
} catch (Exception e) {
return;
}

Player player = event.getPlayer();

int slot = player.getInventory().getHeldItemSlot();
ItemStack amount = player.getInventory().getItem(slot);

assert amount != null;
if (amount.getAmount() == 1) player.getInventory().remove(item);
else amount.setAmount(amount.getAmount() - 1);

Database.addPlayerMoney(player.getUniqueId(), value);
playSound(player, Sound.valueOf(getDepositSound()));
player.sendMessage(PlaceholderAPI.setPlaceholders(player, getDepositedMoney().replace("%money_deposited%", String.valueOf(value))));
}
}
52 changes: 52 additions & 0 deletions src/main/java/me/opkarol/opeconomy/events/DeathEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.opkarol.opeconomy.events;

import me.opkarol.opeconomy.Economy;
import me.opkarol.opeconomy.economy.Database;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.jetbrains.annotations.NotNull;

public class DeathEvent implements Listener {
static double lostMoneyOnDeath;
static double lostPercentageOfMoneyOnDeath;
static boolean killerGetsMoney;

@EventHandler(priority = EventPriority.HIGH)
public void deathEvent(@NotNull PlayerDeathEvent event){
Player died = event.getEntity();
Player killer = event.getEntity().getKiller();

if (died == killer){
return;
}

if (killer == null) return;

if (lostPercentageOfMoneyOnDeath != 0 && lostMoneyOnDeath != 0) return; //Both can't be turned on in the same time
else if (lostPercentageOfMoneyOnDeath == 0 && lostMoneyOnDeath == 0) return; //Nothing happens, both turned off

if (!killerGetsMoney) return;

double money;
if (lostMoneyOnDeath != 0) money = lostMoneyOnDeath;
else money = Database.getMoneyFromPlayer(died) * lostPercentageOfMoneyOnDeath;

Economy.getAPI().removeMoney(died.getUniqueId(), (int) money);
Economy.getAPI().addMoney(killer.getUniqueId(), (int) money);
}

public static void setKillerGetsMoney(boolean killerGetsMoney) {
DeathEvent.killerGetsMoney = killerGetsMoney;
}

public static void setLostMoneyOnDeath(double lostMoneyOnDeath) {
DeathEvent.lostMoneyOnDeath = lostMoneyOnDeath;
}

public static void setLostPercentageOfMoneyOnDeath(double lostPercentageOfMoneyOnDeath) {
DeathEvent.lostPercentageOfMoneyOnDeath = lostPercentageOfMoneyOnDeath;
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/opkarol/opeconomy/events/JoinEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class JoinEvent extends TransactionUtils implements Listener {

@EventHandler(priority = EventPriority.MONITOR)
void onPlayerJoin(@NotNull PlayerJoinEvent event) {
void joinEvent(@NotNull PlayerJoinEvent event) {
if (!isPlayerAccountExists(event.getPlayer().getUniqueId())) createNewBankAccount(event.getPlayer().getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OpEconomyExpansion extends PlaceholderExpansion{

@Override
public @NotNull String getVersion() {
return "1.2.0";
return "1.3.0";
}

@Override
Expand Down
Loading

0 comments on commit 8a41a90

Please sign in to comment.