Fix auto breeding with stacked entities and UltimateStacker hook if the plugin not present

This commit is contained in:
ceze88 2024-04-11 19:12:37 +02:00
parent fd53240f10
commit 8f4d760a38
4 changed files with 62 additions and 35 deletions

View File

@ -32,6 +32,7 @@ import com.craftaro.epicfarming.listeners.EntityListeners;
import com.craftaro.epicfarming.listeners.InteractListeners;
import com.craftaro.epicfarming.listeners.InventoryListeners;
import com.craftaro.epicfarming.listeners.MoistureListeners;
import com.craftaro.epicfarming.listeners.UltimateStackerListener;
import com.craftaro.epicfarming.listeners.UnloadListeners;
import com.craftaro.epicfarming.settings.Settings;
import com.craftaro.epicfarming.storage.Storage;
@ -157,6 +158,10 @@ public class EpicFarming extends SongodaPlugin {
}
}
if (pluginManager.isPluginEnabled("UltimateStacker")) {
pluginManager.registerEvents(new UltimateStackerListener(this), this);
}
// Start tasks
this.growthTask = new GrowthTask(this);
this.farmTask = new FarmTask(this);

View File

@ -56,10 +56,11 @@ public class ModuleAutoBreeding extends Module {
}
List<LivingEntity> entities = new ArrayList<>(entitiesAroundFarm);
Collections.shuffle(entities);
if (entities.size() < this.autoBreedCap) {
int actualAmount = entities.stream().filter(e -> e instanceof Ageable && ((Ageable) e).isAdult() && !e.hasMetadata(BREED_COOLDOWN_METADATA) && !e.isDead()).map(EntityStackerManager::getSize).reduce(Integer::sum).orElse(0);
if (actualAmount < this.autoBreedCap) {
return;
}
Collections.shuffle(entities);
entities.removeIf(e -> !(e instanceof Ageable) || !((Ageable) e).isAdult() || e.hasMetadata(BREED_COOLDOWN_METADATA) || e.isDead());
@ -109,6 +110,7 @@ public class ModuleAutoBreeding extends Module {
if (stackSize > 1) {
handleStackedBreed(entity);
spawnParticlesAndAnimation(entity.getLocation(), farm.getLocation());
} else {
handleBreed(entity);
}
@ -142,9 +144,10 @@ public class ModuleAutoBreeding extends Module {
}
private void handleStackedBreed(LivingEntity entity) {
EntityStackerManager.removeOne(entity);
Bukkit.getScheduler().runTask(this.plugin, () -> {
LivingEntity spawned = (LivingEntity) entity.getWorld().spawnEntity(entity.getLocation(), entity.getType());
Ageable ageable = (Ageable) spawned;
ageable.setBaby();
handleBreed(spawned);
});
}

View File

@ -63,38 +63,6 @@ public class EntityListeners implements Listener {
}
}
@EventHandler
public void onStackedItemSpawn(StackedItemSpawnEvent event) {
Location farmLocation = (Location) event.getExtraData().get("EFA-TAGGED");
if (farmLocation == null) {
return;
}
Farm farm = this.plugin.getFarmManager().getFarm(farmLocation);
boolean autoCollect = false;
for (Module module : farm.getLevel().getRegisteredModules()) {
if (module instanceof ModuleAutoCollect && ((ModuleAutoCollect) module).isEnabled(farm)) {
autoCollect = true;
}
}
if (autoCollect) {
long amount = event.getAmount();
ItemStack itemStack = event.getItemStack();
while (amount > 0) {
ItemStack clone = itemStack.clone();
clone.setAmount((int) Math.min(amount, clone.getMaxStackSize()));
amount -= clone.getAmount();
farm.addItem(clone);
}
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onSpawn(ItemSpawnEvent event) {
Item item = event.getEntity();

View File

@ -0,0 +1,51 @@
package com.craftaro.epicfarming.listeners;
import com.craftaro.epicfarming.EpicFarming;
import com.craftaro.epicfarming.farming.Farm;
import com.craftaro.epicfarming.farming.levels.modules.Module;
import com.craftaro.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.craftaro.ultimatestacker.api.events.entity.StackedItemSpawnEvent;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
public class UltimateStackerListener implements Listener {
private final EpicFarming plugin;
public UltimateStackerListener(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler
public void onStackedItemSpawn(StackedItemSpawnEvent event) {
Location farmLocation = (Location) event.getExtraData().get("EFA-TAGGED");
if (farmLocation == null) {
return;
}
Farm farm = this.plugin.getFarmManager().getFarm(farmLocation);
boolean autoCollect = false;
for (Module module : farm.getLevel().getRegisteredModules()) {
if (module instanceof ModuleAutoCollect && ((ModuleAutoCollect) module).isEnabled(farm)) {
autoCollect = true;
}
}
if (autoCollect) {
long amount = event.getAmount();
ItemStack itemStack = event.getItemStack();
while (amount > 0) {
ItemStack clone = itemStack.clone();
clone.setAmount((int) Math.min(amount, clone.getMaxStackSize()));
amount -= clone.getAmount();
farm.addItem(clone);
}
event.setCancelled(true);
}
}
}