From 4c879ac3b3569d293ba254384c56965358d4fafc Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sun, 10 Jan 2021 21:20:43 +0100 Subject: [PATCH] Added AndroidFarmEvent (+ refactored Farmer Androids) --- CHANGELOG.md | 1 + .../api/events/AndroidFarmEvent.java | 122 ++++++++++++++++++ .../plugins/ThirdPartyPluginService.java | 2 - .../items/androids/AdvancedFarmerAndroid.java | 55 -------- .../items/androids/FarmerAndroid.java | 73 ++++++++--- .../items/androids/Instruction.java | 15 ++- .../items/androids/ProgrammableAndroid.java | 6 +- .../setup/SlimefunItemSetup.java | 3 +- 8 files changed, 185 insertions(+), 92 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AndroidFarmEvent.java delete mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AdvancedFarmerAndroid.java diff --git a/CHANGELOG.md b/CHANGELOG.md index e61b77356..eca85998d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ #### Additions * Added Bee Armor (1.15+ only) +* (API) Added AndroidFarmEvent #### Changes * Performance optimizations for Cargo networks diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AndroidFarmEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AndroidFarmEvent.java new file mode 100644 index 000000000..e2a3d1c9c --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AndroidFarmEvent.java @@ -0,0 +1,122 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.bukkit.block.Block; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.implementation.items.androids.AndroidInstance; +import io.github.thebusybiscuit.slimefun4.implementation.items.androids.FarmerAndroid; + +/** + * This {@link Event} is fired before a {@link FarmerAndroid} harvests a {@link Block}. + * If this {@link Event} is cancelled, the {@link Block} will not be harvested. + *

