Skip to content

Commit

Permalink
backport 2.2.0 to 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Jun 10, 2024
2 parents a2e8755 + 1b8d805 commit a734516
Show file tree
Hide file tree
Showing 25 changed files with 675 additions and 478 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
- Fix a dupe exploit (#44)
This release brings about a significant internal rework with should make Utility Belt significantly
less buggy. I have conducted a long play testing session before this release to try to verify
everything works as intended.

## Closed Issues

- #47: fishing rod flickering
- #46: dupe bug
- #45: dupe bug

Alongside the issues that were reported, many others have been fixed.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public boolean hasBelt(Player player) {
return belt != null && belt.is(UtilityBelt.UTILITY_BELT_ITEM.get());
}

public void onStartTick(Player player) {
}

public abstract boolean isInBelt(Player player);

public abstract void setInBelt(Player player, boolean inBelt);
Expand All @@ -40,4 +37,10 @@ public void onStartTick(Player player) {
public abstract void setSelectedBeltSlot(Player player, int slot);

public abstract UtilityBeltInventory getInventory(Player player);

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

public abstract void setInventory(Player player, UtilityBeltInventory.Mutable inventory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,43 @@
import org.slf4j.LoggerFactory;

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"));
public static final ResourceLocation C2S_UPDATE_STATE = id("update_state");
public static final ResourceLocation C2S_OPEN_SCREEN = id("open_screen");
public static final ResourceLocation S2C_UPDATE_INV = id("update_inv");
public static final ResourceLocation S2C_SET_BELT_SLOT = id("set_belt_slot");
public static final ResourceLocation S2C_SET_HOTBAR_SLOT = id("set_hotbar_slot");
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(MOD_ID, Registries.ITEM);
public static final RegistrySupplier<Item> UTILITY_BELT_ITEM = ITEMS.register("utility_belt", UtilityBeltItem::new);
public static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(MOD_ID, Registries.MENU);
public static final RegistrySupplier<MenuType<UtilityBeltMenu>> MENU_TYPE = MENUS.register("utility_belt", () -> new MenuType<>(UtilityBeltMenu::new, FeatureFlagSet.of()));

public static void init() {
JamLib.checkForJarRenaming(UtilityBelt.class);
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);
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"));

if (Platform.isDevelopmentEnvironment()) {
CommandRegistrationEvent.EVENT.register(((dispatcher, registry, selection) ->
dispatcher.register(
Commands.literal("dumpstate")
.executes(ctx -> {
CommandSourceStack source = ctx.getSource();
StateManager stateManager = StateManager.getServerInstance();
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()).getTag());
return 0;
})
)
));
}
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(MOD_ID, Registries.ITEM);
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(MOD_ID, Registries.MENU);

LOGGER.info(MOD_NAME + " initialized on " + JamLibPlatform.getPlatform());
}
public static final RegistrySupplier<Item> UTILITY_BELT_ITEM = ITEMS.register("utility_belt", UtilityBeltItem::new);
public static final RegistrySupplier<MenuType<UtilityBeltMenu>> MENU_TYPE = MENUS.register("utility_belt", () -> new MenuType<>(UtilityBeltMenu::new, FeatureFlagSet.of()));

public static ResourceLocation id(String path) {
return new ResourceLocation(MOD_ID, path);
}
public static void init() {
JamLib.checkForJarRenaming(UtilityBelt.class);
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();
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()).getTag().getCompound("Inventory"));
return 0;
}))));
}

LOGGER.info(MOD_NAME + " initialized on " + JamLibPlatform.getPlatform());
}

public static ResourceLocation id(String path) {
return new ResourceLocation(MOD_ID, path);
}
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,146 @@
package io.github.jamalam360.utility_belt;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

public class UtilityBeltInventory extends SimpleContainer {
public UtilityBeltInventory() {
super(4);
}

@Override
public void fromTag(ListTag listTag) {
this.clearContent();

for (int i = 0; i < listTag.size(); ++i) {
this.setItem(i, ItemStack.of(listTag.getCompound(i)));
}
}

@Override
public ListTag createTag() {
ListTag listTag = new ListTag();

for (int i = 0; i < this.getContainerSize(); ++i) {
listTag.add(this.getItem(i).save(new CompoundTag()));
}

return listTag;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof UtilityBeltInventory other) {
if (other.getContainerSize() == this.getContainerSize()) {
for (int i = 0; i < this.getContainerSize(); i++) {
if (!ItemStack.matches(this.getItem(i), other.getItem(i))) {
return false;
}
}

return true;
}
}

return false;
}

@Override
public UtilityBeltInventory clone() {
UtilityBeltInventory inv = new UtilityBeltInventory();
for (int i = 0; i < this.getContainerSize(); i++) {
inv.setItem(i, this.getItem(i).copy());
}

return inv;
}
public record UtilityBeltInventory(List<ItemStack> items) {

public static final UtilityBeltInventory EMPTY = new UtilityBeltInventory(NonNullList.withSize(4, ItemStack.EMPTY));

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) {
for (int i = 0; i < items.size(); i++) {
if (!ItemStack.matches(items.get(i), other.items.get(i))) {
return false;
}
}
}

return false;
}

@Override
public int hashCode() {
return 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) {
for (int i = 0; i < this.getItems().size(); i++) {
if (!ItemStack.matches(this.getItems().get(i), other.items.get(i))) {
return false;
}
}
}

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));
if (i < items.size() - 1) {
string.append(", ");
}
}

return string.toString();
}

// Methods added for 1.20.6 --> 1.20.4 backporting ease
private static int hashItemAndTag(@Nullable ItemStack arg) {
if (arg != null) {
int i = 31 + arg.getItem().hashCode();
return 31 * i + (arg.getTag() == null ? 0 : arg.getTag().hashCode());
} else {
return 0;
}
}

private static int hashStackList(List<ItemStack> list) {
int i = 0;

for (ItemStack itemstack : list) {
i = i * 31 + hashItemAndTag(itemstack);
}

return i;
}

public static UtilityBeltInventory fromTag(ListTag listTag) {
UtilityBeltInventory.Mutable inv = new UtilityBeltInventory.Mutable(UtilityBeltInventory.EMPTY);

for (int i = 0; i < listTag.size(); ++i) {
ItemStack itemStack = ItemStack.of(listTag.getCompound(i));
if (!itemStack.isEmpty()) {
inv.setItem(i, itemStack);
}
}

return inv.toImmutable();
}

public ListTag toTag() {
ListTag listTag = new ListTag();

for (int i = 0; i < this.getContainerSize(); ++i) {
ItemStack itemStack = this.getItem(i);
listTag.add(itemStack.save(new CompoundTag()));
}

return listTag;
}
}
Loading

0 comments on commit a734516

Please sign in to comment.