Skip to content

Commit

Permalink
Merge pull request #51 from Dragon-Seeker/1.21-StateMangerSideness
Browse files Browse the repository at this point in the history
[1.21] Adjust for sideness by requiring level or player when side is unknown or in common code area
  • Loading branch information
Jamalam360 authored Jul 15, 2024
2 parents 148c136 + 118547a commit e16e113
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.jamalam360.utility_belt;

import com.mojang.logging.LogUtils;
import net.minecraft.world.entity.player.Player;
import org.slf4j.Logger;

import java.util.List;

final class EmptyStateManager extends StateManager {

static final EmptyStateManager INSTANCE = new EmptyStateManager();

private static final Logger LOGGER = LogUtils.getLogger();

@Override
public boolean isInBelt(Player player) {
LOGGER.warn("[UtilityBelt] Unable to tell if its in the belt as only a empty manger was found!");

return false;
}

@Override
public void setInBelt(Player player, boolean inBelt) {
LOGGER.warn("[UtilityBelt] Unable to set the given such in belt as only a empty manger was found!");
}

@Override
public int getSelectedBeltSlot(Player player) {
LOGGER.warn("[UtilityBelt] Unable to get the given selected Belt slot as only a empty manger was found!");

return 0;
}

@Override
public void setSelectedBeltSlot(Player player, int slot) {
LOGGER.warn("[UtilityBelt] Unable to set the given selected Belt slot as only a empty manger was found!");
}

@Override
public UtilityBeltInventory getInventory(Player player) {
LOGGER.warn("[UtilityBelt] Unable to get given Belt Inventory as only a empty manger was found!");

return new UtilityBeltInventory(List.of());
}

@Override
public void setInventory(Player player, UtilityBeltInventory.Mutable inventory) {
LOGGER.warn("[UtilityBelt] Unable to set given Belt Inventory as only a empty manger was found!");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package io.github.jamalam360.utility_belt;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public abstract class StateManager {
private static StateManager clientInstance;
private static StateManager serverInstance;
private static StateManager clientInstance = EmptyStateManager.INSTANCE;
private static StateManager serverInstance = EmptyStateManager.INSTANCE;

public static StateManager getStateManager(LivingEntity entity) {
return getStateManager(entity.level());
}

public static StateManager getStateManager(Level level) {
return getStateManager(level.isClientSide());
}

public static StateManager getStateManager(boolean isClientSide) {
return isClientSide ? clientInstance : serverInstance;
}

@Deprecated
public static StateManager getClientInstance() {
return clientInstance;
}
Expand All @@ -15,6 +30,7 @@ public static void setClientInstance(StateManager clientInstance) {
StateManager.clientInstance = clientInstance;
}

@Deprecated
public static StateManager getServerInstance() {
return serverInstance;
}
Expand All @@ -23,6 +39,8 @@ public static void setServerInstance(StateManager serverInstance) {
StateManager.serverInstance = serverInstance;
}

//--

public boolean hasBelt(Player player) {
ItemStack belt = UtilityBeltItem.getBelt(player);
return belt != null && belt.is(UtilityBelt.UTILITY_BELT_ITEM.get());
Expand All @@ -43,4 +61,8 @@ public UtilityBeltInventory.Mutable getMutableInventory(Player player) {
}

public abstract void setInventory(Player player, UtilityBeltInventory.Mutable inventory);

public boolean isEmptyManager() {
return this == EmptyStateManager.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void init() {
if (Platform.isDevelopmentEnvironment()) {
CommandRegistrationEvent.EVENT.register(((dispatcher, registry, selection) -> dispatcher.register(Commands.literal("dumpstate").executes(ctx -> {
CommandSourceStack source = ctx.getSource();
StateManager stateManager = StateManager.getServerInstance();
StateManager stateManager = StateManager.getStateManager(false);
System.out.println("In belt: " + stateManager.isInBelt(source.getPlayerOrException()));
System.out.println("Selected slot: " + stateManager.getSelectedBeltSlot(source.getPlayerOrException()));
System.out.println("Belt NBT: " + UtilityBeltItem.getBelt(source.getPlayerOrException()).get(UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,19 @@ public boolean overrideOtherStackedOnMe(ItemStack belt, ItemStack otherStack, Sl
@Override
public void onUnequip(ItemStack stack, SlotReference reference) {
if (reference.entity() instanceof Player player && player.level().isClientSide) {
StateManager.getClientInstance().setInBelt(player, false);
StateManager.getClientInstance().setSelectedBeltSlot(player, 0);
var manager = StateManager.getStateManager(player);

manager.setInBelt(player, false);
manager.setSelectedBeltSlot(player, 0);

ClientNetworking.sendNewStateToServer(false, 0, false);
}
}

@Override
public void onEquip(ItemStack stack, SlotReference reference) {
if (reference.entity() instanceof Player player && !player.level().isClientSide) {
StateManager.getServerInstance().setInventory(player, new Mutable(getInventoryFromTag(stack)));
StateManager.getStateManager(player).setInventory(player, new Mutable(getInventoryFromTag(stack)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public class BeltHotbarRenderer {

public static void render(GuiGraphics graphics, DeltaTracker deltaTracker) {
Player player = Minecraft.getInstance().player;
StateManager stateManager = StateManager.getClientInstance();

if (player != null && stateManager.hasBelt(player) && (stateManager.isInBelt(player)
if(player == null) return;

StateManager stateManager = StateManager.getStateManager(player);

if (stateManager.hasBelt(player) && (stateManager.isInBelt(player)
|| UtilityBelt.CONFIG.get().displayUtilityBeltWhenNotSelected)) {
int scaledHeight = Minecraft.getInstance().getWindow().getGuiScaledHeight();
int x = switch (UtilityBelt.CONFIG.get().hotbarPosition) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CUpdateBeltInventory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.world.entity.player.Player;

@Environment(EnvType.CLIENT)
public class ClientNetworking {
Expand All @@ -36,14 +37,18 @@ public static void sendOpenScreenToServer() {
}

private static void handleSetBeltSlot(S2CSetBeltSlot payload, NetworkManager.PacketContext ctx) {
StateManager.getClientInstance().setSelectedBeltSlot(ctx.getPlayer(), payload.slot());
Player player = ctx.getPlayer();

StateManager.getStateManager(player).setSelectedBeltSlot(player, payload.slot());
}

private static void handleSetHotbarSlot(S2CSetHotbarSlot payload, NetworkManager.PacketContext ctx) {
ctx.getPlayer().getInventory().selected = payload.slot();
}

private static void handleUpdateBeltInventory(S2CUpdateBeltInventory payload, NetworkManager.PacketContext ctx) {
StateManager.getClientInstance().setInventory(ctx.getPlayer(), new Mutable(payload.inventory()));
Player player = ctx.getPlayer();

StateManager.getStateManager(player).setInventory(player, new Mutable(payload.inventory()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.lwjgl.glfw.GLFW;

Expand All @@ -44,7 +45,6 @@ public static void init() {
KeyMappingRegistry.register(OPEN_SCREEN);
ClientNetworking.init();
StateManager.setClientInstance(new ClientStateManager());
StateManager.setServerInstance(new ClientDelegatingServerStateManager()); // Will be fixed later. Currently a hackfix

ClientCommandRegistrationEvent.EVENT.register((dispatcher, c) -> dispatcher.register(
literal("utilitybelt")
Expand All @@ -58,9 +58,11 @@ public static void init() {
.then(
literal("fixme")
.executes(ctx -> {
StateManager stateManager = StateManager.getClientInstance();
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
Player player = Minecraft.getInstance().player;

StateManager stateManager = StateManager.getStateManager(player);
stateManager.setInBelt(player, false);
stateManager.setSelectedBeltSlot(player, 0);
ClientNetworking.sendNewStateToServer(false, 0, false);
ctx.getSource().arch$sendSuccess(() -> Component.literal("Reset state"), false);
return 0;
Expand All @@ -73,15 +75,18 @@ public static void init() {
dispatcher.register(
literal("dumpstatec")
.executes(ctx -> {
StateManager stateManager = StateManager.getClientInstance();
System.out.println("In belt: " + stateManager.isInBelt(Minecraft.getInstance().player));
System.out.println("Selected slot: " + stateManager.getSelectedBeltSlot(Minecraft.getInstance().player));
System.out.println("Belt NBT: " + UtilityBeltItem.getBelt(Minecraft.getInstance().player).get(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()));

StateManager stateManagerS = StateManager.getServerInstance();
System.out.println("In belt (client but server): " + stateManagerS.isInBelt(Minecraft.getInstance().player));
System.out.println("Selected slot (client but server): " + stateManagerS.getSelectedBeltSlot(Minecraft.getInstance().player));
System.out.println("Belt NBT (client but server): " + UtilityBeltItem.getBelt(Minecraft.getInstance().player).get(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()));
var player = Minecraft.getInstance().player;

StateManager stateManager = StateManager.getStateManager(player);

System.out.println("In belt: " + stateManager.isInBelt(player));
System.out.println("Selected slot: " + stateManager.getSelectedBeltSlot(player));
System.out.println("Belt NBT: " + UtilityBeltItem.getBelt(player).get(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()));

StateManager stateManagerS = StateManager.getStateManager(false);
System.out.println("In belt (client but server): " + stateManagerS.isInBelt(player));
System.out.println("Selected slot (client but server): " + stateManagerS.getSelectedBeltSlot(player));
System.out.println("Belt NBT (client but server): " + UtilityBeltItem.getBelt(player).get(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()));
return 0;
})
)
Expand Down Expand Up @@ -109,17 +114,18 @@ private static void onEndClientTick(Minecraft client) {
}

private static EventResult onMouseScrolled(Minecraft client, double scrollX, double scrollY) {
StateManager stateManager = StateManager.getClientInstance();
Player player = client.player;
StateManager stateManager = StateManager.getStateManager(true);

if (scrollY != 0 && stateManager.isInBelt(client.player)) {
int slot = stateManager.getSelectedBeltSlot(client.player);
ItemStack belt = UtilityBeltItem.getBelt(client.player);
if (scrollY != 0 && stateManager.isInBelt(player)) {
int slot = stateManager.getSelectedBeltSlot(player);
ItemStack belt = UtilityBeltItem.getBelt(player);

if (belt == null) {
return EventResult.pass();
}

int beltSize = stateManager.getInventory(client.player).getContainerSize();
int beltSize = stateManager.getInventory(player).getContainerSize();

if (UtilityBelt.CONFIG.get().invertScrolling) {
scrollY = -scrollY;
Expand All @@ -137,8 +143,8 @@ private static EventResult onMouseScrolled(Minecraft client, double scrollX, dou
}
}

stateManager.setSelectedBeltSlot(client.player, slot);
ClientNetworking.sendNewStateToServer(stateManager.isInBelt(client.player), stateManager.getSelectedBeltSlot(client.player), false);
stateManager.setSelectedBeltSlot(player, slot);
ClientNetworking.sendNewStateToServer(stateManager.isInBelt(player), stateManager.getSelectedBeltSlot(player), false);
return EventResult.interruptTrue();
}

Expand All @@ -147,7 +153,7 @@ private static EventResult onMouseScrolled(Minecraft client, double scrollX, dou

private static void onPlayerRespawn(LocalPlayer oldPlayer, LocalPlayer newPlayer) {
if (oldPlayer == Minecraft.getInstance().player || newPlayer == Minecraft.getInstance().player) {
StateManager stateManager = StateManager.getClientInstance();
StateManager stateManager = StateManager.getStateManager(true);
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
ClientNetworking.sendNewStateToServer(false, 0, false);
Expand All @@ -158,7 +164,7 @@ private static void onPlayerRespawn(LocalPlayer oldPlayer, LocalPlayer newPlayer
* @see io.github.jamalam360.utility_belt.mixin.client.ClientPacketListenerMixin
*/
public static void onClientConnect() {
StateManager stateManager = StateManager.getClientInstance();
StateManager stateManager = StateManager.getStateManager(true);
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
ClientNetworking.sendNewStateToServer(false, 0, false);
Expand All @@ -168,7 +174,7 @@ public static void onClientConnect() {
* @see io.github.jamalam360.utility_belt.mixin.client.ClientPacketListenerMixin
*/
public static void onClientDisconnect() {
StateManager stateManager = StateManager.getClientInstance();
StateManager stateManager = StateManager.getStateManager(true);
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
}
Expand All @@ -178,7 +184,7 @@ private static void toggleInBelt(Minecraft client) {
return;
}

StateManager stateManager = StateManager.getClientInstance();
StateManager stateManager = StateManager.getStateManager(true);
stateManager.setInBelt(client.player, !stateManager.isInBelt(client.player));
playSwapSound(client);
ClientNetworking.sendNewStateToServer(stateManager.isInBelt(client.player), stateManager.getSelectedBeltSlot(client.player), client.player.isCrouching());
Expand Down
Loading

0 comments on commit e16e113

Please sign in to comment.