Skip to content

Commit

Permalink
Merge pull request #52 from JamCoreModding/exp/split-sources
Browse files Browse the repository at this point in the history
Potential enhancement: use split sources
  • Loading branch information
Jamalam360 authored Jul 29, 2024
2 parents a72b35e + 358b0cd commit 8856425
Show file tree
Hide file tree
Showing 32 changed files with 254 additions and 216 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Fix (#31).
- Fix duplication when player dies (#53).
4 changes: 4 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ architectury {
common(rootProject.enabled_platforms.split(","))
}

loom {
splitEnvironmentSourceSets()
}

dependencies {
modImplementation libs.fabric.loader
modImplementation libs.architectury.common
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
import dev.architectury.registry.menu.MenuRegistry;
import io.github.jamalam360.jamlib.config.ConfigManager;
import io.github.jamalam360.utility_belt.StateManager;
import io.github.jamalam360.utility_belt.UtilityBelt;
import io.github.jamalam360.utility_belt.UtilityBeltItem;
import io.github.jamalam360.utility_belt.screen.UtilityBeltScreen;
import io.github.jamalam360.utility_belt.client.network.ClientNetworking;
import io.github.jamalam360.utility_belt.client.render.BeltHotbarRenderer;
import io.github.jamalam360.utility_belt.client.render.BeltRenderer;
import io.github.jamalam360.utility_belt.client.screen.UtilityBeltScreen;
import io.github.jamalam360.utility_belt.client.state.ClientStateManager;
import io.github.jamalam360.utility_belt.state.StateManager;
import io.wispforest.accessories.api.client.AccessoriesRendererRegistry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.ChatFormatting;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
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;

import static dev.architectury.event.events.client.ClientCommandRegistrationEvent.literal;

@Environment(EnvType.CLIENT)
public class UtilityBeltClient {
public static final ConfigManager<Config> CONFIG = new ConfigManager<>(UtilityBelt.MOD_ID, Config.class);
Expand All @@ -36,64 +35,32 @@ public class UtilityBeltClient {
private static boolean isHoldingSwap = false;

public static void init() {
ClientGuiEvent.RENDER_HUD.register(BeltHotbarRenderer::render);
UtilityBelt.UTILITY_BELT_ITEM.listen(belt -> AccessoriesRendererRegistry.registerRenderer(belt, BeltRenderer::new));
ClientTickEvent.CLIENT_POST.register(UtilityBeltClient::onEndClientTick);
ClientRawInputEvent.MOUSE_SCROLLED.register(UtilityBeltClient::onMouseScrolled);
ClientPlayerEvent.CLIENT_PLAYER_RESPAWN.register(UtilityBeltClient::onPlayerRespawn);
UtilityBelt.MENU_TYPE.listen(menu -> MenuRegistry.registerScreenFactory(UtilityBelt.MENU_TYPE.get(), UtilityBeltScreen::new));
KeyMappingRegistry.register(SWAP_TOGGLE);
KeyMappingRegistry.register(SWAP_HOLD);
KeyMappingRegistry.register(OPEN_SCREEN);
ClientNetworking.init();
StateManager.setClientInstance(new ClientStateManager());

ClientCommandRegistrationEvent.EVENT.register((dispatcher, c) -> dispatcher.register(
literal("utilitybelt")
.then(
literal("help")
.executes(ctx -> {
ctx.getSource().arch$sendSuccess(UtilityBeltClient::getHelpMessage, false);
return 0;
})
)
.then(
literal("fixme")
.executes(ctx -> {
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;
})
)
));
ClientGuiEvent.RENDER_HUD.register(BeltHotbarRenderer::render);
ClientTickEvent.CLIENT_POST.register(UtilityBeltClient::onEndClientTick);
ClientRawInputEvent.MOUSE_SCROLLED.register(UtilityBeltClient::onMouseScrolled);
ClientPlayerEvent.CLIENT_PLAYER_RESPAWN.register(UtilityBeltClient::onPlayerRespawn);
ClientCommandRegistrationEvent.EVENT.register(UtilityBeltCommands::registerCommands);

if (Platform.isDevelopmentEnvironment()) {
ClientCommandRegistrationEvent.EVENT.register(((dispatcher, c) ->
dispatcher.register(
literal("dumpstatec")
.executes(ctx -> {
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;
})
)
));
ClientCommandRegistrationEvent.EVENT.register(UtilityBeltCommands::registerDevelopmentCommands);
}

UtilityBelt.MENU_TYPE.listen(menu -> MenuRegistry.registerScreenFactory(UtilityBelt.MENU_TYPE.get(), UtilityBeltScreen::new));
UtilityBelt.UTILITY_BELT_ITEM.listen(belt -> AccessoriesRendererRegistry.registerRenderer(belt, BeltRenderer::new));
UtilityBelt.UTILITY_BELT_UNEQUIP_EVENT.register((stack, reference) -> {
if (reference.entity() instanceof Player player && player.level().isClientSide) {
var manager = StateManager.getStateManager(player);
manager.setInBelt(player, false);
manager.setSelectedBeltSlot(player, 0);
ClientNetworking.sendNewStateToServer(false, 0, false);
}
});
ClientNetworking.init();
StateManager.setClientInstance(new ClientStateManager());
}

private static void onEndClientTick(Minecraft client) {
Expand Down Expand Up @@ -155,27 +122,11 @@ 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.getStateManager(true);
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
ClientNetworking.sendNewStateToServer(false, 0, false);
resetClientState();
}
}

/**
* @see io.github.jamalam360.utility_belt.mixin.client.ClientPacketListenerMixin
*/
public static void onClientConnect() {
StateManager stateManager = StateManager.getStateManager(true);
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
ClientNetworking.sendNewStateToServer(false, 0, false);
}

/**
* @see io.github.jamalam360.utility_belt.mixin.client.ClientPacketListenerMixin
*/
public static void onClientDisconnect() {
public static void resetClientState() {
StateManager stateManager = StateManager.getStateManager(true);
stateManager.setInBelt(Minecraft.getInstance().player, false);
stateManager.setSelectedBeltSlot(Minecraft.getInstance().player, 0);
Expand All @@ -193,16 +144,6 @@ private static void toggleInBelt(Minecraft client) {
}

private static void playSwapSound(Minecraft client) {
client.getSoundManager().play(SimpleSoundInstance.forUI(
SoundEvents.ARMOR_EQUIP_LEATHER, client.level.random.nextFloat() + 0.50f));
}

private static Component getHelpMessage() {
return Component.literal("/utilitybelt help").withStyle(ChatFormatting.YELLOW)
.append(Component.literal(" - Shows this help message").withStyle(ChatFormatting.GRAY))
.append(Component.literal("\n"))
.append(Component.literal("/utilitybelt fixme").withStyle(ChatFormatting.YELLOW))
.append(Component.literal(" - Fixes your state. Useful if you manage to get stuck in the belt. If you use this command please report the circumstances to ").withStyle(ChatFormatting.GRAY))
.append(Component.literal("https://github.com/JamCoreModding/utility-belt").withStyle(s -> s.withUnderlined(true).withColor(ChatFormatting.BLUE).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/JamCoreModding/utility-belt"))));
client.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.ARMOR_EQUIP_LEATHER, client.level.random.nextFloat() + 0.50f));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.jamalam360.utility_belt.client;

import com.mojang.brigadier.CommandDispatcher;
import dev.architectury.event.events.client.ClientCommandRegistrationEvent;
import io.github.jamalam360.utility_belt.UtilityBelt;
import io.github.jamalam360.utility_belt.UtilityBeltItem;
import io.github.jamalam360.utility_belt.client.network.ClientNetworking;
import io.github.jamalam360.utility_belt.state.StateManager;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;

import static dev.architectury.event.events.client.ClientCommandRegistrationEvent.literal;

public class UtilityBeltCommands {
public static void registerCommands(CommandDispatcher<ClientCommandRegistrationEvent.ClientCommandSourceStack> dispatcher, CommandBuildContext _ctx) {
literal("utilitybelt")
.then(
literal("help")
.executes(ctx -> {
ctx.getSource().arch$sendSuccess(UtilityBeltCommands::getHelpMessage, false);
return 0;
})
)
.then(
literal("fixme")
.executes(ctx -> {
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;
})
);
}

public static void registerDevelopmentCommands(CommandDispatcher<ClientCommandRegistrationEvent.ClientCommandSourceStack> dispatcher, CommandBuildContext _ctx) {
dispatcher.register(
literal("dumpstatec")
.executes(ctx -> {
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;
})
);
}

private static Component getHelpMessage() {
return Component.literal("/utilitybelt help").withStyle(ChatFormatting.YELLOW)
.append(Component.literal(" - Shows this help message").withStyle(ChatFormatting.GRAY))
.append(Component.literal("\n"))
.append(Component.literal("/utilitybelt fixme").withStyle(ChatFormatting.YELLOW))
.append(Component.literal(" - Fixes your state. Useful if you manage to get stuck in the belt. If you use this command please report the circumstances to ").withStyle(ChatFormatting.GRAY))
.append(Component.literal("https://github.com/JamCoreModding/utility-belt").withStyle(s -> s.withUnderlined(true).withColor(ChatFormatting.BLUE).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/JamCoreModding/utility-belt"))));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jamalam360.utility_belt.mixin.client;
package io.github.jamalam360.utility_belt.client.mixin;

import io.github.jamalam360.utility_belt.client.UtilityBeltClient;
import io.github.jamalam360.utility_belt.client.network.ClientNetworking;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.multiplayer.ClientPacketListener;
Expand All @@ -14,11 +15,12 @@
public class ClientPacketListenerMixin {
@Inject(method = "handleLogin", at = @At("TAIL"))
private void utilitybelt$onClientPacketListenerLogin(CallbackInfo ci) {
UtilityBeltClient.onClientConnect();
UtilityBeltClient.resetClientState();
ClientNetworking.sendNewStateToServer(false, 0, false);
}

@Inject(method = "close", at = @At("HEAD"))
private void utilitybelt$onClientPacketListenerClose(CallbackInfo ci) {
UtilityBeltClient.onClientDisconnect();
UtilityBeltClient.resetClientState();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.jamalam360.utility_belt.mixin.client;
package io.github.jamalam360.utility_belt.client.mixin;

import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import io.github.jamalam360.utility_belt.StateManager;
import io.github.jamalam360.utility_belt.state.StateManager;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.Gui;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.jamalam360.utility_belt.mixin.client;
package io.github.jamalam360.utility_belt.client.mixin;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.sugar.Local;
import io.github.jamalam360.utility_belt.StateManager;
import io.github.jamalam360.utility_belt.state.StateManager;
import io.github.jamalam360.utility_belt.UtilityBeltItem;
import io.github.jamalam360.utility_belt.client.ClientNetworking;
import io.github.jamalam360.utility_belt.client.network.ClientNetworking;
import io.github.jamalam360.utility_belt.client.UtilityBeltClient;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -34,10 +34,7 @@ public class MinecraftMixin {
ClientNetworking.sendNewStateToServer(false, stateManager.getSelectedBeltSlot(this.player), false);
}
}

/**
* @reason Allows using the 1-9 hotbar keys to select slots in the belt or switch back to the hotbar depending on the config
*/

@ModifyExpressionValue(
method = "handleKeybinds",
at = @At(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.github.jamalam360.utility_belt.client;
package io.github.jamalam360.utility_belt.client.network;

import dev.architectury.networking.NetworkManager;
import io.github.jamalam360.utility_belt.StateManager;
import io.github.jamalam360.utility_belt.state.StateManager;
import io.github.jamalam360.utility_belt.UtilityBeltInventory.Mutable;
import io.github.jamalam360.utility_belt.UtilityBeltPackets;
import io.github.jamalam360.utility_belt.UtilityBeltPackets.C2SOpenScreen;
import io.github.jamalam360.utility_belt.UtilityBeltPackets.C2SUpdateState;
import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CSetBeltSlot;
import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CSetHotbarSlot;
import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CUpdateBeltInventory;
import io.github.jamalam360.utility_belt.network.UtilityBeltPackets;
import io.github.jamalam360.utility_belt.network.UtilityBeltPackets.C2SOpenScreen;
import io.github.jamalam360.utility_belt.network.UtilityBeltPackets.C2SUpdateState;
import io.github.jamalam360.utility_belt.network.UtilityBeltPackets.S2CSetBeltSlot;
import io.github.jamalam360.utility_belt.network.UtilityBeltPackets.S2CSetHotbarSlot;
import io.github.jamalam360.utility_belt.network.UtilityBeltPackets.S2CUpdateBeltInventory;
import io.github.jamalam360.utility_belt.client.UtilityBeltClient;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.world.entity.player.Player;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.jamalam360.utility_belt.client;
package io.github.jamalam360.utility_belt.client.render;

import com.mojang.blaze3d.systems.RenderSystem;
import io.github.jamalam360.utility_belt.StateManager;
import io.github.jamalam360.utility_belt.state.StateManager;
import io.github.jamalam360.utility_belt.UtilityBelt;
import io.github.jamalam360.utility_belt.UtilityBeltInventory;
import io.github.jamalam360.utility_belt.client.UtilityBeltClient;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.DeltaTracker;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.jamalam360.utility_belt.client;
package io.github.jamalam360.utility_belt.client.render;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.jamalam360.utility_belt.client;
package io.github.jamalam360.utility_belt.client.render;

import com.google.common.base.Suppliers;
import com.mojang.blaze3d.vertex.PoseStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.github.jamalam360.utility_belt.screen;
package io.github.jamalam360.utility_belt.client.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import io.github.jamalam360.utility_belt.UtilityBelt;
import io.github.jamalam360.utility_belt.screen.UtilityBeltMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.jamalam360.utility_belt.client;
package io.github.jamalam360.utility_belt.client.state;

import io.github.jamalam360.utility_belt.StateManager;
import io.github.jamalam360.utility_belt.state.StateManager;
import io.github.jamalam360.utility_belt.UtilityBeltInventory;
import io.github.jamalam360.utility_belt.UtilityBeltItem;
import net.fabricmc.api.EnvType;
Expand Down
Loading

0 comments on commit 8856425

Please sign in to comment.