Skip to content

Commit

Permalink
Added setting for the Server Data Syncer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jun 20, 2024
1 parent c980f89 commit a4d837d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ author = masa
mod_file_name = tweakeroo-fabric

# Current mod version
mod_version = 0.20.999-sakura.3
mod_version = 0.20.999-sakura.4

# Required malilib version
malilib_version = 0.19.999-sakura.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public enum FeatureToggle implements IHotkeyTogglable, IConfigNotifiable<IConfig
TWEAK_RENDER_LIMIT_ENTITIES ("tweakRenderLimitEntities", false, "", "Enables limiting the number of certain types of entities\nto render per frame. Currently XP Orbs and Item entities\nare supported, see Generic configs for the limits."),
TWEAK_REPAIR_MODE ("tweakRepairMode", false, "", "If enabled, then fully repaired items held in hand will\nbe swapped to damaged items that have Mending on them."),
TWEAK_SCULK_PULSE_LENGTH ("tweakSculkPulseLength", false, true, "", "Allows modifying the Sculk Sensor pulse length. Set the pulse length in Generic -> sculkSensorPulseLength"),
TWEAK_SERVER_ENTITY_DATA_SYNCER ("tweakServerEntityDataSyncer", false, "", "Allows Tweakeroo to attempt to use the Server Data Syncer\nto obtain various Entity Data, such as for Shulker Boxes\n\n§6NOTE: This feature requires OP privileges to work."),
TWEAK_SHULKERBOX_DISPLAY ("tweakShulkerBoxDisplay", false, "", "Enables the Shulker Box contents display when hovering\nover them in an inventory and holding shift"),
TWEAK_SIGN_COPY ("tweakSignCopy", false, "", "When enabled, placed signs will use the text from\nthe previously placed sign.\nCan be combined with tweakNoSignGui to quickly place copies\nof a sign, by enabling that tweak after making the first sign."),
TWEAK_SNAP_AIM ("tweakSnapAim", false, "", KeybindSettings.INGAME_BOTH, "Enabled a snap aim tweak, to make the player face to pre-set exact yaw rotations"),
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/fi/dy/masa/tweakeroo/data/ServerDataSyncer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fi.dy.masa.tweakeroo.data;

import com.mojang.datafixers.util.Either;
import fi.dy.masa.tweakeroo.Tweakeroo;
import fi.dy.masa.tweakeroo.mixin.IMixinDataQueryHandler;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -66,7 +67,10 @@ public ServerDataSyncer(ClientWorld world) {
return null;
}