+ * The {@link Event} will still be fired for non-harvestable blocks. + * + * @author TheBusyBiscuit + * + */ +public class AndroidFarmEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private final Block block; + private final AndroidInstance android; + private final boolean isAdvanced; + private ItemStack drop; + private boolean cancelled; + + /** + * @param block + * The harvested {@link Block} + * @param android + * The {@link AndroidInstance} that triggered this {@link Event} + * @param isAdvanced + * Whether this is an advanced farming action + * @param drop + * The item to be dropped or null + */ + public AndroidFarmEvent(@Nonnull Block block, @Nonnull AndroidInstance android, boolean isAdvanced, @Nullable ItemStack drop) { + this.block = block; + this.android = android; + this.isAdvanced = isAdvanced; + this.drop = drop; + } + + /** + * This method returns the mined {@link Block} + * + * @return the mined {@link Block} + */ + @Nonnull + public Block getBlock() { + return block; + } + + /** + * This returns the harvested item or null. + * + * @return The harvested item or null + */ + @Nullable + public ItemStack getDrop() { + return drop; + } + + /** + * Whether this was invoked via an advanced farming action + * + * @return Whether it is advanced + */ + public boolean isAdvanced() { + return isAdvanced; + } + + /** + * This will set the {@link ItemStack} result. + * + * @param drop + * The result or null + */ + public void setDrop(@Nullable ItemStack drop) { + this.drop = drop; + } + + /** + * This method returns the {@link AndroidInstance} who + * triggered this {@link Event} + * + * @return the involved {@link AndroidInstance} + */ + @Nonnull + public AndroidInstance getAndroid() { + return android; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + @Nonnull + public static HandlerList getHandlerList() { + return handlers; + } + + @Nonnull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + +} \ No newline at end of file diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/plugins/ThirdPartyPluginService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/plugins/ThirdPartyPluginService.java index e5946487b..d5b59882f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/plugins/ThirdPartyPluginService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/plugins/ThirdPartyPluginService.java @@ -46,8 +46,6 @@ public class ThirdPartyPluginService extends IntegrationsManager { @Deprecated public void loadExoticGarden(Plugin plugin, Function> method) { - // TODO: Move this method to IntegrationsManager and think of a better way to handle this - // For next RC! if (plugin.getName().equals("ExoticGarden")) { exoticGardenIntegration = method; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AdvancedFarmerAndroid.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AdvancedFarmerAndroid.java deleted file mode 100644 index 01117440f..000000000 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AdvancedFarmerAndroid.java +++ /dev/null @@ -1,55 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.implementation.items.androids; - -import java.util.Optional; - -import javax.annotation.ParametersAreNonnullByDefault; - -import org.bukkit.Effect; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; -import me.mrCookieSlime.Slimefun.Lists.RecipeType; -import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; - -/** - * The {@link AdvancedFarmerAndroid} is an extension of the {@link FarmerAndroid}. - * It also allows the {@link Player} to harvest plants from the addon ExoticGarden. - * - * @author John000708 - * @author TheBusyBiscuit - * - * @see FarmerAndroid - * - */ -public class AdvancedFarmerAndroid extends FarmerAndroid { - - @ParametersAreNonnullByDefault - public AdvancedFarmerAndroid(Category category, int tier, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { - super(category, tier, item, recipeType, recipe); - } - - @Override - public AndroidType getAndroidType() { - return AndroidType.ADVANCED_FARMER; - } - - @Override - protected void exoticFarm(BlockMenu menu, Block block) { - farm(menu, block); - - if (SlimefunPlugin.getIntegrations().isExoticGardenInstalled()) { - Optional result = SlimefunPlugin.getThirdPartySupportService().harvestExoticGardenPlant(block); - - if (result.isPresent()) { - ItemStack drop = result.get(); - menu.pushItem(drop, getOutputSlots()); - block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType()); - } - } - } - -} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/FarmerAndroid.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/FarmerAndroid.java index e46721c84..45c2ea829 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/FarmerAndroid.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/FarmerAndroid.java @@ -1,15 +1,22 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.androids; +import java.util.Optional; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.data.Ageable; import org.bukkit.block.data.BlockData; +import org.bukkit.event.EventHandler; import org.bukkit.inventory.ItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.AndroidFarmEvent; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; @@ -17,41 +24,65 @@ import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; public class FarmerAndroid extends ProgrammableAndroid { + @ParametersAreNonnullByDefault public FarmerAndroid(Category category, int tier, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, tier, item, recipeType, recipe); } @Override public AndroidType getAndroidType() { - return AndroidType.FARMER; - } - - private boolean isFullGrown(Block block) { - BlockData data = block.getBlockData(); - - if (!(data instanceof Ageable)) { - return false; - } - - Ageable ageable = (Ageable) data; - return ageable.getAge() >= ageable.getMaximumAge(); + return getTier() == 1 ? AndroidType.FARMER : AndroidType.ADVANCED_FARMER; } @Override - protected void farm(BlockMenu menu, Block block) { - if (isFullGrown(block)) { - ItemStack drop = getDropFromCrop(block.getType()); + protected void farm(Block b, BlockMenu menu, Block block, boolean isAdvanced) { + Material blockType = block.getType(); + BlockData data = block.getBlockData(); + ItemStack drop = null; - if (drop != null && menu.fits(drop, getOutputSlots())) { - menu.pushItem(drop, getOutputSlots()); - Ageable ageable = (Ageable) block.getBlockData(); - ageable.setAge(0); - block.setBlockData(ageable); - block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType()); + if (data instanceof Ageable && ((Ageable) data).getAge() >= ((Ageable) data).getMaximumAge()) { + drop = getDropFromCrop(blockType); + } + + AndroidInstance instance = new AndroidInstance(this, b); + + AndroidFarmEvent event = new AndroidFarmEvent(block, instance, isAdvanced, drop); + Bukkit.getPluginManager().callEvent(event); + + handleExoticGarden(event); + + if (!event.isCancelled()) { + drop = event.getDrop(); + + if (drop != null && menu.pushItem(drop, getOutputSlots()) == null) { + block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, blockType); + + if (data instanceof Ageable) { + ((Ageable) data).setAge(0); + block.setBlockData(data); + } } } } + /** + * Deprecated method. + * + * @deprecated This will be moved into an {@link EventHandler} inside exoticgarden itself on the next update. + * + * @param event + * the event + * @param isAdvanced + * is advanced + */ + @Deprecated + private void handleExoticGarden(AndroidFarmEvent event) { + if (event.isAdvanced() && SlimefunPlugin.getIntegrations().isExoticGardenInstalled()) { + Optional result = SlimefunPlugin.getThirdPartySupportService().harvestExoticGardenPlant(event.getBlock()); + result.ifPresent(event::setDrop); + } + } + private ItemStack getDropFromCrop(Material crop) { Random random = ThreadLocalRandom.current(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java index 6c707c979..4f1ffef80 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java @@ -1,11 +1,12 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.androids; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Predicate; + import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Predicate; import org.apache.commons.lang.Validate; import org.bukkit.Location; @@ -191,7 +192,7 @@ public enum Instruction { */ FARM_FORWARD(AndroidType.FARMER, HeadTexture.SCRIPT_FARM_FORWARD, (android, b, inv, face) -> { Block target = b.getRelative(face); - android.farm(inv, target); + android.farm(b, inv, target, false); }), /** @@ -200,7 +201,7 @@ public enum Instruction { */ FARM_DOWN(AndroidType.FARMER, HeadTexture.SCRIPT_FARM_DOWN, (android, b, inv, face) -> { Block target = b.getRelative(BlockFace.DOWN); - android.farm(inv, target); + android.farm(b, inv, target, false); }), /** @@ -211,7 +212,7 @@ public enum Instruction { */ FARM_EXOTIC_FORWARD(AndroidType.ADVANCED_FARMER, HeadTexture.SCRIPT_FARM_FORWARD, (android, b, inv, face) -> { Block target = b.getRelative(face); - android.exoticFarm(inv, target); + android.farm(b, inv, target, true); }), /** @@ -222,7 +223,7 @@ public enum Instruction { */ FARM_EXOTIC_DOWN(AndroidType.ADVANCED_FARMER, HeadTexture.SCRIPT_FARM_DOWN, (android, b, inv, face) -> { Block target = b.getRelative(BlockFace.DOWN); - android.exoticFarm(inv, target); + android.farm(b, inv, target, true); }), /** diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java index 2f80a4848..cd1bfd2bb 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java @@ -880,11 +880,7 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock, throw new UnsupportedOperationException("Non-woodcutter Android tried to chop a Tree!"); } - protected void farm(BlockMenu menu, Block block) { - throw new UnsupportedOperationException("Non-farming Android tried to farm!"); - } - - protected void exoticFarm(BlockMenu menu, Block block) { + protected void farm(Block b, BlockMenu menu, Block block, boolean isAdvanced) { throw new UnsupportedOperationException("Non-farming Android tried to farm!"); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java index 10ab60b09..d20ada89d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java @@ -25,7 +25,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.RadioactiveItem; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import io.github.thebusybiscuit.slimefun4.implementation.items.altar.AncientAltar; import io.github.thebusybiscuit.slimefun4.implementation.items.altar.AncientPedestal; -import io.github.thebusybiscuit.slimefun4.implementation.items.androids.AdvancedFarmerAndroid; import io.github.thebusybiscuit.slimefun4.implementation.items.androids.AndroidInterface; import io.github.thebusybiscuit.slimefun4.implementation.items.androids.ButcherAndroid; import io.github.thebusybiscuit.slimefun4.implementation.items.androids.FarmerAndroid; @@ -1895,7 +1894,7 @@ public final class SlimefunItemSetup { new ItemStack[] {null, SlimefunItems.GPS_TRANSMITTER, null, new ItemStack(Material.DIAMOND_SWORD), SlimefunItems.PROGRAMMABLE_ANDROID_2, new ItemStack(Material.DIAMOND_SWORD), null, SlimefunItems.ELECTRIC_MOTOR, null}) .register(plugin); - new AdvancedFarmerAndroid(categories.androids, 2, SlimefunItems.PROGRAMMABLE_ANDROID_2_FARMER, RecipeType.ENHANCED_CRAFTING_TABLE, + new FarmerAndroid(categories.androids, 2, SlimefunItems.PROGRAMMABLE_ANDROID_2_FARMER, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[] {null, SlimefunItems.GPS_TRANSMITTER, null, new ItemStack(Material.DIAMOND_HOE), SlimefunItems.PROGRAMMABLE_ANDROID_2, new ItemStack(Material.DIAMOND_HOE), null, SlimefunItems.ELECTRIC_MOTOR, null}) .register(plugin);