Skip to content

Commit

Permalink
fix: Remove the wrong "Pair" and use Apache Commons
Browse files Browse the repository at this point in the history
feat: add New InventoryOverlayHandler system; which now makes Tweakeroo's InventoryPreview work the same as MiniHUD.
  • Loading branch information
sakura-ryoko committed Dec 30, 2024
1 parent 94ff930 commit 7090549
Show file tree
Hide file tree
Showing 11 changed files with 566 additions and 363 deletions.
30 changes: 18 additions & 12 deletions src/main/java/fi/dy/masa/tweakeroo/config/Callbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,20 @@
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import fi.dy.masa.malilib.config.IConfigBase;

import fi.dy.masa.malilib.config.IConfigBoolean;
import fi.dy.masa.malilib.config.IHotkeyTogglable;
import fi.dy.masa.malilib.config.options.ConfigBoolean;
import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.hotkeys.IHotkeyCallback;
import fi.dy.masa.malilib.hotkeys.IKeybind;
import fi.dy.masa.malilib.hotkeys.KeyAction;
import fi.dy.masa.malilib.hotkeys.KeyCallbackAdjustable;
import fi.dy.masa.malilib.hotkeys.KeyCallbackToggleBooleanConfigWithMessage;
import fi.dy.masa.malilib.hotkeys.*;
import fi.dy.masa.malilib.interfaces.IValueChangeCallback;
import fi.dy.masa.malilib.render.InventoryOverlayScreen;
import fi.dy.masa.malilib.util.InfoUtils;
import fi.dy.masa.malilib.util.StringUtils;
import fi.dy.masa.tweakeroo.gui.GuiConfigs;
import fi.dy.masa.tweakeroo.mixin.IMixinAbstractBlock;
import fi.dy.masa.tweakeroo.mixin.IMixinSimpleOption;
import fi.dy.masa.tweakeroo.util.CameraEntity;
import fi.dy.masa.tweakeroo.util.InventoryUtils;
import fi.dy.masa.tweakeroo.util.MiscUtils;
import fi.dy.masa.tweakeroo.util.PlacementRestrictionMode;
import fi.dy.masa.tweakeroo.util.SnapAimMode;
import fi.dy.masa.tweakeroo.renderer.InventoryOverlayHandler;
import fi.dy.masa.tweakeroo.util.*;

public class Callbacks
{
Expand Down Expand Up @@ -82,6 +75,7 @@ public static void init(MinecraftClient mc)
Hotkeys.HOTBAR_SWAP_2.getKeybind().setCallback(callbackGeneric);
Hotkeys.HOTBAR_SWAP_3.getKeybind().setCallback(callbackGeneric);
Hotkeys.HOTBAR_SCROLL.getKeybind().setCallback(callbackGeneric);
Hotkeys.INVENTORY_PREVIEW_TOGGLE_SCREEN.getKeybind().setCallback(callbackGeneric);
Hotkeys.OPEN_CONFIG_GUI.getKeybind().setCallback(callbackGeneric);
Hotkeys.PLACEMENT_RESTRICTION_MODE_COLUMN.getKeybind().setCallback(callbackGeneric);
Hotkeys.PLACEMENT_RESTRICTION_MODE_DIAGONAL.getKeybind().setCallback(callbackGeneric);
Expand Down Expand Up @@ -448,6 +442,18 @@ else if (key == Hotkeys.ZOOM_ACTIVATE.getKeybind())
//String.format("%s%.1f%s", GuiBase.TXT_GREEN, Configs.Generic.ZOOM_FOV.getDoubleValue(), GuiBase.TXT_RST));
}
}
else if (key == Hotkeys.INVENTORY_PREVIEW_TOGGLE_SCREEN.getKeybind())
{
if (mc.currentScreen instanceof InventoryOverlayScreen)
{
mc.setScreen(null);
}
else if (FeatureToggle.TWEAK_INVENTORY_PREVIEW.getBooleanValue() &&
Hotkeys.INVENTORY_PREVIEW.getKeybind().isKeybindHeld())
{
InventoryOverlayHandler.getInstance().refreshInventoryOverlay(mc, Configs.Generic.SHULKER_DISPLAY_BACKGROUND_COLOR.getBooleanValue());
}
}

