1.0 Start
This commit is contained in:
parent
dab143e72a
commit
dcab74a952
@ -1,101 +1,278 @@
|
|||||||
package net.ichverstehs.atm_plugin;
|
package net.ichverstehs.atm_plugin;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.*;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.entity.Interaction;
|
import org.bukkit.entity.Interaction;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.*;
|
||||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||||
|
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.InventoryView;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.persistence.PersistentDataContainer;
|
||||||
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public final class ATM_Plugin extends JavaPlugin implements Listener {
|
public final class ATM_Plugin extends JavaPlugin implements Listener {
|
||||||
|
|
||||||
private final Map<Player, Integer> withdrawAmounts = new HashMap<>();
|
private final Map<Player, Map<Integer, Integer>> withdrawCounts = new HashMap<>();
|
||||||
|
private final NamespacedKey atmKey = new NamespacedKey("atmplugin", "realmoney");
|
||||||
|
private final NamespacedKey accountkey = new NamespacedKey("atmplugin", "realaccount");
|
||||||
|
|
||||||
|
private final Map<UUID, String> playeraccountname = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
Bukkit.getPluginManager().registerEvents(this, this);
|
getServer().getPluginManager().registerEvents(this, this);
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
if (IfPlayerHasRealMoney(player, atmKey)) {
|
||||||
|
if (player.getGameMode() != GameMode.SURVIVAL) {
|
||||||
|
player.setGameMode(GameMode.SURVIVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.runTaskTimer(this, 0L, 1L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean IfPlayerHasRealMoney(Player player, NamespacedKey key) {
|
||||||
|
|
||||||
|
|
||||||
|
// Spieler-Inventar
|
||||||
|
ItemStack[] contents = player.getInventory().getContents();
|
||||||
|
for (ItemStack item : contents) {
|
||||||
|
if (isMoney(item, key)) return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Top Inventory - Kiste
|
||||||
|
InventoryView view = player.getOpenInventory();
|
||||||
|
if (view != null) {
|
||||||
|
Inventory top = view.getTopInventory();
|
||||||
|
if (top != null) {
|
||||||
|
ItemStack[] topContents = top.getContents();
|
||||||
|
for (ItemStack item : topContents) {
|
||||||
|
if (isMoney(item, key)) return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isMoney(ItemStack item, NamespacedKey key) {
|
||||||
|
if (item == null) return false;
|
||||||
|
if (item.getType() != Material.EMERALD) return false;
|
||||||
|
if (!item.hasItemMeta()) return false;
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
return meta.getPersistentDataContainer().has(key, PersistentDataType.BYTE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerInteract(PlayerInteractEntityEvent event) {
|
public void onPlayerInteract(PlayerInteractEntityEvent event) {
|
||||||
if (event.getRightClicked() instanceof Interaction) {
|
if (event.getRightClicked() instanceof Interaction) {
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
playeraccountname.put(player.getUniqueId(), player.getName());
|
||||||
openATMMenu(player);
|
openATMMenu(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openATMMenu(Player player) {
|
|
||||||
withdrawAmounts.put(player, 0);
|
|
||||||
|
|
||||||
int currentBalance = getCurrentBalance(player);
|
|
||||||
|
|
||||||
Inventory menu = Bukkit.createInventory(null, 54, ChatColor.GREEN + "ATM " + ChatColor.DARK_GREEN + "| " + ChatColor.GOLD + currentBalance + "€");
|
|
||||||
|
|
||||||
menu.setItem(10, createItem(Material.DIAMOND, "Einzahlen", "Geld Einzahlen"));
|
@EventHandler
|
||||||
menu.setItem(12, createItem(Material.GOLD_INGOT, "Abheben", "Geld Abheben"));
|
public void onInventoryClick(InventoryClickEvent event) {
|
||||||
menu.setItem(14, createItem(Material.PAPER, "Überweisung", "Geld Überweisen"));
|
if (!(event.getWhoClicked() instanceof Player player)) return;
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++) {
|
|
||||||
menu.setItem(i, createGlassPane());
|
Inventory inventory = event.getClickedInventory();
|
||||||
menu.setItem(45 + i, createGlassPane());
|
Inventory clickedInventory = event.getClickedInventory();
|
||||||
|
ItemStack clickedItem = event.getCurrentItem();
|
||||||
|
ItemStack cursorItem = event.getCursor();
|
||||||
|
ClickType click = event.getClick();
|
||||||
|
|
||||||
|
ItemMeta meta = null;
|
||||||
|
|
||||||
|
if (clickedItem == null || clickedInventory == null) return; //no item
|
||||||
|
|
||||||
|
if (clickedItem.hasItemMeta()) {
|
||||||
|
meta = clickedItem.getItemMeta();
|
||||||
}
|
}
|
||||||
|
|
||||||
player.openInventory(menu);
|
|
||||||
|
String TitleName = event.getView().getTitle();
|
||||||
|
if (clickedInventory.equals(player.getInventory()) && TitleName.startsWith(ChatColor.GREEN + "ATM")) {
|
||||||
|
if (meta.getPersistentDataContainer().has(accountkey, PersistentDataType.BYTE) && meta.getCustomModelData() == 1) {
|
||||||
|
|
||||||
|
event.setCancelled(true);
|
||||||
|
String[] kontoname = meta.getDisplayName().split(" ", 2);
|
||||||
|
|
||||||
|
playeraccountname.put(player.getUniqueId(), kontoname[0]);
|
||||||
|
openATMMenu(player);
|
||||||
|
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_CHIME, SoundCategory.VOICE, 100,1);
|
||||||
|
|
||||||
|
}
|
||||||
|
} else if (clickedInventory.equals(player.getInventory())) {
|
||||||
|
return; //if not in
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (event.getView().getTitle().startsWith(ChatColor.GREEN + "ATM")) { //If in ATM
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
if (clickedItem.getType() == Material.GOLD_INGOT) {
|
||||||
|
openWithdrawMenu(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clickedItem.getType() == Material.DIAMOND) {
|
||||||
|
openDepositMenu(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (event.getView().getTitle().startsWith(ChatColor.GREEN + "Einzahlen")) { //If in ATM
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
if (clickedItem.getType() == Material.CHEST) {
|
||||||
|
Deposit(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clickedItem.getType() == Material.BARRIER) {
|
||||||
|
openATMMenu(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (event.getView().getTitle().startsWith(ChatColor.GREEN + "Abheben")) { //If in Abheben
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
if (meta != null && meta.hasCustomModelData()) {
|
||||||
|
Map<Integer, Integer> playerWithdrawals = withdrawCounts.getOrDefault(player, new HashMap<>());
|
||||||
|
|
||||||
|
if (click == ClickType.LEFT) {
|
||||||
|
playerWithdrawals.put(meta.getCustomModelData(),
|
||||||
|
playerWithdrawals.getOrDefault(meta.getCustomModelData(), 0) + 1);
|
||||||
|
} else if (click == ClickType.RIGHT && playerWithdrawals.getOrDefault(meta.getCustomModelData(), 0) >= 1) {
|
||||||
|
playerWithdrawals.put(meta.getCustomModelData(),
|
||||||
|
playerWithdrawals.getOrDefault(meta.getCustomModelData(), 0) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
withdrawCounts.put(player, playerWithdrawals);
|
||||||
|
openWithdrawMenu(player);
|
||||||
|
} else if (clickedItem.getType() == Material.CHEST) {
|
||||||
|
Withdraw(player);
|
||||||
|
} else if (clickedItem.getType() == Material.BARRIER) {
|
||||||
|
withdrawCounts.put(player, new HashMap<>());
|
||||||
|
openATMMenu(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openWithdrawalMenu(Player player) {
|
|
||||||
int currentBalance = getCurrentBalance(player);
|
|
||||||
|
|
||||||
Inventory withdrawalMenu = Bukkit.createInventory(null, 27, ChatColor.GREEN + "Abheben " + ChatColor.DARK_GREEN + "| " + ChatColor.GOLD + currentBalance + "€");
|
|
||||||
|
private int getEmeraldValue(int customModelData) {
|
||||||
|
return switch (customModelData) {
|
||||||
|
case 1 -> 1;
|
||||||
|
case 2 -> 100;
|
||||||
|
case 3 -> 500;
|
||||||
|
case 4 -> 1000;
|
||||||
|
case 5 -> 50;
|
||||||
|
default -> 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private String MenuName(Player player, String Menu) {
|
||||||
|
if (playeraccountname.getOrDefault(player.getUniqueId(), "") == player.getName()) { //If Player
|
||||||
|
return ChatColor.GREEN + Menu + ChatColor.DARK_GREEN + " | " + ChatColor.GOLD + formatCurrency(getMoney(playeraccountname.getOrDefault(player.getUniqueId(), ""))) + "€";
|
||||||
|
} else {
|
||||||
|
return ChatColor.GREEN + Menu + ChatColor.DARK_GREEN + " | " + ChatColor.GOLD + formatCurrency(getMoney(playeraccountname.getOrDefault(player.getUniqueId(), ""))) + "€" + ChatColor.DARK_GREEN + " | " + ChatColor.BLUE + playeraccountname.getOrDefault(player.getUniqueId(), "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void openATMMenu(Player player) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Inventory MainMenu = Bukkit.createInventory(player, 27, MenuName(player,"ATM"));
|
||||||
|
|
||||||
|
|
||||||
|
MainMenu.setItem(11, createItem(Material.DIAMOND, "Einzahlen", "Geld Einzahlen"));
|
||||||
|
MainMenu.setItem(13, createItem(Material.GOLD_INGOT, "Abheben", "Geld Abheben"));
|
||||||
|
MainMenu.setItem(15, createItem(Material.BOOK, "Überweisen", "Geld Überweisen - Comming Soon"));
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
withdrawalMenu.setItem(i, createGlassPane());
|
MainMenu.setItem(i, createItem(Material.WHITE_STAINED_GLASS_PANE, " ", null));
|
||||||
withdrawalMenu.setItem(18 + i, createGlassPane());
|
MainMenu.setItem(18 + i, createItem(Material.WHITE_STAINED_GLASS_PANE, " ", null));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
withdrawalMenu.setItem(11, createEmerald(1, "1 Euro", 0));
|
player.openInventory(MainMenu);
|
||||||
withdrawalMenu.setItem(12, createEmerald(5, "50 Euro", 0));
|
|
||||||
withdrawalMenu.setItem(13, createEmerald(2, "100 Euro", 0));
|
}
|
||||||
withdrawalMenu.setItem(14, createEmerald(3, "500 Euro", 0));
|
|
||||||
withdrawalMenu.setItem(15, createEmerald(4, "1000 Euro", 0));
|
private void openWithdrawMenu(Player player) {
|
||||||
|
|
||||||
|
Inventory withdrawalMenu = Bukkit.createInventory(null, 27, MenuName(player,"Abheben"));
|
||||||
|
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
withdrawalMenu.setItem(i, createItem(Material.WHITE_STAINED_GLASS_PANE, " ", null));
|
||||||
|
withdrawalMenu.setItem(18 + i, createItem(Material.WHITE_STAINED_GLASS_PANE, " ", null));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
withdrawalMenu.setItem(11, addCustomModelData(createItem(Material.EMERALD, "1 Euro", String.valueOf(withdrawCounts.getOrDefault(player, new HashMap<>()).getOrDefault(1, 0)) + "x"),1));
|
||||||
|
withdrawalMenu.setItem(12, addCustomModelData(createItem(Material.EMERALD, "50 Euro", String.valueOf(withdrawCounts.getOrDefault(player, new HashMap<>()).getOrDefault(5, 0)) + "x"),5));
|
||||||
|
withdrawalMenu.setItem(13, addCustomModelData(createItem(Material.EMERALD, "100 Euro", String.valueOf(withdrawCounts.getOrDefault(player, new HashMap<>()).getOrDefault(2, 0)) + "x"),2));
|
||||||
|
withdrawalMenu.setItem(14, addCustomModelData(createItem(Material.EMERALD, "500 Euro", String.valueOf(withdrawCounts.getOrDefault(player, new HashMap<>()).getOrDefault(3, 0)) + "x"),3));
|
||||||
|
withdrawalMenu.setItem(15, addCustomModelData(createItem(Material.EMERALD, "1000 Euro", String.valueOf(withdrawCounts.getOrDefault(player, new HashMap<>()).getOrDefault(4, 0)) + "x"),4));
|
||||||
|
|
||||||
|
|
||||||
withdrawalMenu.setItem(22, createItem(Material.CHEST, "Abheben", ""));
|
withdrawalMenu.setItem(22, createItem(Material.CHEST, "Abheben", ""));
|
||||||
withdrawalMenu.setItem(26, createItem(Material.BARRIER, "Zurück", "Zurück zum Hauptmenü"));
|
withdrawalMenu.setItem(26, createItem(Material.BARRIER, "Zurück", "Zurück zum Hauptmenü"));
|
||||||
|
|
||||||
updateWithdrawAmountDisplay(player, withdrawalMenu);
|
updateWithdrawMenuItems(player, withdrawalMenu);
|
||||||
player.openInventory(withdrawalMenu);
|
player.openInventory(withdrawalMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateWithdrawAmountDisplay(Player player, Inventory menu) {
|
private void openDepositMenu(Player player) {
|
||||||
int amount = withdrawAmounts.getOrDefault(player, 0);
|
|
||||||
ItemStack display = new ItemStack(Material.PAPER);
|
Inventory DepositMenu = Bukkit.createInventory(null, 27, MenuName(player,"Einzahlen"));
|
||||||
ItemMeta meta = display.getItemMeta();
|
for (int i = 0; i < 9; i++) {
|
||||||
|
DepositMenu.setItem(i, createItem(Material.WHITE_STAINED_GLASS_PANE, " ", null));
|
||||||
|
DepositMenu.setItem(18 + i, createItem(Material.WHITE_STAINED_GLASS_PANE, " ", null));
|
||||||
|
|
||||||
if (meta != null) {
|
|
||||||
meta.setDisplayName(ChatColor.AQUA + "Betrag: " + String.valueOf(amount) + "€");
|
|
||||||
display.setItemMeta(meta);
|
|
||||||
}
|
}
|
||||||
menu.setItem(4, display);
|
|
||||||
|
DepositMenu.setItem(13, createItem(Material.CHEST, "Alles Einzahlen", ""));
|
||||||
|
|
||||||
|
|
||||||
|
DepositMenu.setItem(26, createItem(Material.BARRIER, "Zurück", "Zurück zum Hauptmenü"));
|
||||||
|
|
||||||
|
player.openInventory(DepositMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getCurrentBalance(Player player) {
|
|
||||||
return Bukkit.getScoreboardManager().getMainScoreboard()
|
|
||||||
.getObjective("Konto")
|
|
||||||
.getScore(player.getName())
|
|
||||||
.getScore();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ItemStack createItem(Material material, String name, String desc) {
|
private ItemStack createItem(Material material, String name, String desc) {
|
||||||
ItemStack item = new ItemStack(material);
|
ItemStack item = new ItemStack(material);
|
||||||
@ -103,125 +280,299 @@ public final class ATM_Plugin extends JavaPlugin implements Listener {
|
|||||||
|
|
||||||
if (meta != null) {
|
if (meta != null) {
|
||||||
meta.setDisplayName(ChatColor.YELLOW + name);
|
meta.setDisplayName(ChatColor.YELLOW + name);
|
||||||
List<String> lore = new ArrayList<>();
|
if (desc != null && !desc.isEmpty()) {
|
||||||
lore.add(ChatColor.GRAY + desc);
|
List<String> lore = new ArrayList<>();
|
||||||
meta.setLore(lore);
|
lore.add(ChatColor.GRAY + desc);
|
||||||
|
meta.setLore(lore);
|
||||||
|
}
|
||||||
item.setItemMeta(meta);
|
item.setItemMeta(meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemStack createEmerald(int customModelData, String name, int count) {
|
private ItemStack addCustomModelData(ItemStack item, int customModelData) {
|
||||||
ItemStack emerald = new ItemStack(Material.EMERALD);
|
ItemMeta meta = item.getItemMeta();
|
||||||
ItemMeta meta = emerald.getItemMeta();
|
if (meta != null) {
|
||||||
|
meta.setCustomModelData(customModelData);
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String formatCurrency(long amount) {
|
||||||
|
return String.format("%,d", amount).replace(',', '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateWithdrawMenuItems(Player player, Inventory menu) {
|
||||||
|
int totalAmount = withdrawCounts.getOrDefault(player, new HashMap<>()) //sum
|
||||||
|
.entrySet()
|
||||||
|
.stream()
|
||||||
|
.mapToInt(entry -> getEmeraldValue(entry.getKey()) * entry.getValue())
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
ItemStack display = new ItemStack(Material.EMERALD);
|
||||||
|
ItemMeta meta = display.getItemMeta();
|
||||||
|
|
||||||
if (meta != null) {
|
if (meta != null) {
|
||||||
meta.setDisplayName(ChatColor.YELLOW + name);
|
meta.setDisplayName(ChatColor.AQUA + "Betrag: " + formatCurrency(totalAmount) + "€");
|
||||||
meta.setCustomModelData(customModelData);
|
display.setItemMeta(meta);
|
||||||
List<String> lore = new ArrayList<>();
|
}
|
||||||
lore.add(ChatColor.GRAY + String.valueOf(count) + "x");
|
menu.setItem(4, display); // Set Item in 4
|
||||||
meta.setLore(lore);
|
}
|
||||||
emerald.setItemMeta(meta);
|
|
||||||
|
|
||||||
|
private void Deposit(Player player) {
|
||||||
|
int totalAmount = 0;
|
||||||
|
|
||||||
|
for (ItemStack item : player.getInventory().getContents()) {
|
||||||
|
if (item == null) continue;
|
||||||
|
if (isMoney(item, atmKey)) {
|
||||||
|
int value = getEmeraldValue(item.getItemMeta().getCustomModelData());
|
||||||
|
totalAmount += value * item.getAmount();
|
||||||
|
player.getInventory().remove(item); // Remove Item
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (totalAmount > 0) {
|
||||||
|
changeMoney(playeraccountname.getOrDefault(player.getUniqueId(), ""), totalAmount, 0);
|
||||||
|
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_BELL, SoundCategory.VOICE, 100,1);
|
||||||
|
player.sendMessage(ChatColor.GOLD + "[ATM] " + ChatColor.GREEN + "Du hast " + formatCurrency(totalAmount) + "€ eingezahlt");
|
||||||
|
player.closeInventory();
|
||||||
|
} else {
|
||||||
|
player.playSound(player, Sound.ENTITY_VILLAGER_NO,SoundCategory.VOICE, 100, 1);
|
||||||
|
player.sendMessage(ChatColor.GOLD + "[ATM] " + ChatColor.RED + "Du hast kein Geld in deinem Inventar!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Withdraw(Player player) {
|
||||||
|
int totalAmount = withdrawCounts.getOrDefault(player, new HashMap<>())
|
||||||
|
.entrySet()
|
||||||
|
.stream()
|
||||||
|
.mapToInt(entry -> getEmeraldValue(entry.getKey()) * entry.getValue()) // sum
|
||||||
|
.sum();
|
||||||
|
long balance = getMoney(playeraccountname.getOrDefault(player.getUniqueId(), ""));
|
||||||
|
|
||||||
|
if (totalAmount == 0) {
|
||||||
|
player.playSound(player, Sound.ENTITY_VILLAGER_NO,SoundCategory.VOICE, 100, 1);
|
||||||
|
player.sendMessage(ChatColor.GOLD + "[ATM] " +ChatColor.RED + "Du hast kein Geld ausgewählt");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (totalAmount > 0 && balance >= totalAmount) {
|
||||||
|
changeMoney(playeraccountname.getOrDefault(player.getUniqueId(), ""), totalAmount, 1);
|
||||||
|
|
||||||
|
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_BELL, SoundCategory.VOICE, 100,1);
|
||||||
|
player.sendMessage(ChatColor.GOLD + "[ATM] " + ChatColor.GREEN + "Du hast " + formatCurrency(totalAmount) + "€ abgehoben");
|
||||||
|
|
||||||
|
|
||||||
|
giveEmeralds(player);
|
||||||
|
withdrawCounts.put(player, new HashMap<>());
|
||||||
|
player.closeInventory();
|
||||||
|
} else {
|
||||||
|
player.playSound(player, Sound.ENTITY_VILLAGER_NO,SoundCategory.VOICE, 100, 1);
|
||||||
|
player.sendMessage(ChatColor.GOLD + "[ATM] " +ChatColor.RED + "Du hast nicht genug Geld!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void giveEmeralds(Player player) {
|
||||||
|
Map<Integer, Integer> playerWithdrawals = withdrawCounts.getOrDefault(player, new HashMap<>());
|
||||||
|
|
||||||
|
|
||||||
|
Map<Integer, Integer> valueMap = Map.of(
|
||||||
|
1, 1, // CustomModelData 1 = 1€
|
||||||
|
5, 50, // CustomModelData 5 = 50€
|
||||||
|
2, 100, // CustomModelData 2 = 100€
|
||||||
|
3, 500, // CustomModelData 3 = 500€
|
||||||
|
4, 1000 // CustomModelData 4 = 1000€
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sortierung der Beträge
|
||||||
|
List<Map.Entry<Integer, Integer>> sortedEntries = new ArrayList<>(playerWithdrawals.entrySet());
|
||||||
|
sortedEntries.sort(Comparator.comparingInt(entry -> valueMap.getOrDefault(entry.getKey(), Integer.MAX_VALUE)));
|
||||||
|
|
||||||
|
|
||||||
|
for (Map.Entry<Integer, Integer> entry : sortedEntries) { //Items ausgeben
|
||||||
|
int customModelData = entry.getKey();
|
||||||
|
int count = entry.getValue();
|
||||||
|
|
||||||
|
|
||||||
|
int emeraldValue = valueMap.getOrDefault(customModelData, 0);
|
||||||
|
int totalEmeraldValue = emeraldValue * count;
|
||||||
|
|
||||||
|
// Gib den Spieler die Emeralds für den Betrag
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
player.getInventory().addItem(GenRealMoney(customModelData)); // Füge Emerald mit CustomModelData hinzu
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void changeMoney(String playerName, long amount, int action) {
|
||||||
|
File file = new File(getDataFolder(), "ATM_Plugin_Money.txt");
|
||||||
|
List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
|
String line;
|
||||||
|
boolean firstLine = true;
|
||||||
|
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
if (firstLine) {
|
||||||
|
lines.add(line); // erste Zeile beibehalten
|
||||||
|
firstLine = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String[] parts = line.split(" ");
|
||||||
|
if (parts.length == 2 && parts[0].equalsIgnoreCase(playerName)) {
|
||||||
|
long balance = Long.parseLong(parts[1]);
|
||||||
|
if (action == 1) {
|
||||||
|
balance -= amount; // Betrag abziehen
|
||||||
|
} else {
|
||||||
|
balance += amount; // Betrag abziehen
|
||||||
|
}
|
||||||
|
lines.add(parts[0] + " " + balance);
|
||||||
|
} else {
|
||||||
|
lines.add(line); // unverändert übernehmen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (FileWriter writer = new FileWriter(file, false)) {
|
||||||
|
for (String l : lines) {
|
||||||
|
writer.write(l + System.lineSeparator());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public long getMoney(String playerName) {
|
||||||
|
File file = new File(getDataFolder(), "ATM_Plugin_Money.txt");
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
|
try (FileWriter writer = new FileWriter(file)) {
|
||||||
|
writer.write("----ATM Plugin | Money:----\n"); // Erste Zeile
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
|
String line;
|
||||||
|
boolean firstLine = true;
|
||||||
|
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
if (firstLine) {
|
||||||
|
firstLine = false; // erste Zeile überspringen
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] parts = line.split(" ");
|
||||||
|
if (parts.length == 2) {
|
||||||
|
String name = parts[0];
|
||||||
|
long betrag = Long.parseLong(parts[1]);
|
||||||
|
|
||||||
|
if (name.equalsIgnoreCase(playerName)) {
|
||||||
|
return betrag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Player target = Bukkit.getPlayerExact(playerName);
|
||||||
|
if (target != null && target.isOnline()) { //if a player
|
||||||
|
|
||||||
|
// If player has no entry, create one with 5000
|
||||||
|
try (FileWriter writer = new FileWriter(file, true)) {
|
||||||
|
writer.write(playerName + " 5000\n");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 5000;
|
||||||
|
|
||||||
|
} else { // if not a player
|
||||||
|
|
||||||
|
try (FileWriter writer = new FileWriter(file, true)) {
|
||||||
|
writer.write(playerName + " 0\n");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ItemStack GenRealMoney(int ID) {
|
||||||
|
ItemStack emerald = new ItemStack(Material.EMERALD);
|
||||||
|
ItemMeta meta = emerald.getItemMeta();
|
||||||
|
if (meta != null) {
|
||||||
|
PersistentDataContainer dataContainer = meta.getPersistentDataContainer();
|
||||||
|
dataContainer.set(atmKey, PersistentDataType.BYTE, (byte) 1);
|
||||||
|
|
||||||
|
|
||||||
|
if (ID == 1) { // 1 Euro
|
||||||
|
meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.UNDERLINE + "1 Euro");
|
||||||
|
meta.setCustomModelData(ID);
|
||||||
|
List<String> lore = new ArrayList<>();
|
||||||
|
lore.add(ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + "Official Money");
|
||||||
|
meta.setLore(lore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ID == 5) { // 50 Euro
|
||||||
|
meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.UNDERLINE + "50 Euro");
|
||||||
|
meta.setCustomModelData(ID);
|
||||||
|
List<String> lore = new ArrayList<>();
|
||||||
|
lore.add(ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + "Official Money");
|
||||||
|
meta.setLore(lore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ID == 2) { // 100 Euro
|
||||||
|
meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.UNDERLINE + "100 Euro");
|
||||||
|
meta.setCustomModelData(ID);
|
||||||
|
List<String> lore = new ArrayList<>();
|
||||||
|
lore.add(ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + "Official Money");
|
||||||
|
meta.setLore(lore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ID == 3) { // 500 Euro
|
||||||
|
meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.UNDERLINE + "500 Euro");
|
||||||
|
meta.setCustomModelData(ID);
|
||||||
|
List<String> lore = new ArrayList<>();
|
||||||
|
lore.add(ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + "Official Money");
|
||||||
|
meta.setLore(lore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ID == 4) { // 1000 Euro
|
||||||
|
meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.UNDERLINE + "1000 Euro");
|
||||||
|
meta.setCustomModelData(ID);
|
||||||
|
List<String> lore = new ArrayList<>();
|
||||||
|
lore.add(ChatColor.DARK_AQUA + "" + ChatColor.ITALIC + "Official Money");
|
||||||
|
meta.setLore(lore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emerald.setItemMeta(meta);
|
||||||
return emerald;
|
return emerald;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemStack createGlassPane() {
|
|
||||||
ItemStack glassPane = new ItemStack(Material.YELLOW_STAINED_GLASS_PANE);
|
|
||||||
ItemMeta meta = glassPane.getItemMeta();
|
|
||||||
|
|
||||||
if (meta != null) {
|
|
||||||
meta.setDisplayName(" ");
|
|
||||||
glassPane.setItemMeta(meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
return glassPane;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
|
||||||
Player player = (Player) event.getWhoClicked();
|
|
||||||
|
|
||||||
if (event.getView().getTitle().startsWith(ChatColor.GREEN + "ATM")) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
|
|
||||||
if (event.getCurrentItem() != null && event.getCurrentItem().getType() == Material.GOLD_INGOT) {
|
|
||||||
openWithdrawalMenu(player);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.getView().getTitle().startsWith(ChatColor.GREEN + "Abheben")) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
|
|
||||||
if (event.getCurrentItem() != null) {
|
|
||||||
Material clickedType = event.getCurrentItem().getType();
|
|
||||||
ItemStack clickedItem = event.getCurrentItem();
|
|
||||||
ItemMeta meta = clickedItem.getItemMeta();
|
|
||||||
|
|
||||||
if (meta != null && meta.hasCustomModelData()) {
|
|
||||||
int value = getEmeraldValue(meta.getCustomModelData());
|
|
||||||
int amount = withdrawAmounts.getOrDefault(player, 0);
|
|
||||||
|
|
||||||
if (event.getClick().isLeftClick()) {
|
|
||||||
amount += value;
|
|
||||||
updateEmeraldLore(clickedItem, meta, 1);
|
|
||||||
} else if (event.getClick().isRightClick() && amount >= value) {
|
|
||||||
amount -= value;
|
|
||||||
updateEmeraldLore(clickedItem, meta, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
withdrawAmounts.put(player, amount);
|
|
||||||
updateWithdrawAmountDisplay(player, event.getInventory());
|
|
||||||
} else if (clickedType == Material.CHEST) {
|
|
||||||
withdrawAmountFromAccount(player);
|
|
||||||
player.closeInventory();
|
|
||||||
} else if (clickedType == Material.BARRIER) {
|
|
||||||
openATMMenu(player);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateEmeraldLore(ItemStack item, ItemMeta meta, int change) {
|
|
||||||
List<String> lore = meta.getLore();
|
|
||||||
|
|
||||||
if (lore != null && !lore.isEmpty()) {
|
|
||||||
String firstLine = lore.get(0);
|
|
||||||
int count = Integer.parseInt(firstLine.replace(ChatColor.GRAY.toString(), "").replace("x", "").trim()) + change;
|
|
||||||
count = Math.max(count, 0); // Verhindert negative Werte
|
|
||||||
lore.set(0, ChatColor.GRAY + String.valueOf(count) + "x");
|
|
||||||
meta.setLore(lore);
|
|
||||||
item.setItemMeta(meta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getEmeraldValue(int customModelData) {
|
|
||||||
switch (customModelData) {
|
|
||||||
case 1: return 1;
|
|
||||||
case 2: return 100;
|
|
||||||
case 3: return 500;
|
|
||||||
case 4: return 1000;
|
|
||||||
case 5: return 50;
|
|
||||||
default: return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void withdrawAmountFromAccount(Player player) {
|
|
||||||
int amount = withdrawAmounts.getOrDefault(player, 0);
|
|
||||||
int balance = getCurrentBalance(player);
|
|
||||||
|
|
||||||
if (amount > 0 && balance >= amount) {
|
|
||||||
Bukkit.getScoreboardManager().getMainScoreboard()
|
|
||||||
.getObjective("Konto")
|
|
||||||
.getScore(player.getName())
|
|
||||||
.setScore(balance - amount);
|
|
||||||
player.sendMessage(ChatColor.GREEN + "Du hast " + amount + "€ abgehoben!");
|
|
||||||
} else {
|
|
||||||
player.sendMessage(ChatColor.RED + "Nicht genug Geld!");
|
|
||||||
}
|
|
||||||
|
|
||||||
withdrawAmounts.put(player, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -2,13 +2,3 @@ name: ATM_Plugin
|
|||||||
version: '${project.version}'
|
version: '${project.version}'
|
||||||
main: net.ichverstehs.atm_plugin.ATM_Plugin
|
main: net.ichverstehs.atm_plugin.ATM_Plugin
|
||||||
api-version: '1.20'
|
api-version: '1.20'
|
||||||
commands:
|
|
||||||
testmenu:
|
|
||||||
description: Öffne das Test-Menü
|
|
||||||
usage: /<command>
|
|
||||||
createarmorstand:
|
|
||||||
description: Erstellt einen Armorstand mit einem Tag
|
|
||||||
usage: /createarmorstand
|
|
||||||
giveuncopyableemerald:
|
|
||||||
description: "Gibt dir einen unkopierbaren Smaragd"
|
|
||||||
usage: "/giveuncopyableemerald"
|
|
||||||
Loading…
x
Reference in New Issue
Block a user