From 2bb4d3b57fa7aacf14c075f7d415ed8a067350cf Mon Sep 17 00:00:00 2001 From: loganb1max Date: Sat, 22 Jun 2019 15:49:39 -0500 Subject: [PATCH] Almost done. Todo: - Write Logic for each minion - Implement ReFueling - Implement Chest/Block linking - Implement Upgrading --- .../me/loganb1max/minions/MinionsPlugin.java | 5 + .../minions/command/MinionCommand.java | 116 ++++++++ .../minions/manager/MinionManager.java | 60 ++++- .../loganb1max/minions/menu/MinionMenu.java | 35 ++- .../minions/model/AbstractMinion.java | 99 ------- .../me/loganb1max/minions/model/Fuel.java | 26 ++ .../me/loganb1max/minions/model/Minion.java | 254 +++++++++++++++--- .../minions/model/minions/FarmerMinion.java | 100 ++----- .../minions/model/minions/FisherMinion.java | 103 ++----- .../minions/model/minions/MinerMinion.java | 100 ++----- .../loganb1max/minions/util/ItemBuilder.java | 5 + .../me/loganb1max/minions/util/ItemUtil.java | 37 +-- .../me/loganb1max/minions/util/Replacer.java | 9 + src/main/resources/config.yml | 9 + 14 files changed, 558 insertions(+), 400 deletions(-) create mode 100644 src/main/java/me/loganb1max/minions/command/MinionCommand.java delete mode 100644 src/main/java/me/loganb1max/minions/model/AbstractMinion.java create mode 100644 src/main/java/me/loganb1max/minions/model/Fuel.java diff --git a/src/main/java/me/loganb1max/minions/MinionsPlugin.java b/src/main/java/me/loganb1max/minions/MinionsPlugin.java index f4dd132..a589b14 100644 --- a/src/main/java/me/loganb1max/minions/MinionsPlugin.java +++ b/src/main/java/me/loganb1max/minions/MinionsPlugin.java @@ -1,6 +1,7 @@ package me.loganb1max.minions; import lombok.Getter; +import me.loganb1max.minions.command.MinionCommand; import me.loganb1max.minions.manager.MinionManager; import org.bukkit.plugin.java.JavaPlugin; @@ -8,11 +9,15 @@ public class MinionsPlugin extends JavaPlugin { @Getter private static MinionsPlugin instance; @Getter private MinionManager minionManager; + private MinionCommand minionCommand; @Override public void onEnable() { saveDefaultConfig(); this.minionManager = new MinionManager(this); + this.minionCommand = new MinionCommand(); + getCommand("minions").setExecutor(minionCommand); + getCommand("minions").setTabCompleter(minionCommand); } @Override diff --git a/src/main/java/me/loganb1max/minions/command/MinionCommand.java b/src/main/java/me/loganb1max/minions/command/MinionCommand.java new file mode 100644 index 0000000..dfe3a5a --- /dev/null +++ b/src/main/java/me/loganb1max/minions/command/MinionCommand.java @@ -0,0 +1,116 @@ +package me.loganb1max.minions.command; + +import com.google.common.collect.ImmutableList; +import me.loganb1max.minions.model.Minion; +import me.loganb1max.minions.model.minions.FarmerMinion; +import me.loganb1max.minions.model.minions.FisherMinion; +import me.loganb1max.minions.model.minions.MinerMinion; +import me.loganb1max.minions.util.ItemUtil; +import me.loganb1max.minions.util.Text; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class MinionCommand implements CommandExecutor, TabCompleter { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + if (!sender.hasPermission("minions.command")) { + sender.sendMessage(Text.color("&cYou don't have permission for this command.")); + return true; + } + + if (args.length < 1) { + sendHelp(sender); + return true; + } + + if (args.length == 1) { + if (args[0].equalsIgnoreCase("reload")) { + + } else { + sendHelp(sender); + } + } + + if (args.length >= 3) { + if (!args[0].equalsIgnoreCase("give")) { + sendHelp(sender); + return true; + } + + final Player target = Bukkit.getPlayer(args[1]); + if (target == null) { + sender.sendMessage(Text.color("&cPlayer Not Found.")); + return true; + } + + Minion minion = null; + + final String type = args[2].toLowerCase(); + switch (type) { + case "miner": + minion = new MinerMinion(target); + break; + case "farmer": + minion = new FarmerMinion(target); + break; + case "fisher": + minion = new FisherMinion(target); + break; + } + + if (minion == null) { + sender.sendMessage(Text.color("&cInvalid Minion Type.")); + return true; + } + + ItemUtil.giveAndDrop(target, minion.toItemStack()); + target.sendMessage(Text.color("&aYou have received a " + minion.getName())); + sender.sendMessage(Text.color("&aYou gave " + target.getName() + " a " + minion.getName())); + + } + + return true; + } + + @Override + public List onTabComplete(CommandSender commandSender, Command command, String label, String[] args) { + List toReturn = new ArrayList<>(); + switch (args.length) { + case 0: + toReturn = ImmutableList.of( + "give", + "reload" + ); + break; + case 1: + toReturn = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); + break; + case 2: + toReturn = ImmutableList.of( + "miner", + "farmer", + "fisher" + ); + break; + } + return toReturn; + } + + private void sendHelp(final CommandSender sender) { + sender.sendMessage(Text.color("&7============================================")); + sender.sendMessage(Text.color("&6 Minions Help Menu")); + sender.sendMessage(Text.color("&7============================================")); + sender.sendMessage(Text.color("&6 /minions give ")); + sender.sendMessage(Text.color("&6 /minions reload")); + } + +} diff --git a/src/main/java/me/loganb1max/minions/manager/MinionManager.java b/src/main/java/me/loganb1max/minions/manager/MinionManager.java index 988fd90..7045a05 100644 --- a/src/main/java/me/loganb1max/minions/manager/MinionManager.java +++ b/src/main/java/me/loganb1max/minions/manager/MinionManager.java @@ -1,5 +1,6 @@ package me.loganb1max.minions.manager; +import com.google.gson.*; import lombok.Getter; import me.loganb1max.minions.MinionsPlugin; import me.loganb1max.minions.menu.MinionMenu; @@ -14,6 +15,12 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.player.PlayerArmorStandManipulateEvent; import org.bukkit.scheduler.BukkitTask; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -21,19 +28,66 @@ import java.util.Set; public class MinionManager implements Listener { private final MinionsPlugin plugin; - private BukkitTask animationTask; + private final BukkitTask animationTask; + private final BukkitTask tickTask; @Getter private Set minions = new HashSet<>(); public MinionManager(final MinionsPlugin plugin) { this.plugin = plugin; - this.animationTask = Bukkit.getScheduler().runTaskTimer(this.plugin, () -> this.minions.forEach(Minion::tick), 10, 10); + this.animationTask = Bukkit.getScheduler().runTaskTimer(this.plugin, () -> this.minions.forEach(Minion::animationTick), 10, 10); + this.tickTask = Bukkit.getScheduler().runTaskTimer(this.plugin, () -> this.minions.forEach(Minion::tick), 20, 20); Bukkit.getPluginManager().registerEvents(this, this.plugin); + loadMinions(); } public void close() { this.animationTask.cancel(); + this.tickTask.cancel(); HandlerList.unregisterAll(this); + saveMinions(); + } + + private void loadMinions() { + final File dataFile = new File(this.plugin.getDataFolder(), "minions.json"); + final Gson gson = new Gson(); + try (final BufferedReader reader = Files.newBufferedReader(dataFile.toPath(), StandardCharsets.UTF_8)) { + JsonObject data = gson.fromJson(reader, JsonObject.class); + JsonArray minions = data.get("minions").getAsJsonArray(); + for (JsonElement el : minions) { + try { + Minion minion = Minion.deserialize(this.plugin, el); + this.minions.add(minion); + } catch (Exception e) { + this.plugin.getLogger().severe("Failed to load minion."); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void saveMinions() { + final File dataFile = new File(this.plugin.getDataFolder(), "minions.json"); + final Gson gson = new Gson(); + try (final BufferedWriter writer = Files.newBufferedWriter(dataFile.toPath(), StandardCharsets.UTF_8)) { + JsonObject data = new JsonObject(); + JsonArray minions = new JsonArray(); + + for (Minion minion : this.minions) { + try { + minions.add(minion.serialize()); + } catch (Exception e) { + this.plugin.getLogger().severe("Failed to save minion: " + minion.getOwnerName() + " : " + minion.getName()); + e.printStackTrace(); + } + } + + data.add("minions", minions); + writer.write(gson.toJson(data)); + } catch (IOException e) { + e.printStackTrace(); + } } @EventHandler(priority = EventPriority.HIGHEST) @@ -53,7 +107,7 @@ public class MinionManager implements Listener { return; } if (e.getPlayer().isSneaking()) { - minion.pickup(e.getPlayer()); + minion.despawn(e.getPlayer()); return; } new MinionMenu(minion, e.getPlayer()); diff --git a/src/main/java/me/loganb1max/minions/menu/MinionMenu.java b/src/main/java/me/loganb1max/minions/menu/MinionMenu.java index 4ea0b8c..7f502b7 100644 --- a/src/main/java/me/loganb1max/minions/menu/MinionMenu.java +++ b/src/main/java/me/loganb1max/minions/menu/MinionMenu.java @@ -33,28 +33,39 @@ public class MinionMenu implements Listener { this.inventory = Bukkit.createInventory(this.player, 9, "Minion Menu"); bindItem( - ItemBuilder.of(Material.CHEST) - .name("&6Link Chest") - .lore("&7click to link a deposit chest.") - .build(), - 2, + minion.getIcon(), + 1, + clickType -> { } + ); + + bindItem( + ItemBuilder.of(Material.GOLD_INGOT) + .name("&6Upgrade") + .lore("&7click to upgrade this minion.") + .build(), + 3, clickType -> { } ); bindItem( - minion.getIcon(), - 4, - clickType -> { } + ItemBuilder.of(Material.COAL) + .name("&6Deposit Energy") + .lore("") + .build(), + 5, + clickType -> { + + } ); bindItem( - ItemBuilder.of(Material.GOLD_INGOT) - .name("&6Upgrade") - .lore("&7click to upgrade this minion.") + ItemBuilder.of(Material.CHEST) + .name("&6Link Chest") + .lore("&7click to link a deposit chest.") .build(), - 6, + 7, clickType -> { } diff --git a/src/main/java/me/loganb1max/minions/model/AbstractMinion.java b/src/main/java/me/loganb1max/minions/model/AbstractMinion.java deleted file mode 100644 index 90bb434..0000000 --- a/src/main/java/me/loganb1max/minions/model/AbstractMinion.java +++ /dev/null @@ -1,99 +0,0 @@ -package me.loganb1max.minions.model; - -import com.google.common.collect.ImmutableSet; -import lombok.Getter; -import me.loganb1max.minions.MinionsPlugin; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import java.util.Set; -import java.util.UUID; - -@Getter -public abstract class AbstractMinion implements Minion { - - private final UUID owner; - private final String ownerName; - private Location location; - private double energy; - private int level; - private Set linkedBlocks; - private ArmorStand stand; - - private double offset = 0.1; - - public AbstractMinion(final Player owner) { - this.owner = owner.getUniqueId(); - this.ownerName = owner.getName(); - } - - public void spawn(final Location location) { - this.location = location; - if (this.stand == null) { - this.stand = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND); - this.stand.setSmall(true); - this.stand.setBasePlate(false); - this.stand.setGravity(false); - this.stand.setCanPickupItems(false); - this.stand.setRemoveWhenFarAway(false); - this.stand.setMarker(false); - this.equipEquipment(this.stand); - MinionsPlugin.getInstance().getMinionManager().getMinions().add(this); - } else { - this.stand.teleport(location); - } - } - - public void despawn(final Player player) { - if (this.stand == null) return; - this.location = null; - this.stand.remove(); - this.stand = null; - this.linkedBlocks.clear(); - player.getInventory().addItem(this.toItemStack()).forEach((integer, itemStack) -> player.getWorld().dropItemNaturally(player.getLocation(), itemStack)); - } - - @Override - public void pickup(Player player) { - this.despawn(player); - MinionsPlugin.getInstance().getMinionManager().getMinions().remove(this); - } - - @Override - public Set getLinkedBlocks() { - return ImmutableSet.copyOf(this.linkedBlocks); - } - - @Override - public double gainEnergy(final double amount) { - return this.energy += amount; - } - - @Override - public double consumeEnergy(final double amount) { - return this.energy -= amount; - } - - @Override - public UUID getId() { - if (this.stand == null) return null; - return this.stand.getUniqueId(); - } - - @Override - public void tick() { - if (this.stand == null) return; - if (this.stand.getRightArmPose().getX() > 300) offset = -0.1; - if (this.stand.getRightArmPose().getX() < 230) offset = 0.1; - this.stand.setRightArmPose(this.stand.getRightArmPose().add(offset, 0, 0)); - this.stand.setFireTicks(0); - } - - public abstract ItemStack toItemStack(); - - public abstract void equipEquipment(final ArmorStand stand); - -} diff --git a/src/main/java/me/loganb1max/minions/model/Fuel.java b/src/main/java/me/loganb1max/minions/model/Fuel.java new file mode 100644 index 0000000..456e52c --- /dev/null +++ b/src/main/java/me/loganb1max/minions/model/Fuel.java @@ -0,0 +1,26 @@ +package me.loganb1max.minions.model; + +import lombok.AllArgsConstructor; +import me.loganb1max.minions.util.ItemUtil; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; + +@AllArgsConstructor +public class Fuel { + + private final String name; + private final ItemStack itemStack; + private final double energy; + + public boolean isEqual(final ItemStack item) { + return ItemUtil.compareType(this.itemStack, item); + } + + public static Fuel fromConfigSection(final ConfigurationSection section) { + final String name = section.getName(); + final double energy = section.getDouble("Energy"); + final ItemStack item = ItemUtil.fromConfigSection(section.getConfigurationSection("Item")); + return new Fuel(name, item, energy); + } + +} diff --git a/src/main/java/me/loganb1max/minions/model/Minion.java b/src/main/java/me/loganb1max/minions/model/Minion.java index 10f5d5e..9f90418 100644 --- a/src/main/java/me/loganb1max/minions/model/Minion.java +++ b/src/main/java/me/loganb1max/minions/model/Minion.java @@ -1,43 +1,227 @@ package me.loganb1max.minions.model; +import com.google.common.collect.ImmutableSet; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import me.loganb1max.minions.MinionsPlugin; +import me.loganb1max.minions.model.minions.FarmerMinion; +import me.loganb1max.minions.model.minions.FisherMinion; +import me.loganb1max.minions.model.minions.MinerMinion; +import me.loganb1max.minions.util.*; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; - -import java.util.Set; -import java.util.UUID; - -public interface Minion { - - String getName(); - - UUID getId(); - - UUID getOwner(); - - String getOwnerName(); - - Location getLocation(); - - double getEnergy(); - - int getLevel(); - - Set getLinkedBlocks(); - - double gainEnergy(final double amount); - - double consumeEnergy(final double amount); - - void spawn(final Location location); - - void despawn(final Player player); - - ItemStack getIcon(); - - void tick(); - - void pickup(final Player player); +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Getter +public abstract class Minion { + + private final ConfigurationSection section; + private final Replacer replacer; + private final UUID owner; + private final String ownerName; + @Setter(AccessLevel.PROTECTED) private Location location; + @Setter(AccessLevel.PROTECTED) private double energy; + @Setter(AccessLevel.PROTECTED) private int level; + @Setter(AccessLevel.PROTECTED)private Set linkedBlocks; + private ArmorStand stand; + + @Getter(AccessLevel.NONE) + private double offset = 0.1; + + public Minion(final ConfigurationSection section, final UUID owner, final String ownerName) { + this.section = section; + this.owner = owner; + this.ownerName = ownerName; + this.replacer = Replacer.create() + .add("%level%", String.valueOf(getLevel())) + .add("%energy%", String.valueOf(getEnergy())) + .add("%owner%", getOwnerName()); + } + + protected void findStand() { + if (this.stand != null) return; + this.location.getWorld().getNearbyEntities(this.location, 0.5, 0.5, 0.5).stream().filter(entity -> entity instanceof ArmorStand && !entity.getCustomName().isEmpty()).findFirst().ifPresent(entity -> { + this.stand = (ArmorStand) entity; + }); + } + + public void spawn(final Location location) { + this.location = location; + if (this.stand == null) { + this.stand = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND); + this.stand.setSmall(true); + this.stand.setBasePlate(false); + this.stand.setGravity(false); + this.stand.setCanPickupItems(false); + this.stand.setRemoveWhenFarAway(false); + this.stand.setMarker(false); + this.stand.setCustomName(Text.color(getName())); + this.stand.setCustomNameVisible(true); + this.equipEquipment(this.stand); + MinionsPlugin.getInstance().getMinionManager().getMinions().add(this); + } else { + this.stand.teleport(location); + } + } + + public void despawn(final Player player) { + if (this.stand == null) return; + this.location = null; + this.stand.remove(); + this.stand = null; + this.linkedBlocks.clear(); + player.getInventory().addItem(this.toItemStack()).forEach((integer, itemStack) -> player.getWorld().dropItemNaturally(player.getLocation(), itemStack)); + MinionsPlugin.getInstance().getMinionManager().getMinions().remove(this); + } + + public void animationTick() { + if (this.stand == null) return; + if (this.stand.getRightArmPose().getX() > 300) offset = -0.1; + if (this.stand.getRightArmPose().getX() < 230) offset = 0.1; + this.stand.setRightArmPose(this.stand.getRightArmPose().add(offset, 0, 0)); + this.stand.setFireTicks(0); + } + + public double gainEnergy(final double amount) { + return this.energy += amount; + } + + public double consumeEnergy(final double amount) { + return this.energy -= amount; + } + + public void equipEquipment(final ArmorStand stand) { + final String skull = this.section.getString("Equipment.Skull", ""); + ItemStack skullItem = SkullCreator.itemFromBase64(skull); + stand.setHelmet(skullItem); + final ItemStack chestplate = ItemUtil.fromConfigSection(this.section.getConfigurationSection("Equipment.Chestplate")); + stand.setChestplate(chestplate); + final ItemStack leggings = ItemUtil.fromConfigSection(this.section.getConfigurationSection("Equipment.Leggings")); + stand.setLeggings(leggings); + final ItemStack boots = ItemUtil.fromConfigSection(this.section.getConfigurationSection("Equipment.Boots")); + stand.setBoots(boots); + final ItemStack rightHand = ItemUtil.fromConfigSection(this.section.getConfigurationSection("Equipment.RightHand")); + stand.setItemInHand(rightHand); + } + + public ItemStack toItemStack() { + ItemStack item = getIcon(); + item = ItemUtil.setKeyString(item, "minion-owner", this.owner.toString()); + item = ItemUtil.setKeyString(item, "minion-owner-name", this.ownerName); + item = ItemUtil.setKeyInt(item, "minion-level", this.level); + item = ItemUtil.setKeyDouble(item, "minion-energy", this.energy); + return item; + } + + public ItemStack getIcon() { + final Material material = Material.getMaterial(this.section.getString("Item.Material")); + final int durability = this.section.getInt("Item.Durability"); + final String skull = this.section.getString("Item.Skull", null); + final String displayName = this.section.getString("Item.DisplayName"); + final List lore = this.section.getStringList("Item.Lore"); + ItemStack item = ItemBuilder.of(material) + .durability((short) durability) + .name(displayName) + .lore(this.replacer.replace(lore)) + .build(); + if (material == Material.SKULL_ITEM && skull != null) { + item = SkullCreator.itemWithBase64(item, skull); + } + return item; + } + + public Set getLinkedBlocks() { + return ImmutableSet.copyOf(this.linkedBlocks); + } + + public UUID getId() { + if (this.stand == null) return null; + return this.stand.getUniqueId(); + } + + public String getType() { + return this.section.getName(); + } + + public String getName() { + return this.section.getString("Name"); + } + + public double getEnergyPerUse(final int level) { + return this.section.getDouble("Levels." + level + ".EnergyPerAction", 0.0); + } + + public double getPrice(final int level) { + return this.section.getDouble("Levels." + level + ".Price", 0.0); + } + + public JsonElement serialize() { + final Gson gson = new Gson(); + final JsonObject object = new JsonObject(); + object.addProperty("type", getType()); + object.addProperty("owner", this.owner.toString()); + object.addProperty("ownerName", this.ownerName); + final JsonObject loc = new JsonObject(); + loc.addProperty("world", this.location.getWorld().getName()); + loc.addProperty("x", this.location.getX()); + loc.addProperty("y", this.location.getY()); + loc.addProperty("z", this.location.getZ()); + object.add("location", loc); + object.addProperty("energy", this.energy); + object.addProperty("level", this.level); + final List linked = new ArrayList<>(); + this.linkedBlocks.forEach(block -> { + JsonObject b = new JsonObject(); + b.addProperty("world", block.getWorld().getName()); + b.addProperty("x", block.getX()); + b.addProperty("y", block.getY()); + b.addProperty("z", block.getZ()); + linked.add(b); + }); + object.add("linkedBlocks", gson.toJsonTree(linked)); + return object; + } + + public static Minion deserialize(final MinionsPlugin plugin, final JsonElement element) { + final JsonObject data = element.getAsJsonObject(); + final String type = data.get("type").getAsString(); + final UUID owner = UUID.fromString(data.get("owner").getAsString()); + final String ownerName = data.get("ownerName").getAsString(); + final JsonObject locData = data.get("location").getAsJsonObject(); + final Location location = new Location(Bukkit.getWorld(locData.get("world").getAsString()), locData.get("x").getAsDouble(), locData.get("y").getAsDouble(), locData.get("z").getAsDouble()); + final double energy = data.get("energy").getAsDouble(); + final int level = data.get("level").getAsInt(); + final Set linkedBlocks = Stream.of(data.get("linkedBlocks").getAsJsonArray()) + .map(JsonElement::getAsJsonObject) + .map(jsonObject -> Bukkit.getWorld(jsonObject.get("world").getAsString()).getBlockAt(jsonObject.get("x").getAsInt(), jsonObject.get("y").getAsInt(), jsonObject.get("z").getAsInt())).collect(Collectors.toSet()); + Minion toReturn = null; + switch (type) { + case "Miner": + toReturn = new MinerMinion(owner, ownerName, location, energy, level, linkedBlocks); + break; + case "Farmer": + toReturn = new FarmerMinion(owner, ownerName, location, energy, level, linkedBlocks); + break; + case "Fisher": + toReturn = new FisherMinion(owner, ownerName, location, energy, level, linkedBlocks); + break; + } + return toReturn; + } + + public abstract void tick(); } diff --git a/src/main/java/me/loganb1max/minions/model/minions/FarmerMinion.java b/src/main/java/me/loganb1max/minions/model/minions/FarmerMinion.java index 5d6e6ef..905b977 100644 --- a/src/main/java/me/loganb1max/minions/model/minions/FarmerMinion.java +++ b/src/main/java/me/loganb1max/minions/model/minions/FarmerMinion.java @@ -1,93 +1,39 @@ package me.loganb1max.minions.model.minions; import me.loganb1max.minions.MinionsPlugin; -import me.loganb1max.minions.model.AbstractMinion; -import me.loganb1max.minions.util.ItemUtil; -import me.loganb1max.minions.util.Replacer; -import me.loganb1max.minions.util.SkullCreator; -import me.loganb1max.minions.util.Text; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import java.util.List; -import java.util.stream.Collectors; +import me.loganb1max.minions.model.Minion; +import org.bukkit.Location; +import org.bukkit.block.Block; +import java.util.Set; +import java.util.UUID; -public class FarmerMinion extends AbstractMinion { +public class FarmerMinion extends Minion { - private final ConfigurationSection section; - - public FarmerMinion(final Player owner) { - super(owner); - this.section = MinionsPlugin.getInstance().getConfig().getConfigurationSection("Farmer"); + public FarmerMinion(final UUID owner, final String ownerName) { + super( + MinionsPlugin.getInstance().getConfig().getConfigurationSection("Farmer"), + owner, + ownerName + ); + getReplacer().add("%radius%", String.valueOf(getRadius(getLevel()))); } - @Override - public ItemStack getIcon() { - final Material material = Material.getMaterial(section.getString("Item.Material")); - final int durability = section.getInt("Item.Durability"); - final String skull = section.getString("Item.Skull", null); - final String displayName = section.getString("Item.DisplayName"); - final List lore = section.getStringList("Item.Lore"); - final Replacer replacer = Replacer.create() - .add("%level%", String.valueOf(getLevel())) - .add("%energy%", String.valueOf(getEnergy())) - .add("%owner%", getOwnerName()) - .add("%radius%", String.valueOf(getRadius(getLevel()))); - ItemStack item = new ItemStack(material, 1); - item.setDurability((short) durability); - if (material == Material.SKULL_ITEM && skull != null) { - item = SkullCreator.itemWithBase64(item, skull); - } - final ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Text.color(displayName)); - meta.setLore(lore.stream().map(Text::color).map(replacer::replace).collect(Collectors.toList())); - item.setItemMeta(meta); - return item; - } - - @Override - public ItemStack toItemStack() { - ItemStack item = getIcon(); - item = ItemUtil.setKeyString(item, "minion-owner", getOwner().toString()); - item = ItemUtil.setKeyString(item, "minion-owner-name", getOwnerName()); - item = ItemUtil.setKeyInt(item, "minion-level", getLevel()); - item = ItemUtil.setKeyDouble(item, "minion-energy", getEnergy()); - return item; - } - - @Override - public void equipEquipment(ArmorStand stand) { - final String skull = section.getString("Equipment.Skull", ""); - ItemStack skullItem = SkullCreator.itemFromBase64(skull); - stand.setHelmet(skullItem); - final ItemStack chestplate = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Chestplate")); - stand.setChestplate(chestplate); - final ItemStack leggings = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Leggings")); - stand.setLeggings(leggings); - final ItemStack boots = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Boots")); - stand.setBoots(boots); - final ItemStack rightHand = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.RightHand")); - stand.setItemInHand(rightHand); + public FarmerMinion(final UUID owner, final String ownerName, final Location location, final double energy, final int level, final Set linkedBlocks) { + this(owner, ownerName); + setLocation(location); + setEnergy(energy); + setLevel(level); + setLinkedBlocks(linkedBlocks); + findStand(); } public int getRadius(final int level) { - return section.getInt("Levels." + level + ".Radius", 1); - } - - public double getPrice(final int level) { - return section.getDouble("Levels." + level + ".Price", 0.0); - } - - public double getEnergyPerUse(final int level) { - return section.getDouble("Levels." + level + ".EnergyPerAction", 0.0); + return getSection().getInt("Levels." + level + ".Radius", 1); } @Override - public String getName() { - return section.getString("Name"); + public void tick() { + } } diff --git a/src/main/java/me/loganb1max/minions/model/minions/FisherMinion.java b/src/main/java/me/loganb1max/minions/model/minions/FisherMinion.java index 2bb9367..9a39730 100644 --- a/src/main/java/me/loganb1max/minions/model/minions/FisherMinion.java +++ b/src/main/java/me/loganb1max/minions/model/minions/FisherMinion.java @@ -1,98 +1,43 @@ package me.loganb1max.minions.model.minions; import me.loganb1max.minions.MinionsPlugin; -import me.loganb1max.minions.model.AbstractMinion; -import me.loganb1max.minions.util.ItemUtil; -import me.loganb1max.minions.util.Replacer; -import me.loganb1max.minions.util.SkullCreator; -import me.loganb1max.minions.util.Text; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import java.util.List; -import java.util.stream.Collectors; +import me.loganb1max.minions.model.Minion; +import org.bukkit.Location; +import org.bukkit.block.Block; +import java.util.Set; +import java.util.UUID; -public class FisherMinion extends AbstractMinion { +public class FisherMinion extends Minion { - private final ConfigurationSection section; - - public FisherMinion(final Player owner) { - super(owner); - this.section = MinionsPlugin.getInstance().getConfig().getConfigurationSection("Fisher"); + public FisherMinion(final UUID owner, final String ownerName) { + super( + MinionsPlugin.getInstance().getConfig().getConfigurationSection("Fisher"), + owner, + ownerName + ); + getReplacer().add("%radius%", String.valueOf(getRadius(getLevel()))).add("%chanceperblockpersecond%", String.valueOf(getChancePerBlockPerSecond(getLevel()))); } - @Override - public ItemStack getIcon() { - final Material material = Material.getMaterial(section.getString("Item.Material")); - final int durability = section.getInt("Item.Durability"); - final String skull = section.getString("Item.Skull", null); - final String displayName = section.getString("Item.DisplayName"); - final List lore = section.getStringList("Item.Lore"); - final Replacer replacer = Replacer.create() - .add("%level%", String.valueOf(getLevel())) - .add("%energy%", String.valueOf(getEnergy())) - .add("%owner%", getOwnerName()) - .add("%radius%", String.valueOf(getRadius(getLevel()))) - .add("%chanceperblockpersecond%", String.valueOf(getChancePerBlockPerSecond(getLevel()))); - ItemStack item = new ItemStack(material, 1); - item.setDurability((short) durability); - if (material == Material.SKULL_ITEM && skull != null) { - item = SkullCreator.itemWithBase64(item, skull); - } - final ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Text.color(displayName)); - meta.setLore(lore.stream().map(Text::color).map(replacer::replace).collect(Collectors.toList())); - item.setItemMeta(meta); - return item; - } - - @Override - public ItemStack toItemStack() { - ItemStack item = getIcon(); - item = ItemUtil.setKeyString(item, "minion-owner", getOwner().toString()); - item = ItemUtil.setKeyString(item, "minion-owner-name", getOwnerName()); - item = ItemUtil.setKeyInt(item, "minion-level", getLevel()); - item = ItemUtil.setKeyDouble(item, "minion-energy", getEnergy()); - return item; - } - - @Override - public void equipEquipment(ArmorStand stand) { - final String skull = section.getString("Equipment.Skull", ""); - ItemStack skullItem = SkullCreator.itemFromBase64(skull); - stand.setHelmet(skullItem); - final ItemStack chestplate = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Chestplate")); - stand.setChestplate(chestplate); - final ItemStack leggings = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Leggings")); - stand.setLeggings(leggings); - final ItemStack boots = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Boots")); - stand.setBoots(boots); - final ItemStack rightHand = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.RightHand")); - stand.setItemInHand(rightHand); + public FisherMinion(final UUID owner, final String ownerName, final Location location, final double energy, final int level, final Set linkedBlocks) { + this(owner, ownerName); + setLocation(location); + setEnergy(energy); + setLevel(level); + setLinkedBlocks(linkedBlocks); + findStand(); } public double getChancePerBlockPerSecond(final int level) { - return section.getDouble("Levels." + level + ".ChancePerBlockPerSecond", 0.0); + return getSection().getDouble("Levels." + level + ".ChancePerBlockPerSecond", 0.0); } public int getRadius(final int level) { - return section.getInt("Levels." + level + ".Radius", 1); - } - - public double getPrice(final int level) { - return section.getDouble("Levels." + level + ".Price", 0.0); - } - - public double getEnergyPerUse(final int level) { - return section.getDouble("Levels." + level + ".EnergyPerAction", 0.0); + return getSection().getInt("Levels." + level + ".Radius", 1); } @Override - public String getName() { - return section.getString("Name"); + public void tick() { + } } diff --git a/src/main/java/me/loganb1max/minions/model/minions/MinerMinion.java b/src/main/java/me/loganb1max/minions/model/minions/MinerMinion.java index 473dd45..fb86071 100644 --- a/src/main/java/me/loganb1max/minions/model/minions/MinerMinion.java +++ b/src/main/java/me/loganb1max/minions/model/minions/MinerMinion.java @@ -1,93 +1,39 @@ package me.loganb1max.minions.model.minions; import me.loganb1max.minions.MinionsPlugin; -import me.loganb1max.minions.model.AbstractMinion; -import me.loganb1max.minions.util.ItemUtil; -import me.loganb1max.minions.util.Replacer; -import me.loganb1max.minions.util.SkullCreator; -import me.loganb1max.minions.util.Text; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import java.util.List; -import java.util.stream.Collectors; +import me.loganb1max.minions.model.Minion; +import org.bukkit.Location; +import org.bukkit.block.Block; +import java.util.Set; +import java.util.UUID; -public class MinerMinion extends AbstractMinion { +public class MinerMinion extends Minion { - private final ConfigurationSection section; - - public MinerMinion(final Player owner) { - super(owner); - this.section = MinionsPlugin.getInstance().getConfig().getConfigurationSection("Miner"); + public MinerMinion(final UUID owner, final String ownerName) { + super( + MinionsPlugin.getInstance().getConfig().getConfigurationSection("Miner"), + owner, + ownerName + ); + getReplacer().add("%blocklimit%", String.valueOf(getBlockLimit(getLevel()))); } - @Override - public ItemStack getIcon() { - final Material material = Material.getMaterial(section.getString("Item.Material")); - final int durability = section.getInt("Item.Durability"); - final String skull = section.getString("Item.Skull", null); - final String displayName = section.getString("Item.DisplayName"); - final List lore = section.getStringList("Item.Lore"); - final Replacer replacer = Replacer.create() - .add("%level%", String.valueOf(getLevel())) - .add("%energy%", String.valueOf(getEnergy())) - .add("%owner%", getOwnerName()) - .add("%blocklimit%", String.valueOf(getBlockLimit(getLevel()))); - ItemStack item = new ItemStack(material, 1); - item.setDurability((short) durability); - if (material == Material.SKULL_ITEM && skull != null) { - item = SkullCreator.itemWithBase64(item, skull); - } - final ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Text.color(displayName)); - meta.setLore(lore.stream().map(Text::color).map(replacer::replace).collect(Collectors.toList())); - item.setItemMeta(meta); - return item; - } - - @Override - public ItemStack toItemStack() { - ItemStack item = getIcon(); - item = ItemUtil.setKeyString(item, "minion-owner", getOwner().toString()); - item = ItemUtil.setKeyString(item, "minion-owner-name", getOwnerName()); - item = ItemUtil.setKeyInt(item, "minion-level", getLevel()); - item = ItemUtil.setKeyDouble(item, "minion-energy", getEnergy()); - return item; - } - - @Override - public void equipEquipment(ArmorStand stand) { - final String skull = section.getString("Equipment.Skull", ""); - ItemStack skullItem = SkullCreator.itemFromBase64(skull); - stand.setHelmet(skullItem); - final ItemStack chestplate = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Chestplate")); - stand.setChestplate(chestplate); - final ItemStack leggings = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Leggings")); - stand.setLeggings(leggings); - final ItemStack boots = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Boots")); - stand.setBoots(boots); - final ItemStack rightHand = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.RightHand")); - stand.setItemInHand(rightHand); + public MinerMinion(final UUID owner, final String ownerName, final Location location, final double energy, final int level, final Set linkedBlocks) { + this(owner, ownerName); + setLocation(location); + setEnergy(energy); + setLevel(level); + setLinkedBlocks(linkedBlocks); + findStand(); } public int getBlockLimit(final int level) { - return section.getInt("Levels." + level + ".BlockLimit", 1); - } - - public double getPrice(final int level) { - return section.getDouble("Levels." + level + ".Price", 0.0); - } - - public double getEnergyPerUse(final int level) { - return section.getDouble("Levels." + level + ".EnergyPerAction", 0.0); + return getSection().getInt("Levels." + level + ".BlockLimit", 1); } @Override - public String getName() { - return section.getString("Name"); + public void tick() { + } } diff --git a/src/main/java/me/loganb1max/minions/util/ItemBuilder.java b/src/main/java/me/loganb1max/minions/util/ItemBuilder.java index 09576d7..8d1220b 100644 --- a/src/main/java/me/loganb1max/minions/util/ItemBuilder.java +++ b/src/main/java/me/loganb1max/minions/util/ItemBuilder.java @@ -23,6 +23,11 @@ public class ItemBuilder { return this; } + public ItemBuilder durability(final short durability) { + itemStack.setDurability(durability); + return this; + } + public ItemBuilder name(final String name) { ItemMeta meta = itemStack.getItemMeta(); meta.setDisplayName(Text.color(name)); diff --git a/src/main/java/me/loganb1max/minions/util/ItemUtil.java b/src/main/java/me/loganb1max/minions/util/ItemUtil.java index 9d807ba..7bb8ffa 100644 --- a/src/main/java/me/loganb1max/minions/util/ItemUtil.java +++ b/src/main/java/me/loganb1max/minions/util/ItemUtil.java @@ -74,23 +74,21 @@ public class ItemUtil { return null; } + public static boolean compareType(ItemStack stack1, ItemStack stack2) { + if (stack1 == null || stack2 == null) return false; + if (stack1.getType() != stack2.getType()) return false; + if (stack1.getDurability() != stack2.getDurability()) return false; + return true; + } + public static boolean areSimilar(ItemStack stack1, ItemStack stack2) { - if (stack1 != null && stack2 != null) { - if (stack1.getType() == stack2.getType()) { - if (stack1.getDurability() == stack2.getDurability()) { - if (stack1.getAmount() == stack2.getAmount()) { - if (stack1.hasItemMeta() && stack2.hasItemMeta()) { - if (stack1.getItemMeta().equals(stack2.getItemMeta())) { - return true; - } - } else { - return true; - } - } - } - } - } - return false; + if (stack1 == null || stack2 == null) return false; + if (stack1.getType() != stack2.getType()) return false; + if (stack1.getDurability() != stack2.getDurability()) return false; + if (stack1.getAmount() != stack2.getAmount()) return false; + if (stack1.hasItemMeta() != stack2.hasItemMeta()) return false; + if (stack1.hasItemMeta() && stack2.hasItemMeta() && (!stack1.getItemMeta().equals(stack2.getItemMeta()))) return false; + return true; } public static boolean consumeItem(Inventory inventory, int count, ItemStack itemStack) { @@ -140,11 +138,14 @@ public class ItemUtil { return true; } + public static void giveAndDrop(final Player player, final ItemStack itemStack) { + player.getInventory().addItem(itemStack).forEach((integer, itemStack1) -> player.getWorld().dropItemNaturally(player.getLocation(), itemStack1)); + } + public static ItemStack fromConfigSection(final ConfigurationSection section) { final Material material = Material.getMaterial(section.getString("Material")); final int durability = section.getInt("Durability"); - final ItemStack item = new ItemStack(material, 1); - item.setDurability((short) durability); + final ItemStack item = ItemBuilder.of(material).durability((short) durability).build(); if (section.contains("Color") && material.name().toLowerCase().contains("leather")) { LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); int red = section.getInt("Color.Red"); diff --git a/src/main/java/me/loganb1max/minions/util/Replacer.java b/src/main/java/me/loganb1max/minions/util/Replacer.java index df832d1..c388e4f 100644 --- a/src/main/java/me/loganb1max/minions/util/Replacer.java +++ b/src/main/java/me/loganb1max/minions/util/Replacer.java @@ -1,7 +1,9 @@ package me.loganb1max.minions.util; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class Replacer { @@ -23,4 +25,11 @@ public class Replacer { return string; } + public List replace(List strings) { + for (Map.Entry entry : this.replaceMap.entrySet()) { + strings = strings.stream().map(s -> s.replaceAll(entry.getKey(), entry.getValue())).collect(Collectors.toList()); + } + return strings; + } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2ace086..af95979 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,6 +2,15 @@ ###################### Minions ############################# ############################################################################ + +Fuel: + Coal: + Energy: 1.0 + Item: + Material: COAL + Durability: 0 + + ########## Placeholders ############# # %level% # %energy%