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 bc7c2b9 commit a1c4fab
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public enum FeatureToggle implements IHotkeyTogglable, IConfigNotifiable<IConfig
//TWEAK_CUSTOM_FLAT_PRESETS ("tweakCustomFlatPresets", false, "", "Allows adding custom flat world presets to the list.\nThe presets are defined in Lists -> flatWorldPresets"),
TWEAK_CUSTOM_FLY_DECELERATION ("tweakCustomFlyDeceleration", false, "", "Allows changing the fly deceleration in creative or spectator mode.\nThis is mainly meant for faster deceleration ie. less \"glide\"\nwhen releasing the movement keys.\nSee Generic -> flyDecelerationRampValue"),
TWEAK_CUSTOM_INVENTORY_GUI_SCALE("tweakCustomInventoryScreenScale", false, "", "Allows using a custom GUI scale for any inventory screen.\nSee Generic -> §ecustomInventoryGuiScale§r for the scale value"),
TWEAK_DISABLE_SERVER_DATA_SYNC ("tweakDisableServerDataSync", false, "", "Disables Server Data Syncer for entities such as Shulker Boxes,\nmaking inventoryPreview cannot render correct data on servers.\nYou must be an operator of the server or install some server-side\nmods to make the syncer work even this option is false."),
TWEAK_ELYTRA_CAMERA ("tweakElytraCamera", false, "", "Allows locking the real player rotations while holding the 'elytraCamera' activation key.\nThe controls will then only affect the separate 'camera rotations' for the rendering/camera.\nMeant for things like looking down/around while elytra flying nice and straight."),
TWEAK_ENTITY_REACH_OVERRIDE ("tweakEntityReachOverride", false, true, "", "Overrides the entity reach distance with\nthe one set in Generic -> entityReachDistance"),
TWEAK_ENTITY_TYPE_ATTACK_RESTRICTION("tweakEntityTypeAttackRestriction",false, "", "Restricts which entities you are able to attack (manually).\nSee the corresponding 'entityAttackRestriction*' configs in the Lists category."),
Expand Down Expand Up @@ -88,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_DATA_SYNC ("tweakServerDataSync", false, "", "Use Server Data Syncer for entities such as Shulker Boxes,\nwhich allows inventoryPreview to work on servers.\nYou must be an operator of the server or install server-side\nmods to make the syncer work even this option is set to true."),
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
53 changes: 31 additions & 22 deletions src/main/java/fi/dy/masa/tweakeroo/data/ServerDataSyncer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package fi.dy.masa.tweakeroo.data;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.jetbrains.annotations.Nullable;
import com.mojang.datafixers.util.Either;
import fi.dy.masa.tweakeroo.Tweakeroo;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.mixin.IMixinDataQueryHandler;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.ChestBlock;
Expand All @@ -14,24 +17,20 @@
import net.minecraft.client.network.DataQueryHandler;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.inventory.DoubleInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import fi.dy.masa.malilib.util.InfoUtils;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.mixin.IMixinDataQueryHandler;

@SuppressWarnings({"deprecation"})
public class ServerDataSyncer {
public class ServerDataSyncer
{
private static ServerDataSyncer INSTANCE;

public static ServerDataSyncer getInstance()
Expand Down Expand Up @@ -82,6 +81,7 @@ public ServerDataSyncer(ClientWorld world)
{
syncBlockEntity(clientWorld, pos);
}

return data.getLeft();
}

Expand All @@ -100,6 +100,7 @@ public ServerDataSyncer(ClientWorld world)
{
syncEntity(networkId);
}

return data.getLeft();
}

Expand All @@ -108,8 +109,6 @@ public ServerDataSyncer(ClientWorld world)

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

CompletableFuture<@Nullable NbtCompound> future = pendingQueriesById.remove(transactionId);
if (future != null)
{
Expand All @@ -131,15 +130,15 @@ public void handleQueryResponse(int transactionId, NbtCompound nbt)

public Inventory getBlockInventory(World world, BlockPos pos)
{
if (FeatureToggle.TWEAK_DISABLE_SERVER_DATA_SYNC.getBooleanValue())
if (FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue() == false)
{
return null;
}
else if (yesIAmOp.isPresent() && !yesIAmOp.get())
{
InfoUtils.printActionbarMessage("tweakeroo.message.warning.server_data_syncer.not_op");
return null;
}
Tweakeroo.logger.debug("getBlockInventory: pos [{}], op status: {}", pos.toShortString(), yesIAmOp);

if (!world.isChunkLoaded(pos)) return null;
if (getCache(pos) instanceof Inventory inv)
Expand Down Expand Up @@ -169,6 +168,7 @@ else if (yesIAmOp.isPresent() && !yesIAmOp.get())
}
}
}

return inv;
}

