Fix slime/magma cube replication

This commit is contained in:
ceze88 2023-02-05 13:14:44 +01:00
parent 65b817b8dd
commit d610f41d00
4 changed files with 34 additions and 10 deletions

View File

@ -3,11 +3,18 @@ package com.songoda.ultimatestacker.listeners.entity;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.stackable.entity.EntityStack;
import com.songoda.ultimatestacker.stackable.entity.EntityStackManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Slime;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityTransformEvent;
import org.bukkit.event.world.EntitiesLoadEvent;
import java.util.Arrays;
import java.util.List;
public class EntityCurrentListener implements Listener {
@ -23,6 +30,11 @@ public class EntityCurrentListener implements Listener {
if (stackManager.isStackedEntity(event.getEntity())
&& event.getEntity() instanceof LivingEntity
&& event.getTransformedEntity() instanceof LivingEntity) {
if (event.getTransformReason().equals(EntityTransformEvent.TransformReason.SPLIT)) {
stackManager.getStack((LivingEntity) event.getEntity()).removeEntityFromStack(1);
event.setCancelled(true);
return;
}
EntityStack stack = stackManager.updateStack((LivingEntity) event.getEntity(), (LivingEntity) event.getTransformedEntity());
stack.releaseHost();
}

View File

@ -53,7 +53,7 @@ public class EntityStack extends StackedEntity {
return hostEntity;
}
protected void setHostEntity(LivingEntity hostEntity) {
protected synchronized void setHostEntity(LivingEntity hostEntity) {
this.hostEntity = hostEntity;
}
@ -89,7 +89,6 @@ public class EntityStack extends StackedEntity {
private void handleSingleStackDeath(LivingEntity killed, List<Drop> drops, int droppedExp, EntityDeathEvent event) {
EntityStackManager stackManager = plugin.getEntityStackManager();
Bukkit.getPluginManager().callEvent(new EntityStackKillEvent(this, false));
Vector velocity = killed.getVelocity().clone();
@ -138,16 +137,16 @@ public class EntityStack extends StackedEntity {
}
}
public LivingEntity takeOneAndSpawnEntity(Location location) {
public synchronized LivingEntity takeOneAndSpawnEntity(Location location) {
if (amount <= 0) return null;
amount--;
LivingEntity entity = Objects.requireNonNull(location.getWorld()).spawn(location, hostEntity.getClass());
this.hostEntity = entity;
setAmount(amount--);
updateNameTag();
return entity;
}
public void releaseHost() {
public synchronized void releaseHost() {
LivingEntity oldHost = hostEntity;
LivingEntity entity = takeOneAndSpawnEntity(hostEntity.getLocation());
if (getAmount() >= 0) {
@ -158,7 +157,7 @@ public class EntityStack extends StackedEntity {
}
}
public void destroy() {
public synchronized void destroy() {
if (hostEntity == null) return;
Bukkit.getScheduler().runTask(plugin, hostEntity::remove);
hostEntity = null;

View File

@ -89,10 +89,25 @@ public class EntityStackManager {
return stack;
}
/**
* Transfers the stack from one entity to another.
* (e.g. slimes split)
* @param oldEntity The old entity to transfer the stack from.
* @param newEntity The new entity to transfer the stack to.
* @return The new stack.
*/
public EntityStack transferStack(LivingEntity oldEntity, LivingEntity newEntity) {
EntityStack stack = getStack(oldEntity);
if (stack == null) return null;
EntityStack newStack = new EntityStack(newEntity, stack.getAmount());
stack.destroy();
return newStack;
}
public EntityStack updateStack(LivingEntity oldEntity, LivingEntity newEntity) {
EntityStack stack = getStack(oldEntity);
if (stack == null) return null;
int amount = stack.getAmount();
int amount = stack.getAmount()-1;
stack.destroy();
return createStack(newEntity, amount);
}
@ -101,7 +116,6 @@ public class EntityStackManager {
if (isStackedEntity(newEntity)) {
EntityStack stack = getStack(newEntity);
stack.setAmount(amount);
System.err.println("Stacked entity already exists, updating stack amount to " + amount);
} else {
createStack(newEntity, amount);
}

View File

@ -65,7 +65,7 @@ import java.util.concurrent.ScheduledExecutorService;
public class StackingTask implements Runnable {
private final UltimateStacker plugin;
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private final EntityStackManager stackManager;
@ -93,7 +93,6 @@ public class StackingTask implements Runnable {
public StackingTask(UltimateStacker plugin) {
this.plugin = plugin;
this.stackManager = plugin.getEntityStackManager();
// Add loaded worlds.
for (World world : Bukkit.getWorlds())
loadedWorlds.add(new SWorld(world));