From f671a07a4d1dd25208739a9d1bb9a5a6425e0e1e Mon Sep 17 00:00:00 2001 From: FantasyPvP <80643031+FantasyPvP@users.noreply.github.com> Date: Thu, 23 Jan 2025 23:44:30 +0000 Subject: [PATCH] progress --- pom.xml | 2 +- .../java/dev/zxq5/fantasysmp/Fantasysmp.java | 18 +++ .../zxq5/fantasysmp/items/GenericGearSet.java | 81 ++++++++++-- .../dev/zxq5/fantasysmp/items/HasRecipes.java | 5 + .../dev/zxq5/fantasysmp/items/Witherite.java | 40 ++++++ .../java/dev/zxq5/fantasysmp/warps/Warp.java | 119 ++++++++++-------- .../dev/zxq5/fantasysmp/warps/Warper.java | 36 ++++++ src/main/resources/plugin.yml | 25 +++- 8 files changed, 257 insertions(+), 69 deletions(-) create mode 100644 src/main/java/dev/zxq5/fantasysmp/items/HasRecipes.java diff --git a/pom.xml b/pom.xml index fe242d4..d7656d4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.zxq5 fantasysmp - 1.0-SNAPSHOT + 1.1-SNAPSHOT jar fantasysmp diff --git a/src/main/java/dev/zxq5/fantasysmp/Fantasysmp.java b/src/main/java/dev/zxq5/fantasysmp/Fantasysmp.java index eb483b7..268c19a 100644 --- a/src/main/java/dev/zxq5/fantasysmp/Fantasysmp.java +++ b/src/main/java/dev/zxq5/fantasysmp/Fantasysmp.java @@ -1,8 +1,10 @@ package dev.zxq5.fantasysmp; import dev.zxq5.fantasysmp.events.StevenKillCheck; +import dev.zxq5.fantasysmp.items.GenericGearSet; import dev.zxq5.fantasysmp.items.Items; import dev.zxq5.fantasysmp.items.Witherite; +import dev.zxq5.fantasysmp.warps.Warp; import dev.zxq5.fantasysmp.warps.Warper; import org.bukkit.plugin.java.JavaPlugin; @@ -15,9 +17,25 @@ public final class Fantasysmp extends JavaPlugin { getCommand("items").setExecutor(items); // Plugin startup logic + try { + Warp.loadWarps(); + } catch (Exception e) { + e.printStackTrace(); + } + getCommand("home").setExecutor(new Warper()); getCommand("sethome").setExecutor(new Warper()); + getCommand("setwarp").setExecutor(new Warper()); + getCommand("warp").setExecutor(new Warper()); + + getCommand("reloadwarps").setExecutor(new Warper()); + + getServer().broadcastMessage("registered commands + \n /home \n /sethome \n /warp \n /setwarp"); + + GenericGearSet witherite = new Witherite(); + witherite.registerRecipes(); + getServer().getPluginManager().registerEvents(new StevenKillCheck(), this); } diff --git a/src/main/java/dev/zxq5/fantasysmp/items/GenericGearSet.java b/src/main/java/dev/zxq5/fantasysmp/items/GenericGearSet.java index e7fc624..c1dfdcb 100644 --- a/src/main/java/dev/zxq5/fantasysmp/items/GenericGearSet.java +++ b/src/main/java/dev/zxq5/fantasysmp/items/GenericGearSet.java @@ -6,9 +6,13 @@ import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeModifier; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.RecipeChoice; import org.bukkit.inventory.meta.ItemMeta; -public abstract class GenericGearSet { +import java.util.ArrayList; +import java.util.List; + +public abstract class GenericGearSet implements HasRecipes { protected int swordAttackDamage = 0; protected float swordAttackSpeed = 0; protected int swordDurability = 0; @@ -39,6 +43,15 @@ public abstract class GenericGearSet { protected String bootsName = "leather boots"; protected String bootsLore = ""; + protected ItemStack helmetMaterial = new ItemStack(Material.LEATHER_HELMET); + protected ItemStack chestplateMaterial = new ItemStack(Material.LEATHER_CHESTPLATE); + protected ItemStack leggingsMaterial = new ItemStack(Material.LEATHER_LEGGINGS); + protected ItemStack bootsMaterial = new ItemStack(Material.LEATHER_BOOTS); + protected ItemStack swordMaterial = new ItemStack(Material.WOODEN_SWORD); + + protected static RecipeChoice NETHERITE_UPGRADE = new RecipeChoice.MaterialChoice(Material.NETHERITE_UPGRADE_SMITHING_TEMPLATE); + protected static RecipeChoice WITHER_SKULL = new RecipeChoice.MaterialChoice(Material.WITHER_SKELETON_SKULL); + public void setTier1() { swordAttackDamage = 4; swordAttackSpeed = 1.6f; @@ -47,6 +60,12 @@ public abstract class GenericGearSet { } public void setTier3() { + helmetMaterial = new ItemStack(Material.IRON_HELMET); + chestplateMaterial = new ItemStack(Material.IRON_CHESTPLATE); + leggingsMaterial = new ItemStack(Material.IRON_LEGGINGS); + ItemStack bootsMaterial = new ItemStack(Material.IRON_BOOTS); + swordMaterial = new ItemStack(Material.IRON_SWORD); + swordAttackDamage = 6; swordAttackSpeed = 1.6f; helmetArmour = 2; @@ -60,6 +79,12 @@ public abstract class GenericGearSet { } public void setTier4() { + helmetMaterial = new ItemStack(Material.DIAMOND_HELMET); + chestplateMaterial = new ItemStack(Material.DIAMOND_CHESTPLATE); + leggingsMaterial = new ItemStack(Material.DIAMOND_LEGGINGS); + bootsMaterial = new ItemStack(Material.DIAMOND_BOOTS); + swordMaterial = new ItemStack(Material.DIAMOND_SWORD); + swordAttackDamage = 7; swordAttackSpeed = 1.6f; helmetArmour = 3; @@ -73,6 +98,12 @@ public abstract class GenericGearSet { } public void setTier5() { + helmetMaterial = new ItemStack(Material.NETHERITE_HELMET); + chestplateMaterial = new ItemStack(Material.NETHERITE_CHESTPLATE); + leggingsMaterial = new ItemStack(Material.NETHERITE_LEGGINGS); + bootsMaterial = new ItemStack(Material.NETHERITE_BOOTS); + swordMaterial = new ItemStack(Material.NETHERITE_SWORD); + swordAttackDamage = 8; swordAttackSpeed = 1.6f; helmetArmour = 3; @@ -86,6 +117,12 @@ public abstract class GenericGearSet { } public void setTier6() { + helmetMaterial = new ItemStack(Material.NETHERITE_HELMET); + chestplateMaterial = new ItemStack(Material.NETHERITE_CHESTPLATE); + leggingsMaterial = new ItemStack(Material.NETHERITE_LEGGINGS); + bootsMaterial = new ItemStack(Material.NETHERITE_BOOTS); + swordMaterial = new ItemStack(Material.NETHERITE_SWORD); + swordAttackDamage = 10; swordAttackSpeed = 1.6f; helmetArmour = 3; @@ -102,17 +139,21 @@ public abstract class GenericGearSet { public static void init() {} public ItemStack getSword() { - ItemStack item = new ItemStack(Material.WOODEN_SWORD, 1); + ItemStack item = swordMaterial; + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(this.swordName); + meta.setLore(new ArrayList<>(List.of(this.swordLore))); + meta.addAttributeModifier(Attribute.ATTACK_DAMAGE, new AttributeModifier( - NamespacedKey.minecraft("generic.attackDamage"), + NamespacedKey.minecraft("generic.attack_damage"), swordAttackDamage, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.HAND )); meta.addAttributeModifier(Attribute.ATTACK_SPEED, new AttributeModifier( - NamespacedKey.minecraft("generic.attackSpeed"), + NamespacedKey.minecraft("generic.attack_speed"), swordAttackSpeed, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.HAND @@ -123,8 +164,12 @@ public abstract class GenericGearSet { } public ItemStack getHelmet() { - ItemStack item = new ItemStack(Material.LEATHER_HELMET, 1); + ItemStack item = helmetMaterial; + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(this.helmetName); + meta.setLore(new ArrayList<>(List.of(this.helmetLore))); + meta.addAttributeModifier(Attribute.ARMOR, new AttributeModifier( NamespacedKey.minecraft("generic.armor"), helmetArmour, @@ -132,7 +177,7 @@ public abstract class GenericGearSet { EquipmentSlotGroup.HEAD )); meta.addAttributeModifier(Attribute.ARMOR_TOUGHNESS, new AttributeModifier( - NamespacedKey.minecraft("generic.armorToughness"), + NamespacedKey.minecraft("generic.armor_toughness"), helmetArmourToughness, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.HEAD @@ -142,8 +187,12 @@ public abstract class GenericGearSet { } public ItemStack getChestplate() { - ItemStack item = new ItemStack(Material.LEATHER_CHESTPLATE, 1); + ItemStack item = chestplateMaterial; + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(this.chestplateName); + meta.setLore(new ArrayList<>(List.of(this.chestplateLore))); + meta.addAttributeModifier(Attribute.ARMOR, new AttributeModifier( NamespacedKey.minecraft("generic.armor"), chestplateArmour, @@ -151,7 +200,7 @@ public abstract class GenericGearSet { EquipmentSlotGroup.CHEST )); meta.addAttributeModifier(Attribute.ARMOR_TOUGHNESS, new AttributeModifier( - NamespacedKey.minecraft("generic.armorToughness"), + NamespacedKey.minecraft("generic.armor_toughness"), chestplateArmourToughness, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.CHEST @@ -161,8 +210,12 @@ public abstract class GenericGearSet { } public ItemStack getLeggings() { - ItemStack item = new ItemStack(Material.LEATHER_LEGGINGS, 1); + ItemStack item = leggingsMaterial; + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(this.leggingsName); + meta.setLore(new ArrayList<>(List.of(this.leggingsLore))); + meta.addAttributeModifier(Attribute.ARMOR, new AttributeModifier( NamespacedKey.minecraft("generic.armor"), leggingsArmour, @@ -170,7 +223,7 @@ public abstract class GenericGearSet { EquipmentSlotGroup.LEGS )); meta.addAttributeModifier(Attribute.ARMOR_TOUGHNESS, new AttributeModifier( - NamespacedKey.minecraft("generic.armorToughness"), + NamespacedKey.minecraft("generic.armor_toughness"), leggingsArmourToughness, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.LEGS @@ -180,8 +233,12 @@ public abstract class GenericGearSet { } public ItemStack getBoots() { - ItemStack item = new ItemStack(Material.LEATHER_BOOTS, 1); + ItemStack item = bootsMaterial; + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(this.bootsName); + meta.setLore(new ArrayList<>(List.of(this.bootsLore))); + meta.addAttributeModifier(Attribute.ARMOR, new AttributeModifier( NamespacedKey.minecraft("generic.armor"), bootsArmour, @@ -189,7 +246,7 @@ public abstract class GenericGearSet { EquipmentSlotGroup.FEET )); meta.addAttributeModifier(Attribute.ARMOR_TOUGHNESS, new AttributeModifier( - NamespacedKey.minecraft("generic.armorToughness"), + NamespacedKey.minecraft("generic.armor_toughness"), bootsArmourToughness, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.FEET diff --git a/src/main/java/dev/zxq5/fantasysmp/items/HasRecipes.java b/src/main/java/dev/zxq5/fantasysmp/items/HasRecipes.java new file mode 100644 index 0000000..31809fa --- /dev/null +++ b/src/main/java/dev/zxq5/fantasysmp/items/HasRecipes.java @@ -0,0 +1,5 @@ +package dev.zxq5.fantasysmp.items; + +public interface HasRecipes { + void registerRecipes(); +} diff --git a/src/main/java/dev/zxq5/fantasysmp/items/Witherite.java b/src/main/java/dev/zxq5/fantasysmp/items/Witherite.java index 7b0326b..804b16f 100644 --- a/src/main/java/dev/zxq5/fantasysmp/items/Witherite.java +++ b/src/main/java/dev/zxq5/fantasysmp/items/Witherite.java @@ -1,12 +1,19 @@ package dev.zxq5.fantasysmp.items; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.command.CommandExecutor; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.RecipeChoice; +import org.bukkit.inventory.SmithingTransformRecipe; import org.bukkit.potion.PotionEffect; +import static org.bukkit.Bukkit.getServer; + public class Witherite extends GenericGearSet implements Listener, CommandExecutor { @Override @@ -26,6 +33,39 @@ public class Witherite extends GenericGearSet implements Listener, CommandExecut return true; } + @Override + public void registerRecipes() { + ItemStack sword = this.getSword(); + NamespacedKey swordKey = new NamespacedKey("fantasysmp.items", "witherite_sword"); + RecipeChoice swordChoice = new RecipeChoice.MaterialChoice(Material.NETHERITE_SWORD); + SmithingTransformRecipe swordRecipe = new SmithingTransformRecipe(swordKey, sword, NETHERITE_UPGRADE, WITHER_SKULL, swordChoice); + getServer().addRecipe(swordRecipe); + + ItemStack helmet = this.getHelmet(); + NamespacedKey helmetKey = new NamespacedKey("fantasysmp.items", "witherite_helmet"); + RecipeChoice helmetChoice = new RecipeChoice.MaterialChoice(Material.NETHERITE_HELMET); + SmithingTransformRecipe helmetRecipe = new SmithingTransformRecipe(helmetKey, helmet, NETHERITE_UPGRADE, WITHER_SKULL, helmetChoice); + getServer().addRecipe(helmetRecipe); + + ItemStack chestplate = this.getChestplate(); + NamespacedKey chestplateKey = new NamespacedKey("fantasysmp.items", "witherite_chestplate"); + RecipeChoice chestplateChoice = new RecipeChoice.MaterialChoice(Material.NETHERITE_CHESTPLATE); + SmithingTransformRecipe chestplateRecipe = new SmithingTransformRecipe(chestplateKey, chestplate, NETHERITE_UPGRADE, WITHER_SKULL, chestplateChoice); + getServer().addRecipe(chestplateRecipe); + + ItemStack leggings = this.getLeggings(); + NamespacedKey leggingsKey = new NamespacedKey("fantasysmp.items", "witherite_leggings"); + RecipeChoice leggingsChoice = new RecipeChoice.MaterialChoice(Material.NETHERITE_LEGGINGS); + SmithingTransformRecipe leggingsRecipe = new SmithingTransformRecipe(leggingsKey, leggings, NETHERITE_UPGRADE, WITHER_SKULL, leggingsChoice); + getServer().addRecipe(leggingsRecipe); + + ItemStack boots = this.getBoots(); + NamespacedKey bootsKey = new NamespacedKey("fantasysmp.items", "witherite_boots"); + RecipeChoice bootsChoice = new RecipeChoice.MaterialChoice(Material.NETHERITE_BOOTS); + SmithingTransformRecipe bootsRecipe = new SmithingTransformRecipe(bootsKey, boots, NETHERITE_UPGRADE, WITHER_SKULL, bootsChoice); + getServer().addRecipe(bootsRecipe); + } + @EventHandler public void onEntityDamageByEntity(org.bukkit.event.entity.EntityDamageByEntityEvent event) { diff --git a/src/main/java/dev/zxq5/fantasysmp/warps/Warp.java b/src/main/java/dev/zxq5/fantasysmp/warps/Warp.java index a447cc3..54ae113 100644 --- a/src/main/java/dev/zxq5/fantasysmp/warps/Warp.java +++ b/src/main/java/dev/zxq5/fantasysmp/warps/Warp.java @@ -2,16 +2,22 @@ package dev.zxq5.fantasysmp.warps; import com.google.gson.Gson; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.command.CommandExecutor; import org.bukkit.entity.Player; import java.io.*; import java.util.UUID; +import static org.bukkit.Bukkit.broadcastMessage; import static org.bukkit.Bukkit.getServer; public class Warp { + + private static Warp[] warps; + + private String name; private String setby; private String world; private WarpType type; @@ -20,33 +26,31 @@ public class Warp { private int z; public boolean execute(Player player) { - Location location = new Location(player.getWorld(), this.x, this.y, this.z); + Location location = new Location(getServer().getWorld(world), this.x, this.y, this.z); location.setPitch(player.getLocation().getPitch()); location.setYaw(player.getLocation().getYaw()); player.teleport(location); return true; } - public static Warp[] getWarps() throws Exception { + public static void loadWarps() throws Exception { File dataFolder = getServer().getPluginManager().getPlugin("Fantasysmp").getDataFolder(); File file = new File(dataFolder, "warps.json"); if (!file.exists()) { - init(); + createWarpFile(); } BufferedReader reader; reader = new BufferedReader(new FileReader(file)); Gson gson = new Gson(); - Warp[] warps = gson.fromJson(reader, Warp[].class); + warps = gson.fromJson(reader, Warp[].class); reader.close(); - - return warps; } - public static void saveWarps(Warp[] warps) throws Exception { + public static void saveWarps() throws Exception { File dataFolder = getServer().getPluginManager().getPlugin("Fantasysmp").getDataFolder(); File file = new File(dataFolder, "warps.json"); @@ -57,30 +61,7 @@ public class Warp { writer.close(); } - public static Warp getHome(UUID uuid) { - String uuidString = uuid.toString(); - - Warp[] warps; - try { - warps = getWarps(); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - - for (Warp warp : warps) { - if (warp.setby.equals(uuidString)) { - if (warp.type.equals(WarpType.HOME)) { - return warp; - } - return warp; - } - } - - return null; - } - - public static void init() { + public static void createWarpFile() { File dataFolder = getServer().getPluginManager().getPlugin("Fantasysmp").getDataFolder(); File file = new File(dataFolder, "warps.json"); @@ -98,52 +79,73 @@ public class Warp { } } - public static void setHome(Player player) { - String uuid = player.getUniqueId().toString(); + public static Warp getHome(UUID uuid) { + String uuidString = uuid.toString(); - Warp[] warps; - try { - warps = getWarps(); - } catch (Exception e) { - e.printStackTrace(); - return; + for (Warp warp : warps) { + if (warp.setby.equals(uuidString)) { + if (warp.type.equals(WarpType.HOME)) { + return warp; + } + } } - int x, y, z; - x = (int) player.getLocation().getX(); - y = (int) player.getLocation().getY(); - z = (int) player.getLocation().getZ(); + return null; + } + + public static Warp getWarp(String name) { + for (Warp warp : warps) { + if (warp.type.equals(WarpType.PUBLIC)) { + if (warp.name.equals(name)) { + return warp; + } + } + } + return null; + } + + public static void setHome(Player player) { + setWarp("Home (" + player.getName() + ")", player, WarpType.HOME); + } + + public static void setWarp(String name, Player player, WarpType type) { + String uuid = player.getUniqueId().toString(); + Location location = player.getLocation(); + + int x = (int) location.getX(); + int y = (int) location.getY(); + int z = (int) location.getZ(); Warp warp = null; for (Warp w : warps) { - if (w.setby.equals(uuid)) { + if (w.name.equals(name)) { warp = w; break; } } if (warp == null) { + Warp[] tempWarps = new Warp[warps.length + 1]; + System.arraycopy(warps, 0, tempWarps, 0, tempWarps.length - 1); + warps = tempWarps; warp = new Warp(); + warps[warps.length - 1] = warp; } - warp.type = WarpType.HOME; + warp.type = type; warp.setby = uuid; warp.world = player.getWorld().getName(); warp.x = x; warp.y = y; warp.z = z; + warp.name = name; - warps = new Warp[warps.length + 1]; - System.arraycopy(warps, 0, warps, 0, warps.length - 1); - warps[warps.length - 1] = warp; try { - saveWarps(warps); + saveWarps(); } catch (Exception e) { e.printStackTrace(); } - - return; } } @@ -152,4 +154,17 @@ enum WarpType { PUBLIC, DEATH, TEAM, -} \ No newline at end of file +} + + + + + + + + + + + + + diff --git a/src/main/java/dev/zxq5/fantasysmp/warps/Warper.java b/src/main/java/dev/zxq5/fantasysmp/warps/Warper.java index 9ea789e..cb26029 100644 --- a/src/main/java/dev/zxq5/fantasysmp/warps/Warper.java +++ b/src/main/java/dev/zxq5/fantasysmp/warps/Warper.java @@ -10,6 +10,16 @@ public class Warper implements CommandExecutor { return false; } + if (command.getName().equals("reloadwarps")) { + // reply + try { + Warp.loadWarps(); + } catch (Exception e) { + player.sendMessage("Failed to reload warps. please contact an admin."); + e.printStackTrace(); + } + } + if (command.getName().equals("home")) { if (args.length != 0) { player.sendMessage("Usage: /home"); @@ -32,6 +42,32 @@ public class Warper implements CommandExecutor { Warp.setHome(player); } + if (command.getName().equals("warp")) { + if (args.length != 1) { + player.sendMessage("Usage: /warp "); + return true; + } + + Warp warp = Warp.getWarp(args[0]); + + if (warp != null) { + warp.execute(player); + } + } + + if (command.getName().equals("setwarp")) { + if (!(player.hasPermission("fantasysmp.manage_warps"))) { + player.sendMessage("You do not have permission to use this command."); + return false; + } + + if (args.length != 1) { + player.sendMessage("Usage: /setwarp "); + return true; + } + + Warp.setWarp(args[0], player, WarpType.PUBLIC); + } return true; } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index bc065f6..0ad9d85 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: fantasysmp -version: '1.0-SNAPSHOT' +version: '1.1-SNAPSHOT' main: dev.zxq5.fantasysmp.Fantasysmp api-version: '1.21' authors: [ zxq5 ] @@ -10,6 +10,7 @@ commands: items: description: create custom items from plugin usage: /items + permission: fantasysmp.admin home: description: teleport to your home @@ -21,6 +22,22 @@ commands: usage: /sethome permission: fantasysmp.home + setwarp: + description: set a warp + usage: /setwarp + permission: fantasysmp.manage_warps + + reloadwarps: + description: reload warps + usage: /reloadwarps + permission: fantasysmp.admin + + warp: + description: teleport to a warp + usage: /warp + permission: fantasysmp.warps + + permissions: fantasysmp.admin: description: Manage plugin @@ -33,12 +50,12 @@ permissions: default: false fantasysmp.warps: - description: create custom warps from plugin - default: false + description: teleport to custom warps + default: true fantasysmp.manage_warps: description: commands for managing warps - default: false + default: true fantasysmp.home: description: commands for teleporting home