1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added AndroidFarmEvent (+ refactored Farmer Androids)

This commit is contained in:
TheBusyBiscuit 2021-01-10 21:20:43 +01:00
parent 47fc037536
commit 4c879ac3b3
8 changed files with 185 additions and 92 deletions

View File

@ -28,6 +28,7 @@
#### Additions
* Added Bee Armor (1.15+ only)
* (API) Added AndroidFarmEvent
#### Changes
* Performance optimizations for Cargo networks

View File

@ -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.
* <p>
* 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();
}
}

View File

@ -46,8 +46,6 @@ public class ThirdPartyPluginService extends IntegrationsManager {
@Deprecated
public void loadExoticGarden(Plugin plugin, Function<Block, Optional<ItemStack>> 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;
}

View File

@ -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<ItemStack> 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());
}
}
}
}

View File

@ -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<ItemStack> result = SlimefunPlugin.getThirdPartySupportService().harvestExoticGardenPlant(event.getBlock());
result.ifPresent(event::setDrop);
}
}
private ItemStack getDropFromCrop(Material crop) {
Random random = ThreadLocalRandom.current();

View File

@ -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);
}),
/**

View File

@ -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!");
}

View File

@ -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);