Expand All @@ -192,13 +192,17 @@ else if (yesIAmOp.isPresent() && !yesIAmOp.get())
}
}
}

return null;
}

public CompletableFuture<NbtCompound> syncBlockEntity(World world, BlockPos pos)
{
Tweakeroo.logger.debug("syncBlockEntity: pos [{}], op status: {}", pos.toShortString(), yesIAmOp);
if (yesIAmOp.isPresent() && !yesIAmOp.get()) return CompletableFuture.completedFuture(null);
if (yesIAmOp.isPresent() && !yesIAmOp.get())
{
InfoUtils.printActionbarMessage("tweakeroo.message.warning.server_data_syncer.not_op");
return CompletableFuture.completedFuture(null);
}

if (MinecraftClient.getInstance().isIntegratedServerRunning())
{
Expand Down Expand Up @@ -242,6 +246,7 @@ public CompletableFuture<NbtCompound> syncBlockEntity(World world, BlockPos pos)
{
yesIAmOp = Optional.of(false);
}

return future;
}
else
Expand All @@ -252,8 +257,11 @@ public CompletableFuture<NbtCompound> syncBlockEntity(World world, BlockPos pos)

public CompletableFuture<NbtCompound> syncEntity(int networkId)
{
Tweakeroo.logger.debug("syncEntity: pos [{}], op status: {}", networkId, yesIAmOp);
if (yesIAmOp.isPresent() && !yesIAmOp.get()) return CompletableFuture.completedFuture(null);
if (yesIAmOp.isPresent() && !yesIAmOp.get())
{
InfoUtils.printActionbarMessage("tweakeroo.message.warning.server_data_syncer.not_op");
return CompletableFuture.completedFuture(null);
}

Either<BlockPos, Integer> idEither = Either.right(networkId);
if (MinecraftClient.getInstance().getNetworkHandler() != null)
Expand Down Expand Up @@ -286,6 +294,7 @@ public CompletableFuture<NbtCompound> syncEntity(int networkId)
{
yesIAmOp = Optional.of(false);
}

return future;
}
else
Expand All @@ -296,22 +305,22 @@ public CompletableFuture<NbtCompound> syncEntity(int networkId)

public @Nullable Entity getServerEntity(Entity entity)
{
if (FeatureToggle.TWEAK_DISABLE_SERVER_DATA_SYNC.getBooleanValue())
if (FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue() == false)
{
return null;
}
else if (yesIAmOp.isPresent() && !yesIAmOp.get())
{
return null;
}
Tweakeroo.logger.debug("getServerEntity: id [{}], type {}, op status: {}", entity.getId(), EntityType.getId(entity.getType()), yesIAmOp);

Entity serverEntity = getCache(entity.getId());
if (serverEntity == null)
{
syncEntity(entity.getId());
return null;
}

return serverEntity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.client.world.ClientWorld;
import fi.dy.masa.malilib.interfaces.IWorldLoadListener;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.data.ServerDataSyncer;

public class WorldLoadListener implements IWorldLoadListener
{
Expand All @@ -26,5 +27,7 @@ public void onWorldLoadPost(@Nullable ClientWorld worldBefore, @Nullable ClientW
FeatureToggle.TWEAK_GAMMA_OVERRIDE.setBooleanValue(true);
}
}

ServerDataSyncer.resetInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void onPlayerDeath(DeathMessageS2CPacket packetIn, CallbackInfo ci)
)
private void onCommandTree(CallbackInfo ci)
{
if (!FeatureToggle.TWEAK_DISABLE_SERVER_DATA_SYNC.getBooleanValue())
if (FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue())
{
// when the player becomes OP, the server sends the command tree to the client
ServerDataSyncer.getInstance().recheckOpStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void queryResponse(int transactionId, NbtCompound nbt, CallbackInfoRetur
{
Tweakeroo.logger.debug("MixinDataQueryHandler: nbt {}", nbt.toString());

if (!FeatureToggle.TWEAK_DISABLE_SERVER_DATA_SYNC.getBooleanValue())
if (FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue())
{
ServerDataSyncer.getInstance().handleQueryResponse(transactionId, nbt);
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/fi/dy/masa/tweakeroo/mixin/MixinMinecraftClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fi.dy.masa.tweakeroo.mixin;

import fi.dy.masa.tweakeroo.data.ServerDataSyncer;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -131,13 +130,4 @@ private void onProcessKeybindsPre(CallbackInfo ci)
}
}
}

@Inject(
method = "setWorld",
at = @At("HEAD")
)
private void onWorldChanged(ClientWorld world, CallbackInfo ci)
{
ServerDataSyncer.resetInstance();
}
}
42 changes: 37 additions & 5 deletions src/main/java/fi/dy/masa/tweakeroo/renderer/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public class RenderUtils

public static void renderHotbarSwapOverlay(MinecraftClient mc, DrawContext drawContext)
{
if (mc.player == null)
{
return;
}
PlayerEntity player = mc.player;

if (player != null && mc.currentScreen == null)
if (mc.currentScreen == null)
{
final int scaledWidth = GuiUtils.getScaledWindowWidth();
final int scaledHeight = GuiUtils.getScaledWindowHeight();
Expand Down Expand Up @@ -120,6 +124,11 @@ public static void renderInventoryOverlay(MinecraftClient mc, DrawContext drawCo
World world = fi.dy.masa.malilib.util.WorldUtils.getBestWorld(mc);
Entity cameraEntity = EntityUtils.getCameraEntity();

if (mc.player == null)
{
return;
}

if (cameraEntity == mc.player && world instanceof ServerWorld)
{
// We need to get the player from the server world (if available, ie. in single player),
Expand Down Expand Up @@ -150,7 +159,7 @@ public static void renderInventoryOverlay(MinecraftClient mc, DrawContext drawCo

inv = fi.dy.masa.malilib.util.InventoryUtils.getInventory(world, pos);
if (world.isClient && world.getBlockState(pos).getBlock() instanceof BlockEntityProvider
&& !FeatureToggle.TWEAK_DISABLE_SERVER_DATA_SYNC.getBooleanValue())
&& FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue())
{
inv = ServerDataSyncer.getInstance().getBlockInventory(world, pos);
}
Expand All @@ -160,7 +169,7 @@ else if (trace.getType() == HitResult.Type.ENTITY)
Entity entity = ((EntityHitResult) trace).getEntity();

if (entity.getWorld().isClient &&
!FeatureToggle.TWEAK_DISABLE_SERVER_DATA_SYNC.getBooleanValue())
FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue())
{
Entity serverEntity = ServerDataSyncer.getInstance().getServerEntity(entity);
if (serverEntity != null)
Expand Down Expand Up @@ -193,7 +202,7 @@ else if (entity instanceof AbstractHorseEntity)
int x = xCenter - 52 / 2;
int y = yCenter - 92;

if (inv != null && inv.size() > 0)
if (inv != null && !inv.isEmpty())
{
final boolean isHorse = (entityLivingBase instanceof AbstractHorseEntity);
final int totalSlots = isHorse ? inv.size() - 2 : inv.size();
Expand Down Expand Up @@ -243,6 +252,18 @@ else if (entity instanceof AbstractHorseEntity)

public static void renderPlayerInventoryOverlay(MinecraftClient mc, DrawContext drawContext)
{
if (mc.player == null)
{
return;
}

Inventory inv = mc.player.getInventory();

if (inv.isEmpty())
{
return;
}

int x = GuiUtils.getScaledWindowWidth() / 2 - 176 / 2;
int y = GuiUtils.getScaledWindowHeight() / 2 + 10;
int slotOffsetX = 8;
Expand All @@ -252,12 +273,23 @@ public static void renderPlayerInventoryOverlay(MinecraftClient mc, DrawContext
fi.dy.masa.malilib.render.RenderUtils.color(1f, 1f, 1f, 1f);

fi.dy.masa.malilib.render.InventoryOverlay.renderInventoryBackground(type, x, y, 9, 27, mc);
fi.dy.masa.malilib.render.InventoryOverlay.renderInventoryStacks(type, mc.player.getInventory(), x + slotOffsetX, y + slotOffsetY, 9, 9, 27, mc, drawContext);
fi.dy.masa.malilib.render.InventoryOverlay.renderInventoryStacks(type, inv, x + slotOffsetX, y + slotOffsetY, 9, 9, 27, mc, drawContext);
}

public static void renderHotbarScrollOverlay(MinecraftClient mc, DrawContext drawContext)
{
if (mc.player == null)
{
return;
}

Inventory inv = mc.player.getInventory();

if (inv.isEmpty())
{
return;
}

final int xCenter = GuiUtils.getScaledWindowWidth() / 2;
final int yCenter = GuiUtils.getScaledWindowHeight() / 2;
final int x = xCenter - 176 / 2;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/tweakeroo/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
"tweakeroo.message.value.on": "ON",

"tweakeroo.message.warning.block_type_break_restriction": "§6Block breaking prevented by Block Type Break Restriction tweak",
"tweakeroo.message.warning.entity_type_attack_restriction": "§6Entity attack prevented by Entity Type Attack Restriction tweak"
"tweakeroo.message.warning.entity_type_attack_restriction": "§6Entity attack prevented by Entity Type Attack Restriction tweak",
"tweakeroo.message.warning.server_data_syncer.not_op": "§6Unable to obtain entity data when you are not a Server Operator"
}

0 comments on commit a1c4fab

Please sign in to comment.