fill in old stubs regaridng chunk stuff & another inv fix

This commit is contained in:
sylvessa 2026-04-13 20:55:02 -05:00
parent d26c79a11e
commit 08f3aeff5f
10 changed files with 158 additions and 9 deletions

View file

@ -388,6 +388,38 @@ void ServerChunkCache::overwriteHellLevelChunkFromSource(int x, int z, int minVa
#endif
#ifdef MINECRAFT_SERVER_BUILD
void ServerChunkCache::regenerateChunk(int x, int z)
{
if (!source)
return;
LevelChunk *freshChunk = source->getChunk(x, z);
if (!freshChunk)
return;
LevelChunk *cachedChunk = nullptr;
if (hasChunk(x, z))
cachedChunk = getChunk(x, z);
if (cachedChunk && cachedChunk != emptyChunk)
{
for (int lx = 0; lx < 16; lx++)
for (int ly = 0; ly < 128; ly++)
for (int lz = 0; lz < 16; lz++)
cachedChunk->setTileAndData(lx, ly, lz, freshChunk->getTile(lx, ly, lz), freshChunk->getData(lx, ly, lz));
save(cachedChunk);
}
else
{
save(freshChunk);
}
freshChunk->unload(false);
delete freshChunk;
}
#endif
// 4J Added //
#ifdef _LARGE_WORLDS
void ServerChunkCache::dontDrop(int x, int z)

View file

@ -53,6 +53,9 @@ public:
#endif
virtual LevelChunk **getCache() { return cache; } // 4J added
#ifdef MINECRAFT_SERVER_BUILD
void regenerateChunk(int x, int z);
#endif
// 4J-JEV Added; Remove chunk from the toDrop queue.
#ifdef _LARGE_WORLDS

View file

@ -191,7 +191,58 @@ public class Chunk
/// <returns>The entities.</returns>
public Entity.Entity[] getEntities()
{
return Array.Empty<Entity.Entity>();
if (NativeBridge.GetChunkEntities == null) return Array.Empty<Entity.Entity>();
int dimId = _world.getDimensionId();
int count = NativeBridge.GetChunkEntities(dimId, _chunkX, _chunkZ, out IntPtr buf);
if (count <= 0 || buf == IntPtr.Zero) return Array.Empty<Entity.Entity>();
var result = new Entity.Entity[count];
try
{
int[] data = new int[count * 3];
Marshal.Copy(buf, data, 0, count * 3);
for (int i = 0; i < count; i++)
{
int entityId = data[i * 3];
int mappedType = data[i * 3 + 1];
int isLiving = data[i * 3 + 2];
var entityType = Enum.IsDefined(typeof(Entity.EntityType), mappedType)
? (Entity.EntityType)mappedType
: Entity.EntityType.UNKNOWN;
if (entityType == Entity.EntityType.PLAYER)
{
var player = FourKit.GetPlayerByEntityId(entityId);
if (player != null)
{
result[i] = player;
continue;
}
}
if (isLiving == 1)
{
result[i] = new Entity.LivingEntity(entityId, entityType, dimId, 0, 0, 0);
}
else
{
var entity = new Entity.Entity();
entity.SetEntityIdInternal(entityId);
entity.SetEntityTypeInternal(entityType);
entity.SetDimensionInternal(dimId);
result[i] = entity;
}
}
}
finally
{
Marshal.FreeCoTaskMem(buf);
}
return result;
}
/// <summary>

View file

@ -149,11 +149,11 @@ public static partial class FourKitHost
}
[UnmanagedCallersOnly]
public static void SetWorldEntityCallbacks(IntPtr getWorldEntities)
public static void SetWorldEntityCallbacks(IntPtr getWorldEntities, IntPtr getChunkEntities)
{
try
{
NativeBridge.SetWorldEntityCallbacks(getWorldEntities);
NativeBridge.SetWorldEntityCallbacks(getWorldEntities, getChunkEntities);
}
catch (Exception ex)
{

View file

@ -174,7 +174,9 @@ public class Inventory : IEnumerable<ItemStack>
if (_items[slot] == null)
{
int added = Math.Min(64, remaining);
setItem(slot, new ItemStack(toAdd.getType(), added, toAdd.getDurability()));
var newItem = new ItemStack(toAdd.getType(), added, toAdd.getDurability());
newItem.setItemMetaInternal(toAdd.getItemMetaInternal()?.clone());
setItem(slot, newItem);
remaining -= added;
}
}

View file

@ -222,6 +222,9 @@ internal static class NativeBridge
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate int NativeGetWorldEntitiesDelegate(int dimId, out IntPtr outBuf);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate int NativeGetChunkEntitiesDelegate(int dimId, int chunkX, int chunkZ, out IntPtr outBuf);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate int NativeGetSkyLightDelegate(int dimId, int x, int y, int z);
@ -306,6 +309,7 @@ internal static class NativeBridge
internal static NativeRegenerateChunkDelegate? RegenerateChunk;
internal static NativeRefreshChunkDelegate? RefreshChunk;
internal static NativeGetWorldEntitiesDelegate? GetWorldEntities;
internal static NativeGetChunkEntitiesDelegate? GetChunkEntities;
internal static NativeGetSkyLightDelegate? GetSkyLight;
internal static NativeGetBlockLightDelegate? GetBlockLight;
internal static NativeGetBiomeIdDelegate? GetBiomeId;
@ -422,9 +426,10 @@ internal static class NativeBridge
RefreshChunk = Marshal.GetDelegateForFunctionPointer<NativeRefreshChunkDelegate>(refreshChunk);
}
internal static void SetWorldEntityCallbacks(IntPtr getWorldEntities)
internal static void SetWorldEntityCallbacks(IntPtr getWorldEntities, IntPtr getChunkEntities)
{
GetWorldEntities = Marshal.GetDelegateForFunctionPointer<NativeGetWorldEntitiesDelegate>(getWorldEntities);
GetChunkEntities = Marshal.GetDelegateForFunctionPointer<NativeGetChunkEntitiesDelegate>(getChunkEntities);
}
internal static void SetBlockInfoCallbacks(IntPtr getSkyLight, IntPtr getBlockLight, IntPtr getBiomeId, IntPtr setBiomeId)

