use of com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle in project BKCommonLib by bergerhealer.
the class CommonEntity method replaceEntity.
@SuppressWarnings("unchecked")
private void replaceEntity(final EntityHandle newInstance) {
final EntityHandle oldInstance = this.handle;
if (oldInstance.getRaw() == newInstance.getRaw()) {
throw new RuntimeException("Can not replace an entity with itself!");
}
// *** Bukkit Entity ***
CraftEntityHandle craftEntity = CraftEntityHandle.createHandle(this.entity);
craftEntity.setHandle(newInstance);
if (entity instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) entity).getInventory();
if (CraftInventoryHandle.T.isAssignableFrom(inv)) {
CraftInventoryHandle cInv = CraftInventoryHandle.createHandle(inv);
if (IInventoryHandle.T.isAssignableFrom(newInstance.getRaw())) {
IInventoryHandle iinvHandle = IInventoryHandle.createHandle(newInstance.getRaw());
cInv.setHandleField(iinvHandle);
}
}
}
// *** Give the old entity a new Bukkit Entity ***
oldInstance.setBukkitEntityField(CraftEntityHandle.createCraftEntity(Bukkit.getServer(), oldInstance));
this.handle = newInstance;
// *** Replace entity in passenger and vehicle fields ***
EntityHandle vehicle = newInstance.getVehicle();
if (vehicle != null) {
List<EntityHandle> passengers = new ArrayList<EntityHandle>(vehicle.getPassengers());
replaceInList(passengers, newInstance);
vehicle.setPassengers(passengers);
}
for (EntityHandle passenger : newInstance.getPassengers()) {
if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
passenger.setVehicle(newInstance);
}
}
// *** Replace data that is stored in the entity itself ***
newInstance.assignEntityReference();
// *** Perform further replacement all over the place in the server ***
EntityAddRemoveHandler.INSTANCE.replace(oldInstance, newInstance);
// *** Reset entity state ***
oldInstance.setRemovedPassive();
newInstance.setValid(true);
// *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
CommonUtil.nextTick(() -> EntityAddRemoveHandler.INSTANCE.replace(oldInstance, newInstance));
// *** Make sure a controller is set when hooked ***
if (this.isHooked()) {
DefaultEntityController controller = new DefaultEntityController();
controller.bind(this, true);
}
}
use of com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle in project BKCommonLib by bergerhealer.
the class MapPlayerInput method updateInterception.
private void updateInterception(boolean intercept) {
// No receivers, do not do input interception
if (!intercept) {
updateInputInterception(false);
return;
}
// If the player is already inside a vehicle, we can not fake-mount him
if (player.isInsideVehicle()) {
updateInputInterception(false);
return;
}
// Since Minecraft 1.16, it is not possible to be on the mount while sneaking
if (!CommonCapabilities.VEHICLE_EXIT_CANCELLABLE && player.isSneaking()) {
updateInputInterception(false);
return;
}
// Verify the player isn't flying, it results in a kick
if (!player.isFlying() && !player.isOnGround()) {
// Check if there is a block below the player, which means onGround is actually true
// This is because, when the player is 'floating' while intercepting, onGround stays false
EntityHandle playerHandle = EntityHandle.fromBukkit(player);
double half_width = 0.5 * (double) playerHandle.getWidth();
double below = 0.1;
AxisAlignedBBHandle below_bounds = AxisAlignedBBHandle.createNew(playerHandle.getLocX() - half_width, playerHandle.getLocY() - below, playerHandle.getLocZ() - half_width, playerHandle.getLocX() + half_width, playerHandle.getLocY(), playerHandle.getLocZ() + half_width);
if (WorldHandle.fromBukkit(player.getWorld()).isNotCollidingWithBlocks(playerHandle, below_bounds)) {
updateInputInterception(false);
return;
}
}
// Allowed
this.updateInputInterception(true);
}
use of com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle in project BKCommonLib by bergerhealer.
the class ExtendedEntity method getLeashHolder.
/**
* Gets the Entity that is holding this Entity by a leash. If this Entity
* does not support leashing, or the Entity is not on a leash, null is
* returned instead.
*
* @return Leash holder
*/
public org.bukkit.entity.Entity getLeashHolder() {
if (handle.isInstanceOf(EntityInsentientHandle.T)) {
EntityInsentientHandle insHandle = EntityInsentientHandle.createHandle(handle.getRaw());
EntityHandle holder = insHandle.getLeashHolder();
if (holder != null) {
return holder.getBukkitEntity();
}
}
return null;
}
use of com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle in project BKCommonLib by bergerhealer.
the class EntityAddRemoveHandler_1_17 method replace.
@Override
public void replace(EntityHandle oldEntity, EntityHandle newEntity) {
WorldServerHandle world = oldEntity.getWorldServer();
if (newEntity == null) {
if (world != null) {
world.removeEntity(oldEntity);
world.getEntityTracker().stopTracking(oldEntity.getBukkitEntity());
}
}
// *** EntityTrackerEntry ***
if (newEntity == null) {
// If still tracked despite removal, wipe the tracker
world.getEntityTracker().removeEntry(oldEntity.getIdField());
} else {
// Replacement
replaceInEntityTracker(oldEntity, oldEntity, newEntity);
if (oldEntity.getVehicle() != null) {
replaceInEntityTracker(oldEntity.getVehicle(), oldEntity, newEntity);
}
if (oldEntity.getPassengers() != null) {
for (EntityHandle passenger : oldEntity.getPassengers()) {
replaceInEntityTracker(passenger, oldEntity, newEntity);
}
}
}
Object newEntityRaw = Handle.getRaw(newEntity);
if (world != null) {
removeHandler.replaceInWorldStorage(world.getRaw(), oldEntity.getRaw(), newEntityRaw);
}
removeHandler.replaceInSectionStorage(oldEntity.getRaw(), newEntityRaw);
removeHandler.replaceInChunkStorage(oldEntity.getRaw(), newEntityRaw);
// See where the object is still referenced to check we aren't missing any places to replace
// This is SLOW, do not ever have this enabled on a release version!
// com.bergerkiller.bukkit.common.utils.DebugUtil.logInstances(oldEntity.getRaw());
}
use of com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle in project BKCommonLib by bergerhealer.
the class EntityAddRemoveHandler_1_8_to_1_13_2 method replace.
@Override
public void replace(EntityHandle oldEntity, EntityHandle newEntity) {
WorldServerHandle world = oldEntity.getWorldServer();
if (newEntity == null) {
if (world != null) {
world.removeEntity(oldEntity);
world.getEntityTracker().stopTracking(oldEntity.getBukkitEntity());
}
// Works fine, no need to clean up any more
return;
}
Object worldHandle = world.getRaw();
// *** Entities By UUID Map ***
{
Map<UUID, Object> entitiesByUUID = entitiesByUUIDField.get(worldHandle);
Object storedEntityHandle = entitiesByUUID.get(oldEntity.getUniqueID());
if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
if (!oldEntity.getUniqueID().equals(newEntity.getUniqueID())) {
entitiesByUUID.remove(oldEntity.getUniqueID());
}
entitiesByUUID.put(newEntity.getUniqueID(), newEntity.getRaw());
}
}
// *** Entities by Id Map ***
{
IntHashMapHandle entitiesById = IntHashMapHandle.createHandle(this.entitiesByIdField.get(worldHandle));
Object storedEntityHandle = entitiesById.get(oldEntity.getId());
if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
if (oldEntity.getId() != newEntity.getId()) {
entitiesById.remove(oldEntity.getId());
}
entitiesById.put(newEntity.getId(), newEntity.getRaw());
}
}
// *** Entities By UUID Map ***
final Map<UUID, Object> entitiesByUUID = entitiesByUUIDField.get(worldHandle);
entitiesByUUID.put(newEntity.getUniqueID(), newEntity.getRaw());
// *** Entities by Id Map ***
IntHashMapHandle entitiesById = IntHashMapHandle.createHandle(this.entitiesByIdField.get(worldHandle));
entitiesById.put(newEntity.getId(), newEntity.getRaw());
// *** EntityTrackerEntry ***
replaceInEntityTracker(newEntity.getId(), newEntity);
if (newEntity.getVehicle() != null) {
replaceInEntityTracker(newEntity.getVehicle().getId(), newEntity);
}
if (newEntity.getPassengers() != null) {
for (EntityHandle passenger : newEntity.getPassengers()) {
replaceInEntityTracker(passenger.getId(), newEntity);
}
}
// *** World ***
replaceInList(entityListField.get(worldHandle), newEntity);
// Fixes for PaperSpigot
// if (!Common.IS_PAPERSPIGOT_SERVER) {
// replaceInList(WorldRef.entityRemovalList.get(oldInstance.world), newInstance);
// }
// *** Entity Current Chunk ***
final int chunkX = newEntity.getChunkX();
final int chunkZ = newEntity.getChunkZ();
Object chunkHandle = HandleConversion.toChunkHandle(WorldUtil.getChunk(newEntity.getWorld().getWorld(), chunkX, chunkZ));
if (chunkHandle != null) {
this.chunkEntitySliceHandler.replace(chunkHandle, oldEntity, newEntity);
}
// See where the object is still referenced to check we aren't missing any places to replace
// This is SLOW, do not ever have this enabled on a release version!
// com.bergerkiller.bukkit.common.utils.DebugUtil.logInstances(oldInstance.getRaw());
}
Aggregations