From f59cf6663544cc751b06769889d81473e8a90d95 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 11 Sep 2019 14:11:34 -0400 Subject: [PATCH] Multi compatibility. --- .../com/songoda/epicfarming/farming/Farm.java | 51 +++--- .../listeners/InteractListeners.java | 4 +- .../epicfarming/settings/Settings.java | 3 +- .../songoda/epicfarming/tasks/FarmTask.java | 16 +- .../songoda/epicfarming/tasks/GrowthTask.java | 47 +----- .../songoda/epicfarming/utils/CropType.java | 150 ++++++++++++++++-- 6 files changed, 188 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/songoda/epicfarming/farming/Farm.java b/src/main/java/com/songoda/epicfarming/farming/Farm.java index fe14f0c..b228ca1 100644 --- a/src/main/java/com/songoda/epicfarming/farming/Farm.java +++ b/src/main/java/com/songoda/epicfarming/farming/Farm.java @@ -1,15 +1,16 @@ package com.songoda.epicfarming.farming; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.CompatibleParticleHandler; +import com.songoda.core.compatibility.CompatibleSound; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.hooks.EconomyManager; import com.songoda.epicfarming.EpicFarming; import com.songoda.epicfarming.boost.BoostData; import com.songoda.epicfarming.player.PlayerData; import com.songoda.epicfarming.settings.Settings; import com.songoda.epicfarming.utils.Methods; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Material; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; @@ -96,7 +97,7 @@ public class Farm { itemmeta.setLore(lore); item.setItemMeta(itemmeta); - ItemStack itemXP = new ItemStack(Material.valueOf(instance.getConfig().getString("Interfaces.XP Icon")), 1); + ItemStack itemXP = Settings.XP_ICON.getMaterial().getItem(); ItemMeta itemmetaXP = itemXP.getItemMeta(); itemmetaXP.setDisplayName(instance.getLocale().getMessage("interface.button.upgradewithxp").getMessage()); ArrayList loreXP = new ArrayList<>(); @@ -108,7 +109,7 @@ public class Farm { itemmetaXP.setLore(loreXP); itemXP.setItemMeta(itemmetaXP); - ItemStack itemECO = new ItemStack(Material.valueOf(instance.getConfig().getString("Interfaces.Economy Icon")), 1); + ItemStack itemECO = Settings.ECO_ICON.getMaterial().getItem(); ItemMeta itemmetaECO = itemECO.getItemMeta(); itemmetaECO.setDisplayName(instance.getLocale().getMessage("interface.button.upgradewitheconomy").getMessage()); ArrayList loreECO = new ArrayList<>(); @@ -217,20 +218,25 @@ public class Farm { .processPlaceholder("level", level.getLevel()).sendPrefixedMessage(player); } Location loc = location.clone().add(.5, .5, .5); + tillLand(location); + if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) return; + player.getWorld().spawnParticle(org.bukkit.Particle.valueOf(instance.getConfig().getString("Main.Upgrade Particle Type")), loc, 200, .5, .5, .5); if (instance.getLevelManager().getHighestLevel() != level) { - player.playSound(player.getLocation(), org.bukkit.Sound.ENTITY_PLAYER_LEVELUP, 0.6F, 15.0F); - + player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.6F, 15.0F); } else { - player.playSound(player.getLocation(), org.bukkit.Sound.ENTITY_PLAYER_LEVELUP, 2F, 25.0F); + player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 2F, 25.0F); + + if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) return; + + player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 2F, 25.0F); + Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.2F, 35.0F), 5L); + Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.8F, 35.0F), 10L); } - tillLand(location); } public boolean tillLand(Location location) { - Player player = Bukkit.getPlayer(placedBy); - EpicFarming instance = EpicFarming.getInstance(); if (Settings.DISABLE_AUTO_TIL_LAND.getBoolean()) return true; Block block = location.getBlock(); int radius = level.getRadius(); @@ -243,18 +249,25 @@ public class Farm { Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz); // ToDo: enum for all flowers. - if (b2.getType() == Material.TALL_GRASS || b2.getType() == Material.GRASS || b2.getType().name().contains("TULIP") || b2.getType() == Material.AZURE_BLUET || - b2.getType() == Material.BLUE_ORCHID || b2.getType() == Material.ALLIUM || b2.getType() == Material.POPPY || b2.getType() == Material.DANDELION) { + if (b2.getType() == CompatibleMaterial.TALL_GRASS.getMaterial() + || b2.getType() == CompatibleMaterial.GRASS.getMaterial() + || b2.getType().name().contains("TULIP") + || b2.getType().name().contains("ORCHID") + || b2.getType() == CompatibleMaterial.AZURE_BLUET.getMaterial() + || b2.getType() == CompatibleMaterial.ALLIUM.getMaterial() + || b2.getType() == CompatibleMaterial.POPPY.getMaterial() + || b2.getType() == CompatibleMaterial.DANDELION.getMaterial()) { Bukkit.getScheduler().runTaskLater(EpicFarming.getInstance(), () -> { - b2.getRelative(BlockFace.DOWN).setType(Material.LEGACY_SOIL); + b2.getRelative(BlockFace.DOWN).setType(CompatibleMaterial.FARMLAND.getMaterial()); b2.breakNaturally(); - b2.getWorld().playSound(b2.getLocation(), org.bukkit.Sound.BLOCK_GRASS_BREAK, 10, 15); + b2.getWorld().playSound(b2.getLocation(), CompatibleSound.BLOCK_GRASS_BREAK.getSound(), 10, 15); }, random.nextInt(30) + 1); } - if ((b2.getType() == Material.GRASS_BLOCK || b2.getType() == Material.DIRT) && b2.getRelative(BlockFace.UP).getType() == Material.AIR) { + if ((b2.getType() == CompatibleMaterial.GRASS_BLOCK.getMaterial() + || b2.getType() == Material.DIRT) && b2.getRelative(BlockFace.UP).getType() == Material.AIR) { Bukkit.getScheduler().runTaskLater(EpicFarming.getInstance(), () -> { - b2.setType(Material.LEGACY_SOIL); - b2.getWorld().playSound(b2.getLocation(), org.bukkit.Sound.BLOCK_GRASS_BREAK, 10, 15); + b2.setType(CompatibleMaterial.FARMLAND.getMaterial()); + b2.getWorld().playSound(b2.getLocation(), CompatibleSound.BLOCK_GRASS_BREAK.getSound(), 10, 15); }, random.nextInt(30) + 1); } diff --git a/src/main/java/com/songoda/epicfarming/listeners/InteractListeners.java b/src/main/java/com/songoda/epicfarming/listeners/InteractListeners.java index 096e7b1..34cde5d 100644 --- a/src/main/java/com/songoda/epicfarming/listeners/InteractListeners.java +++ b/src/main/java/com/songoda/epicfarming/listeners/InteractListeners.java @@ -1,6 +1,7 @@ package com.songoda.epicfarming.listeners; import com.songoda.epicfarming.EpicFarming; +import com.songoda.epicfarming.settings.Settings; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.event.EventHandler; @@ -22,7 +23,8 @@ public class InteractListeners implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockInteract(PlayerInteractEvent e) { - if (e.getClickedBlock() == null || e.getClickedBlock().getType() != Material.valueOf(instance.getConfig().getString("Main.Farm Block Material"))) + if (e.getClickedBlock() == null + || e.getClickedBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial()) return; if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; diff --git a/src/main/java/com/songoda/epicfarming/settings/Settings.java b/src/main/java/com/songoda/epicfarming/settings/Settings.java index 2245615..766bb0f 100644 --- a/src/main/java/com/songoda/epicfarming/settings/Settings.java +++ b/src/main/java/com/songoda/epicfarming/settings/Settings.java @@ -1,6 +1,7 @@ package com.songoda.epicfarming.settings; import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.configuration.Config; import com.songoda.core.configuration.ConfigSetting; import com.songoda.core.hooks.EconomyManager; @@ -30,7 +31,7 @@ public class Settings { public static final ConfigSetting GROWTH_TICK_SPEED = new ConfigSetting(config, "Main.Growth Tick Speed", 20, "The delay in ticks between each farm entity event."); - public static final ConfigSetting FARM_BLOCK_MATERIAL = new ConfigSetting(config, "Main.Farm Block Material", "END_ROD", + public static final ConfigSetting FARM_BLOCK_MATERIAL = new ConfigSetting(config, "Main.Farm Block Material", ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9) ? "END_ROD" : "TORCH", "What material should be used as a farm item?"); public static final ConfigSetting NON_COMMAND_FARMS = new ConfigSetting(config, "Main.Allow Non Command Issued Farm Items", false, diff --git a/src/main/java/com/songoda/epicfarming/tasks/FarmTask.java b/src/main/java/com/songoda/epicfarming/tasks/FarmTask.java index 13943ff..4fdd5c0 100644 --- a/src/main/java/com/songoda/epicfarming/tasks/FarmTask.java +++ b/src/main/java/com/songoda/epicfarming/tasks/FarmTask.java @@ -61,11 +61,7 @@ public class FarmTask extends BukkitRunnable { if (!doDrop(farm, block.getType())) continue main; if (farm.getLevel().isAutoReplant()) { - BlockState cropState = block.getState(); - Crops cropData = (Crops) cropState.getData(); - cropData.setState(CropState.VERY_SMALL); - cropState.setData(cropData); - cropState.update(); + CropType.replant(block); continue; } block.setType(Material.AIR); @@ -116,9 +112,9 @@ public class FarmTask extends BukkitRunnable { } public List getCrops(Farm farm, boolean add) { - if (System.currentTimeMillis() - ((Farm) farm).getLastCached() > 30 * 1000 || !add) { - ((Farm) farm).setLastCached(System.currentTimeMillis()); - if (add) ((Farm) farm).clearCache(); + if (((System.currentTimeMillis() - farm.getLastCached()) > (30 * 1000)) || !add) { + farm.setLastCached(System.currentTimeMillis()); + if (add) farm.clearCache(); Block block = farm.getLocation().getBlock(); int radius = farm.getLevel().getRadius(); int bx = block.getX(); @@ -132,10 +128,10 @@ public class FarmTask extends BukkitRunnable { if (!(b2.getState().getData() instanceof Crops)) continue; if (add) { - ((Farm) farm).addCachedCrop(b2); + farm.addCachedCrop(b2); continue; } - ((Farm) farm).removeCachedCrop(b2); + farm.removeCachedCrop(b2); plugin.getGrowthTask().removeCropByLocation(b2.getLocation()); } } diff --git a/src/main/java/com/songoda/epicfarming/tasks/GrowthTask.java b/src/main/java/com/songoda/epicfarming/tasks/GrowthTask.java index e3c4d97..953912a 100644 --- a/src/main/java/com/songoda/epicfarming/tasks/GrowthTask.java +++ b/src/main/java/com/songoda/epicfarming/tasks/GrowthTask.java @@ -2,6 +2,8 @@ package com.songoda.epicfarming.tasks; import com.songoda.epicfarming.EpicFarming; import com.songoda.epicfarming.farming.Crop; +import com.songoda.epicfarming.utils.CropHandler; +import com.songoda.epicfarming.utils.CropType; import org.bukkit.CropState; import org.bukkit.Location; import org.bukkit.Material; @@ -42,63 +44,30 @@ public class GrowthTask extends BukkitRunnable { continue; } - if (!(crop.getLocation().getBlock().getState().getData() instanceof Crops)) { + if (!CropType.isGrowableCrop(crop.getLocation().getBlock().getType())) { toRemove.add(crop); continue; } - //ToDO: This should be in config. + // TODO: This should be in config. int cap = (int)Math.ceil(60 / crop.getFarm().getLevel().getSpeedMultiplier()) - crop.getTicksLived(); if (cap > 2) { int rand = random.nextInt(cap) + 1; crop.setTicksLived(crop.getTicksLived() + 1); if (rand != cap - 1 && crop.getTicksLived() != cap / 2) continue; - } - BlockState cropState = crop.getLocation().getBlock().getState(); - Crops cropData = (Crops) cropState.getData(); + CropType.grow(crop); - Material material = crop.getLocation().getBlock().getType(); - - switch(cropData.getState()) { - case SEEDED: - if (material == Material.BEETROOTS) - cropData.setState(CropState.VERY_SMALL); - else - cropData.setState(CropState.GERMINATED); - break; - case GERMINATED: - cropData.setState(CropState.VERY_SMALL); - break; - case VERY_SMALL: - cropData.setState(CropState.SMALL); - break; - case SMALL: - cropData.setState(CropState.MEDIUM); - break; - case MEDIUM: - cropData.setState(CropState.TALL); - break; - case TALL: - cropData.setState(CropState.VERY_TALL); - break; - case VERY_TALL: - cropData.setState(CropState.RIPE); - break; - case RIPE: - break; - } - cropState.setData(cropData); - cropState.update(); crop.setTicksLived(1); - } + for (Crop crop : toRemove) - liveCrops.remove(crop); + liveCrops.remove(crop.getLocation()); } + public void addLiveCrop(Location location, Crop crop) { if (!liveCrops.containsKey(location)) liveCrops.put(location, crop); diff --git a/src/main/java/com/songoda/epicfarming/utils/CropType.java b/src/main/java/com/songoda/epicfarming/utils/CropType.java index 6cf4379..92eb85e 100644 --- a/src/main/java/com/songoda/epicfarming/utils/CropType.java +++ b/src/main/java/com/songoda/epicfarming/utils/CropType.java @@ -1,27 +1,34 @@ package com.songoda.epicfarming.utils; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.ServerVersion; +import com.songoda.epicfarming.farming.Crop; +import org.bukkit.CropState; import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.material.Crops; -public enum CropType { +public enum CropType { - WHEAT("Wheat", Material.WHEAT, Material.WHEAT, Material.WHEAT_SEEDS), + WHEAT("Wheat", CompatibleMaterial.WHEAT, CompatibleMaterial.WHEAT, CompatibleMaterial.WHEAT_SEEDS), - CARROT("Carrot", Material.CARROTS, Material.CARROT, Material.CARROT), + CARROT("Carrot", CompatibleMaterial.CARROTS, CompatibleMaterial.CARROT, CompatibleMaterial.CARROT), - POTATO("Potato", Material.POTATOES, Material.POTATO, Material.POTATO), + POTATO("Potato", CompatibleMaterial.POTATOES, CompatibleMaterial.POTATO, CompatibleMaterial.POTATO), - BEETROOT("Beetroot", Material.BEETROOTS, Material.BEETROOT, Material.BEETROOT_SEEDS), + BEETROOT("Beetroot", CompatibleMaterial.BEETROOTS, CompatibleMaterial.BEETROOT, CompatibleMaterial.BEETROOT_SEEDS), - WATER_MELON_STEM("Watermelon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS), + WATER_MELON_STEM("Watermelon", CompatibleMaterial.MELON_STEM, CompatibleMaterial.MELON, CompatibleMaterial.MELON_SEEDS), - PUMPKIN_STEM("Pumpkin", Material.PUMPKIN_STEM, Material.PUMPKIN, Material.PUMPKIN_SEEDS), + PUMPKIN_STEM("Pumpkin", CompatibleMaterial.PUMPKIN_STEM, CompatibleMaterial.PUMPKIN, CompatibleMaterial.PUMPKIN_SEEDS), - NETHER_WARTS("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WART, Material.NETHER_WART); + NETHER_WARTS("Nether Wart", CompatibleMaterial.NETHER_WART_BLOCK, CompatibleMaterial.NETHER_WART, CompatibleMaterial.NETHER_WART); private final String name; - private final Material yieldMaterial, blockMaterial, seedMaterial; + private final CompatibleMaterial yieldMaterial, blockMaterial, seedMaterial; - CropType(String name, Material blockMaterial, Material yieldMaterial, Material seedMaterial) { + CropType(String name, CompatibleMaterial blockMaterial, CompatibleMaterial yieldMaterial, CompatibleMaterial seedMaterial) { this.name = name; this.blockMaterial = blockMaterial; this.seedMaterial = seedMaterial; @@ -44,7 +51,7 @@ public enum CropType { * @return the represented blockMaterial */ public Material getBlockMaterial() { - return blockMaterial; + return blockMaterial.getMaterial(); } /** @@ -53,7 +60,7 @@ public enum CropType { * @return the represented yieldMaterial */ public Material getYieldMaterial() { - return yieldMaterial; + return yieldMaterial.getMaterial(); } /** @@ -62,7 +69,7 @@ public enum CropType { * @return the represented seed blockMaterial */ public Material getSeedMaterial() { - return seedMaterial; + return seedMaterial.getMaterial(); } /** @@ -101,4 +108,121 @@ public enum CropType { return null; } + /** + * Checks if a crop is growable + * + * @param material The material to check + * @return True if the material is of a growable crop, otherwise false + */ + public static boolean isGrowableCrop(Material material) { + return CropType.isCrop(material); + } + + /** + * Checks if a crop is at its max growth stage + * + * @param block The crop block to check + * @return True if the crop is at its max growth stage, otherwise false + */ + public static boolean isMaxGrowthStage(Block block) { + if (!isGrowableCrop(block.getType())) + throw new IllegalArgumentException("Block given was not a valid crop"); + + return block.getData() >= getMaxGrowthStage(block.getType()); + } + + /** + * Gets the max growth stage for the given material + * + * @param material The material of the crop + * @return The max growth stage of the given crop type + */ + public static int getMaxGrowthStage(Material material) { + if (!isGrowableCrop(material)) + throw new IllegalArgumentException("Block given was not a valid crop"); + + if (material.equals(CompatibleMaterial.BEETROOTS.getMaterial())) + return 3; + + return 7; + } + + /** + * Grows a crop by 1 growth stage + * + * @param crop The crop to grow + */ + public static void grow(Crop crop) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) { + BlockState cropState = crop.getLocation().getBlock().getState(); + Crops cropData = (Crops) cropState.getData(); + + Material material = crop.getLocation().getBlock().getType(); + + switch (cropData.getState()) { + case SEEDED: + if (material == Material.BEETROOTS) + cropData.setState(CropState.VERY_SMALL); + else + cropData.setState(CropState.GERMINATED); + break; + case GERMINATED: + cropData.setState(CropState.VERY_SMALL); + break; + case VERY_SMALL: + cropData.setState(CropState.SMALL); + break; + case SMALL: + cropData.setState(CropState.MEDIUM); + break; + case MEDIUM: + cropData.setState(CropState.TALL); + break; + case TALL: + cropData.setState(CropState.VERY_TALL); + break; + case VERY_TALL: + cropData.setState(CropState.RIPE); + break; + case RIPE: + break; + } + cropState.setData(cropData); + cropState.update(); + crop.setTicksLived(1); + return; + } + + Block block = crop.getLocation().getBlock(); + + if (!isGrowableCrop(block.getType())) + throw new IllegalArgumentException("Block given was not a valid crop"); + + byte data = block.getData(); + + if (isMaxGrowthStage(block)) + return; + + block.setData((byte) (data + 1)); + } + + /** + * Sets a crop's growth back to stage 0 + * + * @param block The crop block to set + */ + public static void replant(Block block) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) { + BlockState cropState = block.getState(); + Crops cropData = (Crops) cropState.getData(); + cropData.setState(CropState.SEEDED); + cropState.setData(cropData); + cropState.update(); + return; + } + if (!isGrowableCrop(block.getType())) + throw new IllegalArgumentException("Block given was not a valid crop"); + + block.setData((byte) 0); + } } \ No newline at end of file