public void handleQueryResponse(int transactionId, NbtCompound nbt) {
public void handleQueryResponse(int transactionId, NbtCompound nbt)
{
Tweakeroo.logger.warn("handleQueryResponse: id [{}] // nbt {}", transactionId, nbt.toString());

if (nbt == null) return;
if (pendingQueries.containsKey(transactionId)) {
Either<BlockPos, Integer> either = pendingQueries.remove(transactionId);
Expand Down Expand Up @@ -96,7 +100,10 @@ public void handleQueryResponse(int transactionId, NbtCompound nbt) {
}
}

public Inventory getBlockInventory(World world, BlockPos pos) {
public Inventory getBlockInventory(World world, BlockPos pos)
{
Tweakeroo.logger.warn("getBlockInventory: pos [{}]", pos.toShortString());

if (!world.isChunkLoaded(pos)) return null;
var data = getCache(pos);
if (data instanceof Inventory inv) {
Expand Down Expand Up @@ -131,8 +138,12 @@ public Inventory getBlockInventory(World world, BlockPos pos) {
return null;
}

public void syncBlockEntity(World world, BlockPos pos) {
if (MinecraftClient.getInstance().isIntegratedServerRunning()) {
public void syncBlockEntity(World world, BlockPos pos)
{
Tweakeroo.logger.warn("syncBlockEntity: pos [{}]", pos.toShortString());

if (MinecraftClient.getInstance().isIntegratedServerRunning())
{
BlockEntity blockEntity = MinecraftClient.getInstance().getServer().getWorld(world.getRegistryKey()).getWorldChunk(pos).getBlockEntity(pos, WorldChunk.CreationType.CHECK);
if (blockEntity != null) {
blockCache.put(pos, new Pair<>(blockEntity, System.currentTimeMillis()));
Expand All @@ -147,7 +158,10 @@ public void syncBlockEntity(World world, BlockPos pos) {
}
}

public void syncEntity(int networkId) {
public void syncEntity(int networkId)
{
Tweakeroo.logger.warn("syncEntity: pos [{}]", networkId);

Either<BlockPos, Integer> idEither = Either.right(networkId);
if (MinecraftClient.getInstance().getNetworkHandler() != null && !pendingQueries.containsValue(idEither)) {
DataQueryHandler handler = MinecraftClient.getInstance().getNetworkHandler().getDataQueryHandler();
Expand All @@ -156,7 +170,8 @@ public void syncEntity(int networkId) {
}
}

public @Nullable Entity getServerEntity(Entity entity) {
public @Nullable Entity getServerEntity(Entity entity)
{
Entity serverEntity = getCache(entity.getId());
if (serverEntity == null) {
syncEntity(entity.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fi.dy.masa.tweakeroo.mixin;

import fi.dy.masa.tweakeroo.Tweakeroo;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.data.ServerDataSyncer;
import net.minecraft.client.network.DataQueryHandler;
import net.minecraft.nbt.NbtCompound;
Expand All @@ -9,12 +11,19 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(DataQueryHandler.class)
public class MixinDataQueryHandler {
public class MixinDataQueryHandler
{
@Inject(
method = "handleQueryResponse",
at = @At("HEAD")
)
private void queryResponse(int transactionId, NbtCompound nbt, CallbackInfoReturnable<Boolean> cir) {
ServerDataSyncer.INSTANCE.handleQueryResponse(transactionId, nbt);
private void queryResponse(int transactionId, NbtCompound nbt, CallbackInfoReturnable<Boolean> cir)
{
Tweakeroo.logger.warn("MixinDataQueryHandler: nbt {}", nbt.toString());

if (FeatureToggle.TWEAK_SERVER_ENTITY_DATA_SYNCER.getBooleanValue())
{
ServerDataSyncer.INSTANCE.handleQueryResponse(transactionId, nbt);
}
}
}
14 changes: 10 additions & 4 deletions src/main/java/fi/dy/masa/tweakeroo/mixin/MixinMinecraftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ private void onProcessKeybindsPre(CallbackInfo ci)
)
private void onWorldChanged(ClientWorld world, CallbackInfo ci)
{
if (world == null) {
ServerDataSyncer.INSTANCE = null;
} else {
ServerDataSyncer.INSTANCE = new ServerDataSyncer(world);
if (FeatureToggle.TWEAK_SERVER_ENTITY_DATA_SYNCER.getBooleanValue())
{
if (world == null)
{
ServerDataSyncer.INSTANCE = null;
}
else
{
ServerDataSyncer.INSTANCE = new ServerDataSyncer(world);
}
}
}
}
39 changes: 22 additions & 17 deletions src/main/java/fi/dy/masa/tweakeroo/renderer/RenderUtils.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package fi.dy.masa.tweakeroo.renderer;

import java.util.Set;
import org.joml.Matrix4f;
import org.joml.Matrix4fStack;
import com.mojang.blaze3d.systems.RenderSystem;
import fi.dy.masa.malilib.util.EntityUtils;
import fi.dy.masa.malilib.util.GuiUtils;
import fi.dy.masa.tweakeroo.config.Configs;
import fi.dy.masa.tweakeroo.data.ServerDataSyncer;
import fi.dy.masa.tweakeroo.mixin.IMixinAbstractHorseEntity;
import fi.dy.masa.tweakeroo.util.MiscUtils;
import fi.dy.masa.tweakeroo.util.RayTraceUtils;
import fi.dy.masa.tweakeroo.util.SnapAimMode;

import net.minecraft.block.Block;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.client.MinecraftClient;
Expand All @@ -37,10 +30,15 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.joml.Matrix4f;
import org.joml.Matrix4fStack;

import java.util.Set;
import fi.dy.masa.malilib.util.EntityUtils;
import fi.dy.masa.malilib.util.GuiUtils;
import fi.dy.masa.tweakeroo.config.Configs;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.data.ServerDataSyncer;
import fi.dy.masa.tweakeroo.mixin.IMixinAbstractHorseEntity;
import fi.dy.masa.tweakeroo.util.MiscUtils;
import fi.dy.masa.tweakeroo.util.RayTraceUtils;
import fi.dy.masa.tweakeroo.util.SnapAimMode;

public class RenderUtils
{
Expand Down Expand Up @@ -149,18 +147,25 @@ public static void renderInventoryOverlay(MinecraftClient mc, DrawContext drawCo
shulkerBoxBlock = (ShulkerBoxBlock) blockTmp;
}

if (world instanceof ServerWorld realWorld) {
if (world instanceof ServerWorld realWorld)
{
inv = fi.dy.masa.malilib.util.InventoryUtils.getInventory(realWorld, pos);
} else {
}
else if (FeatureToggle.TWEAK_SERVER_ENTITY_DATA_SYNCER.getBooleanValue())
{
inv = ServerDataSyncer.INSTANCE.getBlockInventory(world, pos);
}
}
else if (trace.getType() == HitResult.Type.ENTITY)
{
Entity entity = ((EntityHitResult) trace).getEntity();
if (entity.getWorld().isClient) {

if (entity.getWorld().isClient &&
FeatureToggle.TWEAK_SERVER_ENTITY_DATA_SYNCER.getBooleanValue())
{
Entity serverEntity = ServerDataSyncer.INSTANCE.getServerEntity(entity);
if (serverEntity != null) {
if (serverEntity != null)
{
entity = serverEntity;
}
}
Expand Down

0 comments on commit a4d837d

Please sign in to comment.