return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/fi/dy/masa/tweakeroo/config/Hotkeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Hotkeys
public static final ConfigHotkey HOTBAR_SWAP_2 = new ConfigHotkey("hotbarSwap2", "").apply(HOTKEY_KEY);
public static final ConfigHotkey HOTBAR_SWAP_3 = new ConfigHotkey("hotbarSwap3", "").apply(HOTKEY_KEY);
public static final ConfigHotkey INVENTORY_PREVIEW = new ConfigHotkey("inventoryPreview", "LEFT_ALT", KeybindSettings.PRESS_ALLOWEXTRA).apply(HOTKEY_KEY);
public static final ConfigHotkey INVENTORY_PREVIEW_TOGGLE_SCREEN = new ConfigHotkey("inventoryPreviewToggleScreen", "BUTTON_3", KeybindSettings.create(KeybindSettings.Context.ANY, KeyAction.PRESS, true, true, false, true)).apply(HOTKEY_KEY);
public static final ConfigHotkey OPEN_CONFIG_GUI = new ConfigHotkey("openConfigGui", "X,C").apply(HOTKEY_KEY);
public static final ConfigHotkey PLACEMENT_Y_MIRROR = new ConfigHotkey("placementYMirror", "", KeybindSettings.PRESS_ALLOWEXTRA).apply(HOTKEY_KEY);
public static final ConfigHotkey PLAYER_INVENTORY_PEEK = new ConfigHotkey("playerInventoryPeek", "", KeybindSettings.PRESS_ALLOWEXTRA).apply(HOTKEY_KEY);
Expand Down Expand Up @@ -83,6 +84,7 @@ public class Hotkeys
HOTBAR_SWAP_2,
HOTBAR_SWAP_3,
INVENTORY_PREVIEW,
INVENTORY_PREVIEW_TOGGLE_SCREEN,
OPEN_CONFIG_GUI,
PLACEMENT_Y_MIRROR,
PLAYER_INVENTORY_PEEK,
Expand Down
33 changes: 26 additions & 7 deletions src/main/java/fi/dy/masa/tweakeroo/data/ServerDataSyncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import com.llamalad7.mixinextras.lib.apache.commons.tuple.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;

import com.mojang.datafixers.util.Either;
import net.minecraft.block.BlockEntityProvider;
Expand Down Expand Up @@ -35,6 +35,7 @@
import net.minecraft.world.World;

import fi.dy.masa.malilib.interfaces.IClientTickHandler;
import fi.dy.masa.malilib.interfaces.IDataSyncer;
import fi.dy.masa.malilib.network.ClientPlayHandler;
import fi.dy.masa.malilib.network.IPluginClientPlayHandler;
import fi.dy.masa.malilib.util.Constants;
Expand All @@ -52,7 +53,7 @@
import fi.dy.masa.tweakeroo.network.ServuxTweaksPacket;

@SuppressWarnings({"deprecation"})
public class ServerDataSyncer implements IClientTickHandler
public class ServerDataSyncer implements IClientTickHandler, IDataSyncer
{
private static final ServerDataSyncer INSTANCE = new ServerDataSyncer();
public static ServerDataSyncer getInstance()
Expand All @@ -78,13 +79,15 @@ public static ServerDataSyncer getInstance()
private final Map<Integer, Either<BlockPos, Integer>> transactionToBlockPosOrEntityId = new HashMap<>();
private ClientWorld clientWorld;

@Override
@Nullable
public World getWorld()
{
return WorldUtils.getBestWorld(mc);
}

private ClientWorld getClientWorld()
@Override
public ClientWorld getClientWorld()
{
if (this.clientWorld == null)
{
Expand Down Expand Up @@ -182,6 +185,7 @@ public IPluginClientPlayHandler<ServuxTweaksPacket.Payload> getNetworkHandler()
return HANDLER;
}

@Override
public void reset(boolean isLogout)
{
if (isLogout)
Expand Down Expand Up @@ -247,6 +251,7 @@ private void tickCache()
}
}

@Override
public @Nullable NbtCompound getFromBlockEntityCacheNbt(BlockPos pos)
{
if (this.blockEntityCache.containsKey(pos))
Expand All @@ -257,6 +262,7 @@ private void tickCache()
return null;
}

@Override
public @Nullable BlockEntity getFromBlockEntityCache(BlockPos pos)
{
if (this.blockEntityCache.containsKey(pos))
Expand All @@ -267,6 +273,7 @@ private void tickCache()
return null;
}

@Override
public @Nullable NbtCompound getFromEntityCacheNbt(int entityId)
{
if (this.entityCache.containsKey(entityId))
Expand All @@ -277,6 +284,7 @@ private void tickCache()
return null;
}

@Override
public @Nullable Entity getFromEntityCache(int entityId)
{
if (this.entityCache.containsKey(entityId))
Expand Down Expand Up @@ -336,12 +344,14 @@ public int getEntityCacheCount()
return this.entityCache.size();
}

@Override
public void onGameInit()
{
ClientPlayHandler.getInstance().registerClientPlayHandler(HANDLER);
HANDLER.registerPlayPayload(ServuxTweaksPacket.Payload.ID, ServuxTweaksPacket.Payload.CODEC, IPluginClientPlayHandler.BOTH_CLIENT);
}

@Override
public void onWorldPre()
{
if (DataManager.getInstance().hasIntegratedServer() == false)
Expand All @@ -350,6 +360,7 @@ public void onWorldPre()
}
}

