Split project

This commit is contained in:
Len
2022-07-09 14:53:27 +02:00
parent 259e806d59
commit bde0f5d040
25 changed files with 132 additions and 69 deletions
+50
View File
@@ -0,0 +1,50 @@
import java.io.ByteArrayOutputStream
plugins {
id("java")
id("net.minecrell.plugin-yml.bukkit") version "0.5.1"
id("com.github.johnrengelman.shadow") version "7.1.0"
}
dependencies {
implementation(project(":api"))
compileOnly("com.alttd:Galaxy-API:1.18.2-R0.1-SNAPSHOT")
compileOnly("com.github.milkbowl:VaultAPI:1.7") {
exclude("org.bukkit","bukkit")
}
compileOnly("com.github.TechFortress:GriefPrevention:16.17.1")
compileOnly("org.projectlombok:lombok:1.18.24")
annotationProcessor("org.projectlombok:lombok:1.18.24")
}
bukkit {
name = rootProject.name
main = "$group.${rootProject.name}"
version = "${rootProject.version}-${gitCommit()}"
apiVersion = "1.18"
authors = listOf("destro174")
depend = listOf("Vault")
}
fun gitCommit(): String {
val os = ByteArrayOutputStream()
project.exec {
commandLine = "git rev-parse --short HEAD".split(" ")
standardOutput = os
}
return String(os.toByteArray()).trim()
}
tasks {
shadowJar {
archiveFileName.set("${rootProject.name}-${project.version}.jar")
}
build {
dependsOn(shadowJar)
}
}
@@ -0,0 +1,79 @@
package com.alttd.playershops;
import com.alttd.playershops.api.ShopHandler;
import com.alttd.playershops.listener.PlayerListener;
import com.alttd.playershops.listener.ShopListener;
import com.alttd.playershops.config.Config;
import com.alttd.playershops.handler.ShopHandlerImpl;
import lombok.Getter;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class PlayerShops extends JavaPlugin {
@Getter
private static PlayerShops instance;
@Getter
private Economy econ;
@Getter
private ShopHandler shopHandler;
private ShopListener shopListener;
private PlayerListener playerListener;
public void onEnable() {
instance = this;
if(!setupEconomy()) {
Bukkit.getLogger().warning("Error loading Vault and economy.\n Disabling plugin");
this.setEnabled(false);
return;
}
Bukkit.getLogger().info("Hooked into Vault economy provided by " + econ.getName());
reloadConfigs();
registerListeners();
registerCommands();
shopHandler = new ShopHandlerImpl(instance);
}
private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
return true;
}
public Economy getEconomy() {
if(econ == null)
setupEconomy();
return econ;
}
private void registerListeners() {
shopListener = new ShopListener(this);
playerListener = new PlayerListener(this);
}
private void UnregisterListeners() {
shopListener.unregister();
playerListener.unregister();
}
private void registerCommands() {
// for(String command : this.getDescription().getCommands().keySet()) {
// getCommand(command).setExecutor();
// }
}
public void reloadConfigs() {
Config.reload();
}
}
@@ -0,0 +1,49 @@
package com.alttd.playershops.config;
import com.alttd.galaxy.configuration.AbstractConfiguration;
import com.alttd.playershops.api.Shop;
import com.alttd.playershops.api.ShopType;
import java.io.File;
import java.util.HashMap;
@SuppressWarnings("unused")
public class Config extends AbstractConfiguration {
private Config() {
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "com/alttd/playershops"), "config");
}
static Config config;
static int version;
static HashMap<ShopType, ShopTypeConfig> shopTypeConfigs;
public static void reload() {
config = new Config();
version = config.getInt("config-version", 1);
config.set("config-version", 1);
config.readConfig(Config.class, null);
shopTypeConfigs = new HashMap<>();
for (ShopType shopType : ShopType.values()) {
shopTypeConfigs.put(shopType, new ShopTypeConfig(shopType.toString()));
}
}
public static int shopLimit = 100;
public static boolean usePermissionShopLimit = false;
private static void shopSettings() {
String path = "shop-settings.";
shopLimit = config.getInt(path + "player-shop-limit", shopLimit);
usePermissionShopLimit = config.getBoolean(path + "use-permission-based-shop-limit", usePermissionShopLimit);
}
public static String shopLimitPermission = "shop.buildlimit";
private static void permissionSettings() {
String path = "permission.";
shopLimitPermission = config.getString(path + "build-limit", shopLimitPermission);
}
}
@@ -0,0 +1,30 @@
package com.alttd.playershops.config;
public class ShopTypeConfig {
private final String shopType;
private final String configPath;
private final String defaultPath;
public ShopTypeConfig(String shopType) {
this.shopType = shopType;
this.configPath = "shop.type." + this.shopType + ".";
this.defaultPath = "shop.type.default.";
init();
}
public void init() {
Config.config.readConfig(ShopTypeConfig.class, this);
}
private static void set(String path, Object def) {
Config.config.set(path, def);
}
private String getString(String path, String def) {
set(defaultPath + path, def);
return Config.config.getNode(configPath + path).getString(
Config.config.getNode(defaultPath + path).getString(def));
}
}
@@ -0,0 +1,99 @@
package com.alttd.playershops.handler;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.config.Config;
import com.alttd.playershops.api.Shop;
import com.alttd.playershops.api.ShopHandler;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ShopHandlerImpl implements ShopHandler {
private final PlayerShops plugin;
@Getter
private final Object2IntMap<UUID> shopBuildLimits;
@Getter
private final Map<Location, Shop> shopLocation;
@Getter
private final ArrayList<Material> shopMaterials;
public ShopHandlerImpl(PlayerShops instance) {
plugin = instance;
shopLocation = new ConcurrentHashMap<>();
shopBuildLimits = new Object2IntOpenHashMap<>();
shopBuildLimits.defaultReturnValue(Config.shopLimit);
shopMaterials = new ArrayList<>(); // TODO move into parent method where materials are loaded in.
}
public Shop getShop(Location location) {
Location newLocation = new Location(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
return shopLocation.get(newLocation);
}
public boolean isShop(Location location) {
return getShop(location) != null;
}
public Collection<Shop> getShops() {
return Collections.unmodifiableCollection(shopLocation.values());
}
public void addPlayerLimit(UUID uuid, int limit) {
shopBuildLimits.put(uuid, limit);
}
public int getShopLimit(UUID uuid) {
return shopBuildLimits.getInt(uuid);
}
public void removeShops() {
shopLocation.clear();
}
public boolean isShopMaterial(Block block) {
if (Tag.SHULKER_BOXES.isTagged(block.getType())) {
return true;
}
return shopMaterials.contains(block.getType());
}
public List<Shop> getShops(UUID uuid) {
List<Shop> shops = new ArrayList<>();
for (Shop shop : shopLocation.values()) {
if (shop.getOwnerUUID().equals(uuid))
shops.add(shop);
}
return shops;
}
public Shop getShopBySignLocation(Location signLocation) {
for (Shop shop : shopLocation.values()) {
if (shop.getSignLocation().equals(signLocation))
return shop;
}
return null;
}
public Shop getShopNearBlock(Block block) {
BlockFace[] faces = {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
for (BlockFace face : faces) {
if (this.isShopMaterial(block.getRelative(face))) {
Block blockRelative = block.getRelative(face);
if (isShop(blockRelative.getLocation()))
return getShop(blockRelative.getLocation());
}
}
return null;
}
}
@@ -0,0 +1,21 @@
package com.alttd.playershops.listener;
import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class EventListener implements Listener {
protected boolean isRegistered = false;
public void register(JavaPlugin instance) {
Bukkit.getServer().getPluginManager().registerEvents(this, instance);
isRegistered = true;
}
public void unregister() {
HandlerList.unregisterAll(this);
isRegistered = false;
}
}
@@ -0,0 +1,39 @@
package com.alttd.playershops.listener;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
public class InventoryListener extends EventListener {
protected final Inventory inventory;
protected boolean cancelCloseUnregister = false;
public InventoryListener(Inventory inv) {
this.inventory = inv;
}
@EventHandler(ignoreCancelled = true)
public void unregisterOnClose(InventoryCloseEvent event) {
if (event.getView().getTopInventory().equals(inventory) && !cancelCloseUnregister) unregister();
}
@EventHandler(ignoreCancelled = true)
public void unregisterOnLeaveEvent(PlayerQuitEvent event) {
if ((inventory.getHolder() instanceof OfflinePlayer) && event.getPlayer().getUniqueId().equals(((OfflinePlayer) inventory.getHolder()).getUniqueId()))
unregister();
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onInventoryDrag(InventoryDragEvent event) {
if (event.getView().getTopInventory().equals(inventory)) for (int slot : event.getRawSlots()) if (slot < inventory.getSize()) {
event.setCancelled(true);
return;
}
}
}
@@ -0,0 +1,68 @@
package com.alttd.playershops.listener;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.api.Shop;
import com.alttd.playershops.api.ShopHandler;
import com.alttd.playershops.config.Config;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.permissions.PermissionAttachmentInfo;
import java.util.UUID;
public class PlayerListener extends EventListener {
private final PlayerShops plugin;
public PlayerListener(PlayerShops plugin) {
this.plugin = plugin;
this.register(this.plugin);
}
@EventHandler(ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
if(!this.isRegistered || !Config.usePermissionShopLimit) return;
Player player = event.getPlayer();
if (!player.hasPermission(Config.shopLimitPermission)) return;
ShopHandler shopHandler = plugin.getShopHandler();
UUID uuid = player.getUniqueId();
// early return to not check this all the time, if this changes by rankup etc handle it in another event
if (shopHandler.getShopLimit(uuid) != Config.shopLimit) return;
int buildPermissionNumber = -1;
for (PermissionAttachmentInfo permInfo : player.getEffectivePermissions()) {
if (permInfo.getPermission().contains("shop.buildlimit.")) {
try {
int tempNum = Integer.parseInt(permInfo.getPermission().substring(permInfo.getPermission().lastIndexOf(".") + 1));
if (tempNum > buildPermissionNumber) {
buildPermissionNumber = tempNum;
}
} catch (Exception ignore) {}
}
}
if (buildPermissionNumber == -1)
shopHandler.addPlayerLimit(player.getUniqueId(), buildPermissionNumber);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onSignChange(SignChangeEvent event) {
Block block = event.getBlock();
if (!(block.getState() instanceof Sign)) return;
Shop shop = plugin.getShopHandler().getShop(block.getLocation());
if(shop == null) return;
if(shop.isInitialized()) event.setCancelled(true);
}
}
@@ -0,0 +1,13 @@
package com.alttd.playershops.listener;
import com.alttd.playershops.PlayerShops;
public class ShopListener extends EventListener {
private final PlayerShops plugin;
public ShopListener(PlayerShops plugin) {
this.plugin = plugin;
this.register(this.plugin);
}
}
@@ -0,0 +1,78 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.api.Shop;
import com.alttd.playershops.api.ShopType;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.data.Directional;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public abstract class AbstractShop implements Shop {
@Getter
private int id;
private String ownerName;
@Getter
private UUID ownerUUID;
@Getter @Setter
private ShopType type;
@Getter
private Location signLocation;
@Getter
private Location containerLocation;
@Getter
private String server;
@Getter @Setter
private double price;
@Getter @Setter
private int amount;
@Getter @Setter
private double balance;
@Getter @Setter
private ItemStack itemStack;
@Getter @Setter
private ItemStack secondaryItem;
@Getter @Setter
private long lastTransaction;
protected boolean initialized;
AbstractShop(Location signLocation, UUID uuid, double price, int amount) {
this.signLocation = signLocation;
if (signLocation != null) {
Directional sign = (Directional) signLocation.getBlock().getState().getBlockData();
this.containerLocation = signLocation.getBlock().getRelative(sign.getFacing().getOppositeFace()).getLocation();
}
this.ownerUUID = uuid;
ownerName = getOwnerName();
this.price = price;
this.amount = amount;
}
public static AbstractShop create(Location signLocation, UUID player, double price, int amount, ShopType shopType) {
return switch (shopType) {
case SELL -> new SellShop(signLocation, player, price, amount);
case BUY -> new BuyShop(signLocation, player, price, amount);
case GAMBLE -> new GambleShop(signLocation, player, price, amount);
case BARTER -> new BarterShop(signLocation, player, price, amount);
};
}
public String getOwnerName() {
if(this.ownerName != null) return ownerName;
if (this.getOwnerUUID() != null) {
ownerName = Bukkit.getOfflinePlayer(this.getOwnerUUID()).getName();
return ownerName;
}
return ChatColor.RED + "[CLOSED]";
}
public boolean isInitialized() {
return initialized;
}
}
@@ -0,0 +1,16 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.api.ShopType;
import org.bukkit.Location;
import java.util.UUID;
public class BarterShop extends AbstractShop {
public BarterShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.BARTER);
}
}
@@ -0,0 +1,16 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.api.ShopType;
import org.bukkit.Location;
import java.util.UUID;
public class BuyShop extends AbstractShop {
public BuyShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.BUY);
}
}
@@ -0,0 +1,20 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.api.ShopType;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class GambleShop extends AbstractShop {
private ItemStack gambleItem;
public GambleShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.GAMBLE);
this.gambleItem = this.getItemStack();
}
}
@@ -0,0 +1,16 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.api.ShopType;
import org.bukkit.Location;
import java.util.UUID;
public class SellShop extends AbstractShop {
public SellShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.SELL);
}
}
@@ -0,0 +1,13 @@
package com.alttd.playershops.shop;
public class ShopTransaction {
public enum ShopTransactionError {
CANCELLED,
INSUFFICIENT_FUNDS_SHOP,
INSUFFICIENT_FUNDS_PLAYER,
INVENTORY_FULL_SHOP,
INVENTORY_FULL_PLAYER,
NONE;
}
}
@@ -0,0 +1,10 @@
package com.alttd.playershops.shop;
public enum ShopTransactionError {
CANCELLED,
INSUFFICIENT_FUNDS_SHOP,
INSUFFICIENT_FUNDS_PLAYER,
INVENTORY_FULL_SHOP,
INVENTORY_FULL_PLAYER,
NONE;
}
@@ -0,0 +1,29 @@
package com.alttd.playershops.utils;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import java.util.logging.Level;
public class Logger
{
public static void info(String str) {
log(Level.INFO,"&e" + str);
}
public static void warn(String str) {
log(Level.SEVERE,"&6" + str);
}
public static void severe(String str) {
log(Level.SEVERE,"&c" + str);
}
public static void log(Level level, String str) {
Bukkit.getLogger().log(level,
ChatColor.translateAlternateColorCodes('&',
"&r " + str));
}
}