Skip to content

Commit

Permalink
feat BaseChunkData
Browse files Browse the repository at this point in the history
  • Loading branch information
a3510377 committed Mar 23, 2024
1 parent 10be928 commit 1b8e0e5
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 54 deletions.
14 changes: 10 additions & 4 deletions src/main/java/monkey/unlimitedtrade/AutoTradeModClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import it.unimi.dsi.fastutil.ints.IntArrayList;
import monkey.unlimitedtrade.config.Configs;
import monkey.unlimitedtrade.config.types.AfterTradeActions;
import monkey.unlimitedtrade.utils.chunkdebug.BaseChunkData;
import monkey.unlimitedtrade.utils.chunkdebug.BaseChunkDebug;
import monkey.unlimitedtrade.utils.chunkdebug.ChunkDebugAPI;
import monkey.unlimitedtrade.utils.chunkdebug.essential.EssentialChunkDebugAPI;
Expand Down Expand Up @@ -127,16 +128,21 @@ else if (client.crosshairTarget instanceof EntityHitResult hitResult) {
}

// if in listening world != villageOldWorld re-listen
if (CHUNK_DEBUG_API != null && CHUNK_DEBUG_API.getCurrentWorld() != null && !CHUNK_DEBUG_API.getCurrentWorld().equals(world)) {
if (CHUNK_DEBUG_API != null && (CHUNK_DEBUG_API.getCurrentWorld() == null || !CHUNK_DEBUG_API.getCurrentWorld().equals(world))) {
CHUNK_DEBUG_API.requestChunkData(world);
}
}
} else if (tradeEntity != null && GuiUtils.getCurrentScreen() instanceof MerchantScreen merchantScreen) {
if (client.player != null && client.player.isSneaking()) return;

if (!Configs.waitChunkDebug.getBooleanValue() || CHUNK_DEBUG_API != null &&
CHUNK_DEBUG_API.getChunkData(tradeEntity.getChunkPos()).levelType() == ChunkLevelType.INACCESSIBLE) {
AutoTradeModClient.startTrade(client, merchantScreen);
if (!Configs.waitChunkDebug.getBooleanValue() || CHUNK_DEBUG_API != null) {
@Nullable BaseChunkData chunkData = CHUNK_DEBUG_API.getChunkData(tradeEntity.getChunkPos());

LOGGER.info(String.valueOf(chunkData));
if (chunkData != null && chunkData.getLevelType() == ChunkLevelType.INACCESSIBLE) {
LOGGER.info("Start");
AutoTradeModClient.startTrade(client, merchantScreen);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import monkey.unlimitedtrade.config.options.AutoTradeIConfigBase;

public record AutoTradeConfigOption(Config annotation, AutoTradeIConfigBase config) {

public Config.Category getCategory() {
return this.annotation.category();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void load() {
public void loadFromFile(File configFile) {
JsonElement element = JsonUtils.parseJsonFile(configFile);

AutoTradeModClient.LOGGER.info(String.valueOf(element));
if (element != null && element.isJsonObject()) {
this.jsonObject = element.getAsJsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public AutoTradGuiConfigsBase(String defaultTab) {

public static void openGui() {
GuiBase.openGui(new AutoTradGuiConfigsBase());
AutoTradeModClient.LOGGER.info("open ui test");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package monkey.unlimitedtrade.utils.chunkdebug;

import net.minecraft.server.world.ChunkLevelType;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ChunkPos;

public class BaseChunkData {
private final ChunkPos chunkPos;
private final ChunkLevelType levelType;
private final Identifier serverWorld;

public BaseChunkData(ChunkPos chunkPos, ChunkLevelType levelType, Identifier serverWorld) {
this.chunkPos = chunkPos;
this.levelType = levelType;
this.serverWorld = serverWorld;
}

public ChunkPos getChunkPos() {
return chunkPos;
}

public ChunkLevelType getLevelType() {
return levelType;
}

public Identifier getServerWorld() {
return serverWorld;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void requestChunkData(Identifier world) {

public abstract void clearChunkData();

public abstract ChunkData getChunkData(ChunkPos chunkPos);
public abstract @Nullable BaseChunkData getChunkData(ChunkPos chunkPos);

public @Nullable Identifier getCurrentWorld() {
return this.currentWorld;
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/monkey/unlimitedtrade/utils/chunkdebug/ChunkData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ChunkPos;

public record ChunkData(
ChunkPos chunkPos,
ChunkLevelType levelType,
byte statusCode,
byte ticketCode,
Identifier serverWorld
) {
public class ChunkData extends BaseChunkData {
byte statusCode;
byte ticketCode;

public ChunkData(ChunkPos chunkPos, ChunkLevelType levelType, byte statusCode, byte ticketCode, Identifier serverWorld) {
super(chunkPos, levelType, serverWorld);
this.statusCode = statusCode;
this.ticketCode = ticketCode;
}

public static ChunkData deserialize(Identifier world, long chunkPosition, byte levelType, byte statusType, byte ticketType) {
return new ChunkData(new ChunkPos(chunkPosition), ChunkLevelType.values()[levelType], statusType, ticketType, world);
}
}

public byte getStatusCode() {
return statusCode;
}

//public ChunkPos(long pos) {
// this.x = (int)pos;
// this.z = (int)(pos >> 32);
//}
public byte getTicketCode() {
return ticketCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ private void onData(PacketByteBuf buf) {

for (int i = 0; i < size; i++) {
ChunkData chunkData = ChunkData.deserialize(worldIdentify, chunkPositions[i], levelTypes[i], statusTypes[i], ticketTypes[i]);
worldChunks.put(chunkData.chunkPos(), chunkData);
worldChunks.put(chunkData.getChunkPos(), chunkData);
}

this.UPDATE_EVENT.invoker().onUpdate(worldIdentify, worldChunks);
}

@Override
public ChunkData getChunkData(ChunkPos chunkPos) {
public @Nullable BaseChunkData getChunkData(ChunkPos chunkPos) {
return this.worldChunks.get(chunkPos);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package monkey.unlimitedtrade.utils.chunkdebug.essential;

import monkey.unlimitedtrade.AutoTradeModClient;
import monkey.unlimitedtrade.utils.chunkdebug.BaseChunkData;
import monkey.unlimitedtrade.utils.chunkdebug.BaseChunkDebug;
import monkey.unlimitedtrade.utils.chunkdebug.ChunkData;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ChunkLevelType;
Expand All @@ -12,12 +12,14 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class EssentialChunkDebugAPI extends BaseChunkDebug {
@Nullable Class<?> essentialClientClass;
@Nullable Class<IChunkHandler> chunkHandlerClass;
@Nullable Class<IChunkTypeEnum> chunkTypeEnumClass;
Map<Object, Integer> chunkTypeEnumMaps = new HashMap<>();

@Nullable Method clearAllChunksMethod;
@Nullable Method getChunksMethod;
Expand All @@ -32,6 +34,16 @@ public EssentialChunkDebugAPI() {
essentialClientClass = Class.forName("me.senseiwells.essentialclient.EssentialClient");
//noinspection unchecked
chunkHandlerClass = (Class<IChunkHandler>) Class.forName("me.senseiwells.essentialclient.feature.chunkdebug.ChunkHandler");
//noinspection unchecked
chunkTypeEnumClass = (Class<IChunkTypeEnum>) Class.forName("me.senseiwells.essentialclient.feature.chunkdebug.ChunkType");
if (chunkTypeEnumClass.isEnum()) {
Object[] values = (Object[]) chunkTypeEnumClass.getMethod("values").invoke(chunkTypeEnumClass);

int i = 0;
for (Object value : values) {
chunkTypeEnumMaps.put(value, i++);
}
}

clearAllChunksMethod = chunkHandlerClass.getMethod("clearAllChunks");
getChunksMethod = chunkHandlerClass.getMethod("getChunks", RegistryKey.class);
Expand All @@ -57,36 +69,27 @@ public void clearAllChunks() {
}
}

public List<ChunkData> objectToChunkData(Identifier world, Object[] chunkData) {
List<ChunkData> result = new ArrayList<>();

for (Object data : chunkData) {
try {
//noinspection unchecked
Class<IChunkData> chunkDataCls = (Class<IChunkData>) data.getClass();

int x = (int) chunkDataCls.getMethod("getPosX").invoke(data);
int y = (int) chunkDataCls.getMethod("getPosZ").invoke(data);

result.add(new ChunkData(
new ChunkPos(x, y),
ChunkLevelType.INACCESSIBLE,
(byte) 0,
(byte) 0,
world));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public BaseChunkData objectToChunkData(Identifier world, Object chunkData) {
try {
//noinspection unchecked
Class<IChunkData> chunkDataCls = (Class<IChunkData>) chunkData.getClass();

int x = (int) chunkDataCls.getMethod("getPosX").invoke(chunkData);
int y = (int) chunkDataCls.getMethod("getPosZ").invoke(chunkData);

return result;
Object chunkTypeClass = chunkDataCls.getMethod("getChunkType").invoke(chunkData);
int chunkLevelTypeIndex = chunkTypeEnumMaps.getOrDefault(chunkTypeClass, -1);
return new BaseChunkData(new ChunkPos(x, y), ChunkLevelType.values()[chunkLevelTypeIndex], world);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

public List<ChunkData> getChunks() {
public Object[] getChunks() {
if (getChunksMethod == null) return null;

try {
return objectToChunkData(getCurrentWorld(), (Object[]) getChunksMethod.invoke(chunkHandlerClass, RegistryKey.of(RegistryKeys.WORLD, getCurrentWorld())));
return (Object[]) getChunksMethod.invoke(chunkHandlerClass, RegistryKey.of(RegistryKeys.WORLD, getCurrentWorld()));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
Expand All @@ -98,12 +101,12 @@ public void clearChunkData() {
}

@Override
public ChunkData getChunkData(ChunkPos chunkPos) {
List<ChunkData> chunkData = getChunks();
public @Nullable BaseChunkData getChunkData(ChunkPos chunkPos) {
for (Object chunkObject : getChunks()) {
BaseChunkData chunkData = objectToChunkData(getCurrentWorld(), chunkObject);

for (ChunkData chunk : chunkData) {
if (chunkPos.equals(chunk.chunkPos())) {
return chunk;
if (chunkPos.equals(chunkData.getChunkPos())) {
return chunkData;
}
}

Expand All @@ -123,9 +126,11 @@ public void requestChunkData(Identifier world) {
if (this.removeChunkDataMethod == null) {
AutoTradeModClient.LOGGER.error("run EssentialChunkDebug but removeChunkDataMethod is null");
} else {
AutoTradeModClient.LOGGER.debug("remove chunk data of world: {}", world);
this.removeChunkDataMethod.invoke(chunkClientNetworkHandler);
}
} else if (this.requestChunkDataMethod != null) {
AutoTradeModClient.LOGGER.debug("request chunk data of world: {}", world);
this.requestChunkDataMethod.invoke(chunkClientNetworkHandler, RegistryKey.of(RegistryKeys.WORLD, world));
} else {
AutoTradeModClient.LOGGER.error("run EssentialChunkDebug but requestChunkDataMethod is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface IChunkData {
int getPosX();

int getPosZ();

IChunkType getChunkType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package monkey.unlimitedtrade.utils.chunkdebug.essential;

public interface IChunkTypeEnum {
IChunkTypeEnum[] values();
}

0 comments on commit 1b8e0e5

Please sign in to comment.