Skip to content

Commit

Permalink
fix: fix issue #290
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jul 14, 2024
1 parent 6cf2908 commit d53ba10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
10 changes: 2 additions & 8 deletions Allay-API/src/main/java/org/allaymc/api/container/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,9 @@ default int tryAddItem(ItemStack itemStack, int minSlotIndex, int maxSlotIndex)
break;
}
}
// First, try to merge with other item stack
// Firstly, try to merge with other item stack
for (int index = minSlotIndex; index <= maxSlotIndex; index++) {
var content = itemStacks[index];
// if (content == Container.EMPTY_SLOT_PLACE_HOLDER) {
// if (minEmptySlot == -1) {
// minEmptySlot = index;
// }
// continue;
// }
if (content.getCount() != content.getItemData().maxStackSize() && content.canMerge(itemStack, true)) {
if (content.getCount() + itemStack.getCount() <= content.getItemData().maxStackSize()) {
content.setCount(content.getCount() + itemStack.getCount());
Expand All @@ -167,7 +161,7 @@ default int tryAddItem(ItemStack itemStack, int minSlotIndex, int maxSlotIndex)
}
}
}
// Second, put the item on an empty slot (if exists)
// Secondly, put the item on an empty slot (if exists)
if (minEmptySlot != -1) {
setItemStack(minEmptySlot, itemStack.copy());
itemStack.setCount(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.allaymc.api.utils.MathUtils;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.World;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.server.world.chunk.AllayChunk;
import org.cloudburstmc.math.vector.Vector2f;
import org.cloudburstmc.nbt.NbtMap;
Expand Down Expand Up @@ -301,18 +302,39 @@ protected void checkChunk(Location3fc oldLoc, Location3fc newLoc) {
var oldChunkZ = (int) oldLoc.z() >> 4;
var newChunkX = (int) newLoc.x() >> 4;
var newChunkZ = (int) newLoc.z() >> 4;
if (this.location.dimension != null && (oldChunkX != newChunkX || oldChunkZ != newChunkZ)) {
var oldChunk = this.location.dimension().getChunkService().getChunk(oldChunkX, oldChunkZ);
if (oldChunk != null) ((AllayChunk) oldChunk).removeEntity(runtimeId);
else log.debug("Old chunk {} {} is null while moving entity!", oldChunkX, oldChunkZ);
}
if (oldChunkX != newChunkX || oldChunkZ != newChunkZ) {
// Current chunk changed
Chunk oldChunk = null;
if (this.location.dimension != null) {
oldChunk = this.location.dimension().getChunkService().getChunk(oldChunkX, oldChunkZ);
// It is possible that the oldChunk is null
// For example, when spawning an entity, the entity's old location is meaningless
if (oldChunk != null) {
((AllayChunk) oldChunk).removeEntity(runtimeId);
}
}

var newChunk = newLoc.dimension().getChunkService().getChunk(newChunkX, newChunkZ);
if (newChunk != null) ((AllayChunk) newChunk).addEntity(thisEntity);
else {
// Moving into an unloaded chunk is not allowed. Because the chunk holds the entity,
// moving to an unloaded chunk will result in the loss of the entity
log.debug("New chunk {} {} is null while moving entity!", newChunkX, newChunkZ);
var newChunk = newLoc.dimension().getChunkService().getChunk(newChunkX, newChunkZ);
if (newChunk != null) {
((AllayChunk) newChunk).addEntity(thisEntity);
Set<EntityPlayer> oldChunkPlayers = oldChunk != null ? oldChunk.getPlayerChunkLoaders() : Collections.emptySet();
Set<EntityPlayer> samePlayers = new HashSet<>(newChunk.getPlayerChunkLoaders());
samePlayers.retainAll(oldChunkPlayers);
for (var player : oldChunkPlayers) {
if (!samePlayers.contains(player) && player != thisEntity) {
despawnFrom(player);
}
}
for (var player : newChunk.getPlayerChunkLoaders()) {
if (!samePlayers.contains(player) && player != thisEntity) {
spawnTo(player);
}
}
} else {
// Moving into an unloaded chunk is not allowed. Because the chunk holds the entity,
// moving to an unloaded chunk will result in the loss of the entity
log.warn("New chunk {} {} is null while moving entity!", newChunkX, newChunkZ);
}
}
}

Expand Down

0 comments on commit d53ba10

Please sign in to comment.