View file

@ -105,7 +105,7 @@ typedef int(__stdcall *fn_fire_command_preprocess)(int entityId, const char *cmd
typedef int(__stdcall *fn_fire_block_from_to)(int dimId, int fromX, int fromY, int fromZ, int toX, int toY, int toZ, int face);
typedef void(__stdcall *fn_set_chunk_callbacks)(void *isChunkLoaded, void *loadChunk, void *unloadChunk, void *getLoadedChunks, void *isChunkInUse, void *getChunkSnapshot, void *unloadChunkRequest, void *regenerateChunk, void *refreshChunk);
typedef void(__stdcall *fn_set_block_info_callbacks)(void *getSkyLight, void *getBlockLight, void *getBiomeId, void *setBiomeId);
typedef void(__stdcall *fn_set_world_entity_callbacks)(void *getWorldEntities);
typedef void(__stdcall *fn_set_world_entity_callbacks)(void *getWorldEntities, void *getChunkEntities);
typedef void(__stdcall *fn_fire_chunk_load)(int dimId, int chunkX, int chunkZ, int isNewChunk);
typedef int(__stdcall *fn_fire_chunk_unload)(int dimId, int chunkX, int chunkZ);
@ -369,7 +369,8 @@ void Initialize()
(void *)&NativeSetBiomeId);
s_managedSetWorldEntityCallbacks(
(void *)&NativeGetWorldEntities);
(void *)&NativeGetWorldEntities,
(void *)&NativeGetChunkEntities);
LogInfo("fourkit", "FourKit initialized successfully.");
}

View file

@ -33,6 +33,7 @@
#include "..\Minecraft.World\Player.h"
#include "..\Minecraft.World\PlayerAbilitiesPacket.h"
#include "..\Minecraft.World\SetCarriedItemPacket.h"
#include "..\Minecraft.World\BlockRegionUpdatePacket.h"
#include "..\Minecraft.World\SetExperiencePacket.h"
#include "..\Minecraft.World\SetHealthPacket.h"
#include "..\Minecraft.World\LevelSoundPacket.h"
@ -1363,6 +1364,39 @@ int __cdecl NativeGetWorldEntities(int dimId, int **outBuf)
return count;
}
int __cdecl NativeGetChunkEntities(int dimId, int chunkX, int chunkZ, int **outBuf)
{
*outBuf = nullptr;
ServerLevel *level = GetLevel(dimId);
if (!level)
return 0;
EnterCriticalSection(&level->m_entitiesCS);
int total = (int)level->entities.size();
int *buf = (int *)CoTaskMemAlloc(total * 3 * sizeof(int));
int count = 0;
if (buf)
{
for (auto &entity : level->entities)
{
if (!entity)
continue;
int ecx = Mth::floor(entity->x / 16.0);
int ecz = Mth::floor(entity->z / 16.0);
if (ecx != chunkX || ecz != chunkZ)
continue;
int idx = count * 3;
buf[idx] = entity->entityId;
buf[idx + 1] = MapEntityType((int)entity->GetType());
buf[idx + 2] = entity->instanceof(eTYPE_LIVINGENTITY) ? 1 : 0;
count++;
}
}
LeaveCriticalSection(&level->m_entitiesCS);
*outBuf = buf;
return count;
}
int __cdecl NativeIsChunkLoaded(int dimId, int chunkX, int chunkZ)
{
ServerLevel *level = GetLevel(dimId);
@ -1513,12 +1547,31 @@ int __cdecl NativeUnloadChunkRequest(int dimId, int chunkX, int chunkZ, int safe
int __cdecl NativeRegenerateChunk(int dimId, int chunkX, int chunkZ)
{
return 0;
ServerLevel *level = GetLevel(dimId);
if (!level || !level->cache)
return 0;
level->cache->regenerateChunk(chunkX, chunkZ);
return 1;
}
int __cdecl NativeRefreshChunk(int dimId, int chunkX, int chunkZ)
{
return 0;
ServerLevel *level = GetLevel(dimId);
if (!level)
return 0;
PlayerList *list = MinecraftServer::getPlayerList();
if (!list)
return 0;
auto packet = std::make_shared<BlockRegionUpdatePacket>(chunkX * 16, 0, chunkZ * 16, 16, Level::maxBuildHeight, 16, level);
for (auto &p : list->players)
{
if (!p || p->dimension != dimId || !p->connection || p->connection->isLocal())
continue;
p->connection->send(packet);
}
return 1;
}
int __cdecl NativeGetSkyLight(int dimId, int x, int y, int z)

View file

@ -98,6 +98,7 @@ namespace FourKitBridge
// world entity bs
int __cdecl NativeGetWorldEntities(int dimId, int **outBuf);
int __cdecl NativeGetChunkEntities(int dimId, int chunkX, int chunkZ, int **outBuf);
// block info (light, biome)
int __cdecl NativeGetSkyLight(int dimId, int x, int y, int z);

View file

@ -394,6 +394,7 @@ set(_MINECRAFT_SERVER_COMMON_ROOT
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/ScrolledSelectionList.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/SelectWorldScreen.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/ServerChunkCache.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/ServerChunkCache.h"
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/ServerCommandDispatcher.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/ServerConnection.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../Minecraft.Client/ServerLevel.cpp"