Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to 1.20.6 #242

Merged
merged 8 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.4.+'
id 'fabric-loom' version '1.6.+'
id 'maven-publish'
id 'checkstyle'
}
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.1
loader_version=0.14.22
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
loader_version=0.15.10

# Mod Properties
mod_version = 9.2.2
maven_group = io.github.cottonmc
archives_base_name = LibGui

# Dependencies
fabric_version=0.89.2+1.20.2
jankson_version=6.0.0+j1.2.3
modmenu_version=8.0.0
fabric_version=0.97.8+1.20.6
jankson_version=7.0.0+j1.2.3
modmenu_version=10.0.0-beta.1
libninepatch_version=1.2.0
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public ItemStack quickMove(PlayerEntity player, int index) {
/** WILL MODIFY toInsert! Returns true if anything was inserted. */
private boolean insertIntoExisting(ItemStack toInsert, Slot slot, PlayerEntity player) {
ItemStack curSlotStack = slot.getStack();
if (!curSlotStack.isEmpty() && ItemStack.canCombine(toInsert, curSlotStack) && slot.canInsert(toInsert)) {
if (!curSlotStack.isEmpty() && ItemStack.areItemsAndComponentsEqual(toInsert, curSlotStack) && slot.canInsert(toInsert)) {
int combinedAmount = curSlotStack.getCount() + toInsert.getCount();
int maxAmount = Math.min(toInsert.getMaxCount(), slot.getMaxItemCount(toInsert));
if (combinedAmount <= maxAmount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.github.cottonmc.cotton.gui.impl;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.util.Identifier;

Expand All @@ -26,8 +31,34 @@ public class ScreenNetworkingImpl implements ScreenNetworking {
// message: identifier
// rest: buf

public static final Identifier SCREEN_MESSAGE_S2C = new Identifier(LibGuiCommon.MOD_ID, "screen_message_s2c");
public static final Identifier SCREEN_MESSAGE_C2S = new Identifier(LibGuiCommon.MOD_ID, "screen_message_c2s");
public record ScreenMessageData(int syncId, Identifier message, PacketByteBuf buf) {
public static final PacketCodec<RegistryByteBuf, ScreenMessageData> PACKET_CODEC = PacketCodec.tuple(
PacketCodecs.INTEGER, ScreenMessageData::syncId,
Identifier.PACKET_CODEC, ScreenMessageData::message,
PacketCodec.of(PacketByteBuf::writeBytes, packetBuf -> packetBuf.readBytes(PacketByteBufs.create())), ScreenMessageData::buf,
ScreenMessageData::new
);
}

public record S2CScreenMessage(ScreenMessageData data) implements CustomPayload {
public static final Id<S2CScreenMessage> PACKET_ID = new Id<>(new Identifier(LibGuiCommon.MOD_ID, "screen_message_s2c"));
public static final PacketCodec<RegistryByteBuf, S2CScreenMessage> PACKET_CODEC = ScreenMessageData.PACKET_CODEC.xmap(S2CScreenMessage::new, S2CScreenMessage::data);

@Override
public Id<? extends CustomPayload> getId() {
return PACKET_ID;
}
}

public record C2SScreenMessage(ScreenMessageData data) implements CustomPayload {
public static final Id<C2SScreenMessage> PACKET_ID = new Id<>(new Identifier(LibGuiCommon.MOD_ID, "screen_message_c2s"));
public static final PacketCodec<RegistryByteBuf, C2SScreenMessage> PACKET_CODEC = ScreenMessageData.PACKET_CODEC.xmap(C2SScreenMessage::new, C2SScreenMessage::data);

@Override
public Id<? extends CustomPayload> getId() {
return PACKET_ID;
}
}

private static final Logger LOGGER = LogManager.getLogger();
private static final Map<SyncedGuiDescription, ScreenNetworkingImpl> instanceCache = new WeakHashMap<>();
Expand Down Expand Up @@ -58,51 +89,48 @@ public void send(Identifier message, Consumer<PacketByteBuf> writer) {
Objects.requireNonNull(writer, "writer");

PacketByteBuf buf = PacketByteBufs.create();
buf.writeVarInt(description.syncId);
buf.writeIdentifier(message);
writer.accept(buf);
description.getPacketSender().sendPacket(side == NetworkSide.SERVER ? SCREEN_MESSAGE_S2C : SCREEN_MESSAGE_C2S, buf);
ScreenMessageData data = new ScreenMessageData(description.syncId, message, buf);
description.getPacketSender().sendPacket(side == NetworkSide.SERVER ? new S2CScreenMessage(data) : new C2SScreenMessage(data));
}

public static void init() {
ServerPlayNetworking.registerGlobalReceiver(SCREEN_MESSAGE_C2S, (server, player, networkHandler, buf, responseSender) -> {
handle(server, player, buf);
PayloadTypeRegistry.playS2C().register(S2CScreenMessage.PACKET_ID, S2CScreenMessage.PACKET_CODEC);
PayloadTypeRegistry.playC2S().register(C2SScreenMessage.PACKET_ID, C2SScreenMessage.PACKET_CODEC);
ServerPlayNetworking.registerGlobalReceiver(C2SScreenMessage.PACKET_ID, (payload, context) -> {
handle(context.player().server, context.player(), payload.data());
});
}

public static void handle(Executor executor, PlayerEntity player, PacketByteBuf buf) {
public static void handle(Executor executor, PlayerEntity player, ScreenMessageData data) {
ScreenHandler screenHandler = player.currentScreenHandler;

// Packet data
int syncId = buf.readVarInt();
Identifier messageId = buf.readIdentifier();

if (!(screenHandler instanceof SyncedGuiDescription)) {
LOGGER.error("Received message packet for screen handler {} which is not a SyncedGuiDescription", screenHandler);
return;
} else if (syncId != screenHandler.syncId) {
LOGGER.error("Received message for sync ID {}, current sync ID: {}", syncId, screenHandler.syncId);
} else if (data.syncId() != screenHandler.syncId) {
LOGGER.error("Received message for sync ID {}, current sync ID: {}", data.syncId(), screenHandler.syncId);
return;
}

ScreenNetworkingImpl networking = instanceCache.get(screenHandler);

if (networking != null) {
MessageReceiver receiver = networking.messages.get(messageId);
MessageReceiver receiver = networking.messages.get(data.message());

if (receiver != null) {
buf.retain();
data.buf().retain();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the buffer is now created by us, I think this extra retain might leak memory:

  • Initial state: refcount = 1
  • retain: refcount = 2
  • release: refcount = 1 (needs 0 to be deallocated according to Netty ReferenceCounted docs)

executor.execute(() -> {
try {
receiver.onMessage(buf);
receiver.onMessage(data.buf());
} catch (Exception e) {
LOGGER.error("Error handling screen message {} for {} on side {}", messageId, screenHandler, networking.side, e);
LOGGER.error("Error handling screen message {} for {} on side {}", data.message(), screenHandler, networking.side, e);
} finally {
buf.release();
data.buf().release();
}
});
} else {
LOGGER.warn("Message {} not registered for {} on side {}", messageId, screenHandler, networking.side);
LOGGER.warn("Message {} not registered for {} on side {}", data.message(), screenHandler, networking.side);
}
} else {
LOGGER.warn("GUI description {} does not use networking", screenHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class LibGuiClient implements ClientModInitializer {
public void onInitializeClient() {
config = loadConfig();

ClientPlayNetworking.registerGlobalReceiver(ScreenNetworkingImpl.SCREEN_MESSAGE_S2C, (client, networkHandler, buf, responseSender) -> {
ScreenNetworkingImpl.handle(client, client.player, buf);
ClientPlayNetworking.registerGlobalReceiver(ScreenNetworkingImpl.S2CScreenMessage.PACKET_ID, (payload, context) -> {
ScreenNetworkingImpl.handle(context.client(), context.player(), payload.data());
});

LibGuiShaders.register();
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
"java": ">=17",
"fabricloader": ">=0.14.22",
"fabric-api-base": ">=0.4.4",
"fabric-lifecycle-events-v1": "^2.0.2",
"fabric-networking-api-v1": "^3.0.5",
"fabric-rendering-v1": "^3.0.6",
"fabric-lifecycle-events-v1": ">=2.0.2",
"fabric-networking-api-v1": ">=3.0.5",
"fabric-rendering-v1": ">=3.0.6",
"fabric-resource-loader-v0": "*",
"minecraft": ">=1.20.2",
"jankson": "^6.0.0",
"minecraft": ">=1.20.5",
"jankson": "^7.0.0",
"libninepatch": "^1.2.0"
},
"suggests": {
Expand Down
14 changes: 9 additions & 5 deletions src/testMod/java/io/github/cottonmc/test/GuiBlock.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package io.github.cottonmc.test;

import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import com.mojang.serialization.MapCodec;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class GuiBlock extends BlockWithEntity {

public GuiBlock() {
super(FabricBlockSettings.copy(Blocks.IRON_BLOCK));
super(Settings.copy(Blocks.IRON_BLOCK));
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hitResult) {
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
player.openHandledScreen(state.createScreenHandlerFactory(world, pos));
return ActionResult.SUCCESS;
}
Expand All @@ -34,4 +33,9 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}

@Override
protected MapCodec<? extends BlockWithEntity> getCodec() {
return null;
}
}
7 changes: 3 additions & 4 deletions src/testMod/java/io/github/cottonmc/test/GuiItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.inventory.StackReference;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -35,7 +34,7 @@ private NamedScreenHandlerFactory createScreenHandlerFactory(PlayerEntity player
case OFF_HAND -> EquipmentSlot.OFFHAND;
};
ItemStack stack = player.getStackInHand(hand);
return new ExtendedScreenHandlerFactory() {
return new ExtendedScreenHandlerFactory<EquipmentSlot>() {
@Override
public Text getDisplayName() {
return stack.getName();
Expand All @@ -47,8 +46,8 @@ public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, Pla
}

@Override
public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) {
buf.writeEnumConstant(slot);
public EquipmentSlot getScreenOpeningData(ServerPlayerEntity player) {
return slot;
}
};
}
Expand Down
11 changes: 5 additions & 6 deletions src/testMod/java/io/github/cottonmc/test/LibGuiTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.cottonmc.test;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
Expand All @@ -14,6 +13,7 @@
import net.minecraft.inventory.StackReference;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.resource.featuretoggle.FeatureFlags;
Expand Down Expand Up @@ -49,18 +49,17 @@ public void onInitialize() {
NO_BLOCK_INVENTORY_BLOCK = new NoBlockInventoryBlock(AbstractBlock.Settings.copy(Blocks.STONE));
Registry.register(Registries.BLOCK, new Identifier(MODID, "no_block_inventory"), NO_BLOCK_INVENTORY_BLOCK);
Registry.register(Registries.ITEM, new Identifier(MODID, "no_block_inventory"), new BlockItem(NO_BLOCK_INVENTORY_BLOCK, new Item.Settings()));
GUI_BLOCKENTITY_TYPE = FabricBlockEntityTypeBuilder.create(GuiBlockEntity::new, GUI_BLOCK).build(null);
GUI_BLOCKENTITY_TYPE = BlockEntityType.Builder.create(GuiBlockEntity::new, GUI_BLOCK).build(null);
Registry.register(Registries.BLOCK_ENTITY_TYPE, new Identifier(MODID, "gui"), GUI_BLOCKENTITY_TYPE);

GUI_SCREEN_HANDLER_TYPE = new ScreenHandlerType<>((int syncId, PlayerInventory inventory) -> {
return new TestDescription(GUI_SCREEN_HANDLER_TYPE, syncId, inventory, ScreenHandlerContext.EMPTY);
}, FeatureSet.of(FeatureFlags.VANILLA));
Registry.register(Registries.SCREEN_HANDLER, new Identifier(MODID, "gui"), GUI_SCREEN_HANDLER_TYPE);
ITEM_SCREEN_HANDLER_TYPE = new ExtendedScreenHandlerType<>((syncId, inventory, buf) -> {
var equipmentSlot = buf.readEnumConstant(EquipmentSlot.class);
StackReference handStack = StackReference.of(inventory.player, equipmentSlot);
ITEM_SCREEN_HANDLER_TYPE = new ExtendedScreenHandlerType<>((syncId, inventory, slot) -> {
StackReference handStack = StackReference.of(inventory.player, slot);
return new TestItemDescription(syncId, inventory, handStack);
});
}, PacketCodecs.codec(EquipmentSlot.CODEC).cast());
Registry.register(Registries.SCREEN_HANDLER, new Identifier(MODID, "item_gui"), ITEM_SCREEN_HANDLER_TYPE);

REALLY_SIMPLE_SCREEN_HANDLER_TYPE = new ScreenHandlerType<>(ReallySimpleDescription::new, FeatureSet.of(FeatureFlags.VANILLA));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -21,7 +20,7 @@ public NoBlockInventoryBlock(Settings settings) {
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
player.openHandledScreen(this);
return world.isClient ? ActionResult.SUCCESS : ActionResult.CONSUME;
}
Expand Down