Files
src
build.gradle
settings.gradle
Code
9 779
src/main/java/com/kodari/shopchest/ShopChestPlugin.java
package com.kodari.shopchest;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.block.data.Directional;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
public final class ShopChestPlugin extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private final Map<Location, Shop> shops = new HashMap<>();
private final Map<UUID, Double> balances = new HashMap<>();
private final DecimalFormat moneyFormat = new DecimalFormat("0.00");
private MessageService messages;
private File shopsFile;
private File balancesFile;
@Override
public void onEnable() {
saveDefaultConfig();
messages = new MessageService(this);
shopsFile = new File(getDataFolder(), "shops.yml");
balancesFile = new File(getDataFolder(), "balances.yml");
loadBalances();
loadShops();
Bukkit.getPluginManager().registerEvents(this, this);
if (getCommand("shop") != null) {
getCommand("shop").setExecutor(this);
getCommand("shop").setTabCompleter(this);
}
}
@Override
public void onDisable() {
saveBalances();
saveShops();
}
@EventHandler(priority = EventPriority.NORMAL)
public void onSignChange(SignChangeEvent event) {
if (!event.getLine(0).equalsIgnoreCase("[shop]")) {
return;
}
Player player = event.getPlayer();
if (!player.hasPermission("shopchest.use")) {
messages.send(player, "no-permission", Collections.emptyMap());
return;
}
Block chestBlock = findAttachedChest(event.getBlock());
if (chestBlock == null) {
messages.send(player, "invalid-shop-sign", Collections.emptyMap());
return;
}
Location location = normalize(chestBlock.getLocation());
Shop existing = shops.get(location);
if (existing == null && countShops(player.getUniqueId()) >= getConfig().getInt("settings.max-shops-per-player", 10)) {
messages.send(player, "shop-limit", Collections.emptyMap());
return;
}
if (existing != null && !existing.getOwner().equals(player.getUniqueId()) && !player.hasPermission("shopchest.admin")) {
messages.send(player, "no-permission", Collections.emptyMap());
return;
}
Double buyPrice = parsePrice(event.getLine(1));
Double sellPrice = parsePrice(event.getLine(2));
Material material = Material.matchMaterial(event.getLine(3).trim());
if (buyPrice == null || sellPrice == null) {
messages.send(player, "invalid-price", Collections.emptyMap());
return;
}
if (material == null || material.isAir()) {
messages.send(player, "invalid-material", Collections.emptyMap());
return;
}
if (buyPrice < -1 || sellPrice < -1) {
messages.send(player, "invalid-price", Collections.emptyMap());
return;
}
if (buyPrice == -1 && sellPrice == -1) {
messages.send(player, "price-required", Collections.emptyMap());
return;
}
Shop shop = new Shop(location, player.getUniqueId(), material, buyPrice, sellPrice);
shops.put(location, shop);
saveShops();
messages.send(player, "shop-created", placeholders("material", material.name()));
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.LEFT_CLICK_BLOCK) {
return;
}
Block clicked = event.getClickedBlock();
if (clicked == null) {
return;
}
Location chestLocation = normalize(clicked.getLocation());
if (!(clicked.getState() instanceof Chest)) {
Block chest = findAttachedChest(clicked);
if (chest == null) {
return;
}
chestLocation = normalize(chest.getLocation());
}
Shop shop = shops.get(chestLocation);
if (shop == null) {
return;
}
event.setCancelled(true);
int amount = event.getPlayer().isSneaking() ? getConfig().getInt("settings.max-transaction-items", 64) : 1;
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
buy(event.getPlayer(), shop, amount);
} else {
sell(event.getPlayer(), shop, amount);
}
}
private void buy(Player player, Shop shop, int amount) {
if (shop.getBuyPrice() < 0) {
messages.send(player, "shop-disabled-buy", Collections.emptyMap());
return;
}
Chest chest = getChest(shop);
if (chest == null) {
messages.send(player, "shop-missing-container", Collections.emptyMap());
return;
}
double price = shop.getBuyPrice() * amount;
double balance = getBalance(player.getUniqueId());
if (balance < price) {
messages.send(player, "not-enough-money", placeholders("amount", money(price), "balance", money(balance)));
return;
}
ItemStack item = new ItemStack(shop.getMaterial(), amount);
if (!chest.getInventory().containsAtLeast(item, amount)) {
messages.send(player, "not-enough-stock", Collections.emptyMap());
return;
}
if (!canFit(player.getInventory(), item)) {
messages.send(player, "inventory-full", Collections.emptyMap());
return;
}
chest.getInventory().removeItem(item);
player.getInventory().addItem(item);
setBalance(player.getUniqueId(), balance - price);
setBalance(shop.getOwner(), getBalance(shop.getOwner()) + price);
saveBalances();
messages.send(player, "purchase", placeholders("amount", String.valueOf(amount), "material", shop.getMaterial().name(), "price", money(price)));
}
private void sell(Player player, Shop shop, int amount) {
if (shop.getSellPrice() < 0) {
messages.send(player, "shop-disabled-sell", Collections.emptyMap());
return;
}
Chest chest = getChest(shop);
if (chest == null) {
messages.send(player, "shop-missing-container", Collections.emptyMap());
return;
}