Skip to content

Commit

Permalink
feat: 1.20.6 port
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed May 20, 2024
1 parent 629cbf8 commit 3c484ef
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 256 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Setup JDK 17
- name: Setup JDK 21
uses: actions/setup-java@v1
with:
java-version: 17
java-version: 21

- name: Build
run: ./gradlew clean neoforge:build fabric:build
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ allprojects {

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.release = 17
options.release = 21
}

tasks.create("prepareWorkspace") {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.github.jamalam360.utility_belt.server.ServerStateManager;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
Expand All @@ -27,50 +28,48 @@
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);
private static final DeferredRegister<DataComponentType<?>> COMPONENT_TYPES = DeferredRegister.create(MOD_ID, Registries.DATA_COMPONENT_TYPE);

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 final RegistrySupplier<DataComponentType<UtilityBeltInventory>> UTILITY_BELT_INVENTORY_COMPONENT_TYPE = COMPONENT_TYPES.register("utility_belt_inventory", () ->
DataComponentType.<UtilityBeltInventory>builder().persistent(UtilityBeltInventory.CODEC).networkSynchronized(UtilityBeltInventory.STREAM_CODEC).build()
);

public static ResourceLocation id(String path) {
return new ResourceLocation(MOD_ID, path);
}
public static void init() {
JamLib.checkForJarRenaming(UtilityBelt.class);
ITEMS.register();
MENUS.register();
COMPONENT_TYPES.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()).get(UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()));
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,73 @@
package io.github.jamalam360.utility_belt;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import com.mojang.serialization.Codec;
import java.util.List;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;

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 static final Codec<UtilityBeltInventory> CODEC = Codec
.list(ItemStack.CODEC)
.xmap(UtilityBeltInventory::new, UtilityBeltInventory::getItems);

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


public UtilityBeltInventory() {
super(4);
}

private UtilityBeltInventory(List<ItemStack> stacks) {
super(4);

for (int i = 0; i < stacks.size(); i++) {
this.setItem(i, stacks.get(i));
}
}

@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 int hashCode() {
int hash = 0;
for (int i = 0; i < this.getContainerSize(); i++) {
hash += this.getItem(i).hashCode();
}

return hash;
}

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.jamalam360.utility_belt.client.ClientNetworking;
import java.util.List;
import java.util.function.Consumer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.Mth;
Expand All @@ -24,15 +23,14 @@
import net.minecraft.world.item.TieredItem;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.TridentItem;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

public class UtilityBeltItem extends Item {

private static final int BAR_COLOR = Mth.color(0.4F, 0.4F, 1.0F);

public UtilityBeltItem() {
super(new Item.Properties().stacksTo(1));
super(new Item.Properties().stacksTo(1).component(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), new UtilityBeltInventory()));
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
Expand Down Expand Up @@ -66,26 +64,24 @@ public static boolean isValidItem(ItemStack stack) {
return stack.getItem() instanceof TieredItem || stack.getItem() instanceof ProjectileWeaponItem || stack.getItem() instanceof FishingRodItem || stack.getItem() instanceof SpyglassItem || stack.getItem() instanceof TridentItem || stack.getItem() instanceof FlintAndSteelItem || stack.getItem() instanceof ShearsItem || stack.getItem() instanceof BrushItem || stack.isEmpty() || stack.is(UtilityBelt.ALLOWED_IN_UTILITY_BELT);
}

// Going to keep this name as is until 1.20.4 support is dropped, to keep the diffs smaller
public static UtilityBeltInventory getInventoryFromTag(ItemStack stack) {
UtilityBeltInventory inv = new UtilityBeltInventory();

if (stack != null) {
CompoundTag tag = stack.getOrCreateTag();
if (!tag.contains("Inventory")) {
tag.put("Inventory", inv.createTag());
} else {
inv.fromTag(tag.getList("Inventory", CompoundTag.TAG_COMPOUND));
}
if (!stack.has(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get())) {
stack.set(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), new UtilityBeltInventory());
}

return inv;
return stack.get(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get());
}

public static void setInventory(ItemStack stack, UtilityBeltInventory inv) {
stack.set(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), inv);
}

@Nullable
public static ItemStack getBelt(Player player) {
ItemStack stack = UtilityBeltPlatform.getStackInBeltSlot(player);

if (stack != null && stack.getItem() == UtilityBelt.UTILITY_BELT_ITEM.get()) {
if (stack != null && stack.getItem() == UtilityBelt.UTILITY_BELT_ITEM.get()) {
return stack;
} else {
return null;
Expand All @@ -109,7 +105,7 @@ public int getBarColor(ItemStack itemStack) {
}

@Override
public void appendHoverText(ItemStack itemStack, @Nullable Level level, List<Component> list, TooltipFlag tooltipFlag) {
public void appendHoverText(ItemStack itemStack, TooltipContext tooltipContext, List<Component> list, TooltipFlag tooltipFlag) {
UtilityBeltInventory inv = getInventoryFromTag(itemStack);

for (int i = 0; i < inv.getContainerSize(); i++) {
Expand All @@ -134,7 +130,7 @@ public boolean overrideStackedOnOther(ItemStack belt, Slot slot, ClickAction cli
}

playInsertSound(player);
belt.getOrCreateTag().put("Inventory", inv.createTag());
setInventory(belt, inv);
return true;
}

Expand All @@ -151,7 +147,7 @@ public boolean overrideOtherStackedOnMe(ItemStack belt, ItemStack otherStack, Sl
}

playInsertSound(player);
belt.getOrCreateTag().put("Inventory", inv.createTag());
setInventory(belt, inv);
return true;
}

Expand Down
Loading

0 comments on commit 3c484ef

Please sign in to comment.