mirror of
https://github.com/songoda/UltimateStacker.git
synced 2024-12-29 03:48:43 +01:00
Fix slime/magma cube replication
This commit is contained in:
parent
65b817b8dd
commit
d610f41d00
@ -3,11 +3,18 @@ package com.songoda.ultimatestacker.listeners.entity;
|
|||||||
import com.songoda.ultimatestacker.UltimateStacker;
|
import com.songoda.ultimatestacker.UltimateStacker;
|
||||||
import com.songoda.ultimatestacker.stackable.entity.EntityStack;
|
import com.songoda.ultimatestacker.stackable.entity.EntityStack;
|
||||||
import com.songoda.ultimatestacker.stackable.entity.EntityStackManager;
|
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.LivingEntity;
|
||||||
|
import org.bukkit.entity.Slime;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.entity.EntityTransformEvent;
|
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 {
|
public class EntityCurrentListener implements Listener {
|
||||||
|
|
||||||
@ -23,6 +30,11 @@ public class EntityCurrentListener implements Listener {
|
|||||||
if (stackManager.isStackedEntity(event.getEntity())
|
if (stackManager.isStackedEntity(event.getEntity())
|
||||||
&& event.getEntity() instanceof LivingEntity
|
&& event.getEntity() instanceof LivingEntity
|
||||||
&& event.getTransformedEntity() 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());
|
EntityStack stack = stackManager.updateStack((LivingEntity) event.getEntity(), (LivingEntity) event.getTransformedEntity());
|
||||||
stack.releaseHost();
|
stack.releaseHost();
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ public class EntityStack extends StackedEntity {
|
|||||||
return hostEntity;
|
return hostEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setHostEntity(LivingEntity hostEntity) {
|
protected synchronized void setHostEntity(LivingEntity hostEntity) {
|
||||||
this.hostEntity = 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) {
|
private void handleSingleStackDeath(LivingEntity killed, List<Drop> drops, int droppedExp, EntityDeathEvent event) {
|
||||||
EntityStackManager stackManager = plugin.getEntityStackManager();
|
EntityStackManager stackManager = plugin.getEntityStackManager();
|
||||||
|
|
||||||
Bukkit.getPluginManager().callEvent(new EntityStackKillEvent(this, false));
|
Bukkit.getPluginManager().callEvent(new EntityStackKillEvent(this, false));
|
||||||
|
|
||||||
Vector velocity = killed.getVelocity().clone();
|
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;
|
if (amount <= 0) return null;
|
||||||
amount--;
|
|
||||||
LivingEntity entity = Objects.requireNonNull(location.getWorld()).spawn(location, hostEntity.getClass());
|
LivingEntity entity = Objects.requireNonNull(location.getWorld()).spawn(location, hostEntity.getClass());
|
||||||
this.hostEntity = entity;
|
this.hostEntity = entity;
|
||||||
|
setAmount(amount--);
|
||||||
updateNameTag();
|
updateNameTag();
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseHost() {
|
public synchronized void releaseHost() {
|
||||||
LivingEntity oldHost = hostEntity;
|
LivingEntity oldHost = hostEntity;
|
||||||
LivingEntity entity = takeOneAndSpawnEntity(hostEntity.getLocation());
|
LivingEntity entity = takeOneAndSpawnEntity(hostEntity.getLocation());
|
||||||
if (getAmount() >= 0) {
|
if (getAmount() >= 0) {
|
||||||
@ -158,7 +157,7 @@ public class EntityStack extends StackedEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroy() {
|
public synchronized void destroy() {
|
||||||
if (hostEntity == null) return;
|
if (hostEntity == null) return;
|
||||||
Bukkit.getScheduler().runTask(plugin, hostEntity::remove);
|
Bukkit.getScheduler().runTask(plugin, hostEntity::remove);
|
||||||
hostEntity = null;
|
hostEntity = null;
|
||||||
|
@ -89,10 +89,25 @@ public class EntityStackManager {
|
|||||||
return stack;
|
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) {
|
public EntityStack updateStack(LivingEntity oldEntity, LivingEntity newEntity) {
|
||||||
EntityStack stack = getStack(oldEntity);
|
EntityStack stack = getStack(oldEntity);
|
||||||
if (stack == null) return null;
|
if (stack == null) return null;
|
||||||
int amount = stack.getAmount();
|
int amount = stack.getAmount()-1;
|
||||||
stack.destroy();
|
stack.destroy();
|
||||||
return createStack(newEntity, amount);
|
return createStack(newEntity, amount);
|
||||||
}
|
}
|
||||||
@ -101,7 +116,6 @@ public class EntityStackManager {
|
|||||||
if (isStackedEntity(newEntity)) {
|
if (isStackedEntity(newEntity)) {
|
||||||
EntityStack stack = getStack(newEntity);
|
EntityStack stack = getStack(newEntity);
|
||||||
stack.setAmount(amount);
|
stack.setAmount(amount);
|
||||||
System.err.println("Stacked entity already exists, updating stack amount to " + amount);
|
|
||||||
} else {
|
} else {
|
||||||
createStack(newEntity, amount);
|
createStack(newEntity, amount);
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
public class StackingTask implements Runnable {
|
public class StackingTask implements Runnable {
|
||||||
|
|
||||||
private final UltimateStacker plugin;
|
private final UltimateStacker plugin;
|
||||||
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);
|
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
private final EntityStackManager stackManager;
|
private final EntityStackManager stackManager;
|
||||||
|
|
||||||
@ -93,7 +93,6 @@ public class StackingTask implements Runnable {
|
|||||||
public StackingTask(UltimateStacker plugin) {
|
public StackingTask(UltimateStacker plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.stackManager = plugin.getEntityStackManager();
|
this.stackManager = plugin.getEntityStackManager();
|
||||||
|
|
||||||
// Add loaded worlds.
|
// Add loaded worlds.
|
||||||
for (World world : Bukkit.getWorlds())
|
for (World world : Bukkit.getWorlds())
|
||||||
loadedWorlds.add(new SWorld(world));
|
loadedWorlds.add(new SWorld(world));
|
||||||
|
Loading…
Reference in New Issue
Block a user