Skip to content

Commit

Permalink
feat: my random changes, to be merged with the main branch in a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Jul 15, 2024
1 parent 148c136 commit 48f6bef
Show file tree
Hide file tree
Showing 25 changed files with 312 additions and 416 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
- Update to 1.21
- Switch to using the [Accessories](https://modrinth.com/mod/accessories) mod. See its mod page for information on compatibility with existing mods using Trinkets and Curios.
- Fix hotbar rendering even when F1 is pressed.
- Fix server crash (#48).
- Move config client side, since all of its options were only applicable there.

In addition, several other issues have been fixed related to network synchronization.
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
package io.github.jamalam360.utility_belt;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

public abstract class StateManager {
private static StateManager clientInstance;
private static StateManager serverInstance;
import java.util.Map;
import java.util.UUID;

public static StateManager getClientInstance() {
return clientInstance;
public class StateManager {
private static final StateManager instance = new StateManager();
private final Map<UUID, PlayerState> playerStates = new Object2ObjectArrayMap<>();

public static StateManager getInstance() {
return instance;
}

public static void setClientInstance(StateManager clientInstance) {
StateManager.clientInstance = clientInstance;
public boolean hasBelt(Player player) {
ItemStack belt = UtilityBeltItem.getBelt(player);
return belt != null && belt.is(UtilityBelt.UTILITY_BELT_ITEM.get());
}

private PlayerState getState(Player player) {
return playerStates.computeIfAbsent(player.getUUID(), uuid -> new PlayerState(false, 0));
}

public static StateManager getServerInstance() {
return serverInstance;
public boolean isInBelt(Player player) {
return getState(player).inBelt;
}

public static void setServerInstance(StateManager serverInstance) {
StateManager.serverInstance = serverInstance;
public void setInBelt(Player player, boolean inBelt) {
getState(player).inBelt = inBelt;
}

public boolean hasBelt(Player player) {
ItemStack belt = UtilityBeltItem.getBelt(player);
return belt != null && belt.is(UtilityBelt.UTILITY_BELT_ITEM.get());
public int getSelectedBeltSlot(Player player) {
return getState(player).selectedBeltSlot;
}

public abstract boolean isInBelt(Player player);
public void setSelectedBeltSlot(Player player, int slot) {
getState(player).selectedBeltSlot = slot;
}

public UtilityBeltInventory getInventory(Player player) {
PlayerState state = getState(player);

public abstract void setInBelt(Player player, boolean inBelt);
if (state.inventory == null) {
ItemStack belt = UtilityBeltItem.getBelt(player);

public abstract int getSelectedBeltSlot(Player player);
if (belt == null) {
return UtilityBeltInventory.EMPTY;
}

public abstract void setSelectedBeltSlot(Player player, int slot);
state.inventory = UtilityBeltItem.getInventory(belt);
}

public abstract UtilityBeltInventory getInventory(Player player);
return state.inventory;
}

public UtilityBeltInventory.Mutable getMutableInventory(Player player) {
return new UtilityBeltInventory.Mutable(this.getInventory(player));
}

public abstract void setInventory(Player player, UtilityBeltInventory.Mutable inventory);
public void setInventory(Player player, UtilityBeltInventory.Mutable inventory) {
getState(player).inventory = inventory.toImmutable();
}

private static class PlayerState {
boolean inBelt;
int selectedBeltSlot;
@Nullable
UtilityBeltInventory inventory;

PlayerState(boolean inBelt, int selectedBeltSlot) {
this.inBelt = inBelt;
this.selectedBeltSlot = selectedBeltSlot;
this.inventory = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import dev.architectury.utils.EnvExecutor;
import io.github.jamalam360.jamlib.JamLib;
import io.github.jamalam360.jamlib.JamLibPlatform;
import io.github.jamalam360.jamlib.config.ConfigManager;
import io.github.jamalam360.utility_belt.client.UtilityBeltClient;
import io.github.jamalam360.utility_belt.screen.UtilityBeltMenu;
import io.github.jamalam360.utility_belt.server.ServerNetworking;
import io.github.jamalam360.utility_belt.server.ServerStateManager;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.component.DataComponentType;
Expand All @@ -32,7 +30,6 @@ public class UtilityBelt {
public static final String MOD_ID = "utility_belt";
public static final String MOD_NAME = "Utility Belt";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);
public static final ConfigManager<Config> CONFIG = new ConfigManager<>(MOD_ID, Config.class);
public static final TagKey<Item> ALLOWED_IN_UTILITY_BELT = TagKey.create(Registries.ITEM, id("allowed_in_utility_belt"));

private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(MOD_ID, Registries.ITEM);
Expand All @@ -47,21 +44,22 @@ public class UtilityBelt {

public static void init() {
JamLib.checkForJarRenaming(UtilityBelt.class);
COMPONENT_TYPES.register(); // needs to be before items
COMPONENT_TYPES.register();
ITEMS.register();
MENUS.register();
UTILITY_BELT_ITEM.listen((belt) -> CreativeTabRegistry.append(CreativeModeTabs.TOOLS_AND_UTILITIES, belt));
ServerNetworking.init();
StateManager.setServerInstance(new ServerStateManager());
EnvExecutor.runInEnv(Env.CLIENT, () -> UtilityBeltClient::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.getInstance();
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()));
System.out.println("Server state manager inv: " + stateManager.getInventory(source.getPlayerOrException()));
System.out.println("stack in hand: " + source.getPlayerOrException().getMainHandItem());
return 0;
}))));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.jamalam360.utility_belt;

import com.mojang.serialization.Codec;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand All @@ -11,100 +13,104 @@
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;

public record UtilityBeltInventory(List<ItemStack> items) {

public static final UtilityBeltInventory EMPTY = new UtilityBeltInventory(NonNullList.withSize(4, ItemStack.EMPTY));
public static final Codec<UtilityBeltInventory> CODEC = Codec
.list(ItemStack.OPTIONAL_CODEC)
.xmap(UtilityBeltInventory::new, UtilityBeltInventory::items);

public static final StreamCodec<RegistryFriendlyByteBuf, UtilityBeltInventory> STREAM_CODEC = StreamCodec
.composite(
ItemStack.OPTIONAL_STREAM_CODEC.apply(ByteBufCodecs.list(4)),
UtilityBeltInventory::items,
UtilityBeltInventory::new
);

public UtilityBeltInventory {
if (items.size() != 4) {
throw new IllegalArgumentException("Utility belt inventory must have exactly 4 items");
}
}

public ItemStack getItem(int index) {
return items.get(index);
}

public int getContainerSize() {
return 4;
}

public UtilityBeltInventory clone() {
return new UtilityBeltInventory(new ArrayList<>(items));
}

@Override
public boolean equals(Object obj) {
if (obj instanceof UtilityBeltInventory other) {
return ItemStack.listMatches(items, other.items);
}

return false;
}

@SuppressWarnings("deprecation")
@Override
public int hashCode() {
return ItemStack.hashStackList(items);
}

@Override
public String toString() {
return invToString("UtilityBeltInventory[", items);
}

public static class Mutable extends SimpleContainer {

public Mutable(UtilityBeltInventory inv) {
super(4);

for (int i = 0; i < inv.getContainerSize(); i++) {
this.setItem(i, inv.getItem(i));
}
}

public UtilityBeltInventory toImmutable() {
return new UtilityBeltInventory(new ArrayList<>(this.getItems()));
}

@Override
public boolean equals(Object obj) {
if (obj instanceof UtilityBeltInventory other) {
return ItemStack.listMatches(this.getItems(), other.items);
}
public record UtilityBeltInventory(NonNullList<ItemStack> items) {

return false;
}

@Override
public String toString() {
return invToString("UtilityBeltInventory$Mutable[", this.getItems());
}
public static final UtilityBeltInventory EMPTY = new UtilityBeltInventory(NonNullList.withSize(4, ItemStack.EMPTY));
public static final Codec<UtilityBeltInventory> CODEC = Codec
.list(ItemStack.OPTIONAL_CODEC)
.xmap(UtilityBeltInventory::new, UtilityBeltInventory::items);
public static final StreamCodec<RegistryFriendlyByteBuf, UtilityBeltInventory> STREAM_CODEC = ItemStack.OPTIONAL_STREAM_CODEC.apply(ByteBufCodecs.list(4)).map(UtilityBeltInventory::new, UtilityBeltInventory::items);

public UtilityBeltInventory(List<ItemStack> items) {
this(nonNullListFromList(items));
}

private static String invToString(String prefix, List<ItemStack> items) {
StringBuilder string = new StringBuilder(prefix);
private static NonNullList<ItemStack> nonNullListFromList(List<ItemStack> items) {
NonNullList<ItemStack> list = NonNullList.withSize(4, ItemStack.EMPTY);

for (int i = 0; i < items.size(); i++) {
string.append(items.get(i));
string.append(" {");
string.append(items.get(i).getComponents().get(DataComponents.DAMAGE));
string.append("}");
if (i < items.size() - 1) {
string.append(", ");
}
list.set(i, items.get(i));
}

return string.toString();
return list;
}

public UtilityBeltInventory {
if (items.size() != 4) {
throw new IllegalArgumentException("Utility belt inventory must have exactly 4 items");
}
}

public ItemStack getItem(int index) {
return items.get(index);
}

public int getContainerSize() {
return 4;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof UtilityBeltInventory other) {
return ItemStack.listMatches(items, other.items);
}

return false;
}

@SuppressWarnings("deprecation")
@Override
public int hashCode() {
return ItemStack.hashStackList(items);
}

@Override
public String toString() {
return invToString("UtilityBeltInventory[", items);
}

public static class Mutable extends SimpleContainer {

public Mutable(UtilityBeltInventory inv) {
super(4);

for (int i = 0; i < inv.getContainerSize(); i++) {
this.setItem(i, inv.getItem(i));
}
}

public UtilityBeltInventory toImmutable() {
return new UtilityBeltInventory(new ArrayList<>(this.getItems()));
}

@Override
public boolean equals(Object obj) {
if (obj instanceof UtilityBeltInventory other) {
return ItemStack.listMatches(this.getItems(), other.items);
}

return false;
}

@Override
public String toString() {
return invToString("UtilityBeltInventory$Mutable[", this.getItems());
}
}

private static String invToString(String prefix, List<ItemStack> items) {
StringBuilder string = new StringBuilder(prefix);

for (int i = 0; i < items.size(); i++) {
string.append(items.get(i));
string.append(" {");
string.append(items.get(i).getComponents().get(DataComponents.DAMAGE));
string.append("}");
if (i < items.size() - 1) {
string.append(", ");
}
}

return string.toString();
}
}
Loading

0 comments on commit 48f6bef

Please sign in to comment.