@Override
public void onWorldJoin()
{
// NO-OP
Expand Down Expand Up @@ -398,6 +409,7 @@ public void onPacketFailure()
this.hasInValidServux = true;
}

@Override
public @Nullable Pair<BlockEntity, NbtCompound> requestBlockEntity(World world, BlockPos pos)
{
if (this.blockEntityCache.containsKey(pos))
Expand Down Expand Up @@ -431,7 +443,8 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
return null;
}

public @Nullable Pair<Entity, NbtCompound> requestEntity(int entityId)
@Override
public @Nullable Pair<Entity, NbtCompound> requestEntity(World world, int entityId)
{
if (this.entityCache.containsKey(entityId))
{
Expand Down Expand Up @@ -464,6 +477,7 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
return null;
}

@Override
@Nullable
public Inventory getBlockInventory(World world, BlockPos pos, boolean useNbt)
{
Expand Down Expand Up @@ -536,8 +550,9 @@ public Inventory getBlockInventory(World world, BlockPos pos, boolean useNbt)
return null;
}

@Override
@Nullable
public Inventory getEntityInventory(int entityId, boolean useNbt)
public Inventory getEntityInventory(World world, int entityId, boolean useNbt)
{
if (this.entityCache.containsKey(entityId) && this.getWorld() != null)
{
Expand Down Expand Up @@ -581,7 +596,7 @@ else if (entity instanceof PiglinEntity)

if (FeatureToggle.TWEAK_SERVER_DATA_SYNC.getBooleanValue())
{
this.requestEntity(entityId);
this.requestEntity(world, entityId);
}

return null;
Expand Down Expand Up @@ -641,6 +656,7 @@ private void requestServuxEntityData(int entityId)
}
}

@Override
@Nullable
public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullable Identifier type)
{
Expand Down Expand Up @@ -717,6 +733,7 @@ public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullabl
return null;
}

@Override
@Nullable
public Entity handleEntityData(int entityId, NbtCompound nbt)
{
Expand Down Expand Up @@ -750,11 +767,13 @@ public Entity handleEntityData(int entityId, NbtCompound nbt)
return entity;
}

@Override
public void handleBulkEntityData(int transactionId, NbtCompound nbt)
{
// todo
}

@Override
public void handleVanillaQueryNbt(int transactionId, NbtCompound nbt)
{
Either<BlockPos, Integer> either = this.transactionToBlockPosOrEntityId.remove(transactionId);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/fi/dy/masa/tweakeroo/event/RenderHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fi.dy.masa.tweakeroo.event;

import com.llamalad7.mixinextras.lib.apache.commons.tuple.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.joml.Matrix4f;

import com.mojang.blaze3d.systems.RenderSystem;
Expand All @@ -25,7 +25,6 @@

import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.interfaces.IRenderer;
import fi.dy.masa.malilib.render.InventoryOverlay;
import fi.dy.masa.malilib.util.ActiveMode;
import fi.dy.masa.malilib.util.Color4f;
import fi.dy.masa.malilib.util.InventoryUtils;
Expand All @@ -35,8 +34,8 @@
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.config.Hotkeys;
import fi.dy.masa.tweakeroo.data.ServerDataSyncer;
import fi.dy.masa.tweakeroo.renderer.InventoryOverlayHandler;
import fi.dy.masa.tweakeroo.renderer.RenderUtils;
import fi.dy.masa.tweakeroo.util.RayTraceUtils;

public class RenderHandler implements IRenderer
{
Expand Down Expand Up @@ -70,12 +69,16 @@ else if (FeatureToggle.TWEAK_HOTBAR_SCROLL.getBooleanValue() &&
if (FeatureToggle.TWEAK_INVENTORY_PREVIEW.getBooleanValue() &&
Hotkeys.INVENTORY_PREVIEW.getKeybind().isKeybindHeld())
{
/*
InventoryOverlay.Context context = RayTraceUtils.getTargetInventory(mc);
if (context != null)
{
RenderUtils.renderInventoryOverlay(context, drawContext);
}
*/

InventoryOverlayHandler.getInstance().getRenderContext(drawContext, profiler, mc);
}

if (FeatureToggle.TWEAK_PLAYER_INVENTORY_PEEK.getBooleanValue() &&
Expand Down Expand Up @@ -135,7 +138,7 @@ else if (stack.isOf(Items.ENDER_CHEST) && Configs.Generic.SHULKER_DISPLAY_ENDER_

if (player != null)
{
Pair<Entity, NbtCompound> pair = ServerDataSyncer.getInstance().requestEntity(player.getId());
Pair<Entity, NbtCompound> pair = ServerDataSyncer.getInstance().requestEntity(world, player.getId());
NbtCompound nbt = new NbtCompound();
EnderChestInventory inv;

Expand Down
Loading

0 comments on commit 7090549

Please sign in to comment.