Skip to content

Commit

Permalink
wip: Port to Minecraft 1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed May 18, 2024
1 parent 13ca1d9 commit f90f747
Show file tree
Hide file tree
Showing 37 changed files with 292 additions and 365 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Fixed item use events staying cancelled after one was cancelled
- Updated to Minecraft 1.20.6
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.2-SNAPSHOT' apply(false)
id 'fabric-loom' version '1.6-SNAPSHOT' apply(false)
// id 'org.spongepowered.gradle.vanilla' version '0.2.1-SNAPSHOT' apply(false) // this causes ForgeGradle to fail in reobf
id "org.jetbrains.gradle.plugin.idea-ext" version "1.1.7" // this is required for VanillaGradle and NeoForge to exist in harmony
id 'org.spongepowered.mixin' version '0.7-SNAPSHOT' apply(false)
Expand Down Expand Up @@ -74,7 +74,7 @@ subprojects {
"pack_format_number": pack_format_number
]

filesMatching(['pack.mcmeta', 'fabric.mod.json', 'META-INF/mods.toml', '*.mixins.json']) {
filesMatching(['pack.mcmeta', 'fabric.mod.json', 'META-INF/mods.toml', 'META-INF/neoforge.mods.toml', '*.mixins.json']) {
expand expandProps
}
inputs.properties(expandProps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.fabricmc.fabric.api.registry.FuelRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -94,7 +95,7 @@ public DyeColor getColor(ItemStack itemStack) {

@Override
public boolean canItemsStack(ItemStack first, ItemStack second) {
return !first.isEmpty() && ItemStack.isSameItemSameTags(first, second);
return !first.isEmpty() && ItemStack.isSameItemSameComponents(first, second);
}

@Override
Expand Down Expand Up @@ -168,7 +169,8 @@ public boolean isShield(ItemStack itemStack) {

@Override
public boolean isRepairable(ItemStack itemStack) {
return itemStack.getItem().canBeDepleted();
final var repairCost = itemStack.getItem().components().get(DataComponents.REPAIR_COST);
return repairCost != null && repairCost > 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.blay09.mods.balm.api.client.screen.BalmScreenFactory;
import net.blay09.mods.balm.api.client.screen.BalmScreens;
import net.fabricmc.fabric.api.client.screen.v1.Screens;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand All @@ -15,7 +15,7 @@
public class FabricBalmScreens implements BalmScreens {
@Override
public <T extends AbstractContainerMenu, S extends Screen & MenuAccess<T>> void registerScreen(Supplier<MenuType<? extends T>> type, BalmScreenFactory<T, S> screenFactory) {
ScreenRegistry.register(type.get(), screenFactory::create);
MenuScreens.register(type.get(), screenFactory::create);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public static void registerEvents(FabricBalmEvents events) {
}));
});

events.registerEvent(ItemTooltipEvent.class, () -> ItemTooltipCallback.EVENT.register((stack, context, lines) -> {
final ItemTooltipEvent event = new ItemTooltipEvent(stack, Minecraft.getInstance().player, lines, context);
events.registerEvent(ItemTooltipEvent.class, () -> ItemTooltipCallback.EVENT.register((stack, context, flag, lines) -> {
final ItemTooltipEvent event = new ItemTooltipEvent(stack, Minecraft.getInstance().player, lines, flag);
events.fireEventHandlers(event);
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.common.collect.Multimaps;
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.item.BalmItems;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.core.Registry;
Expand All @@ -15,7 +14,6 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
Expand All @@ -34,7 +32,7 @@ public class FabricBalmItems implements BalmItems {

@Override
public Item.Properties itemProperties() {
return new FabricItemSettings();
return new Item.Properties();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.menu.BalmMenus;
import net.blay09.mods.balm.api.menu.BalmMenuFactory;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
Expand All @@ -12,7 +11,10 @@ public class FabricBalmMenus implements BalmMenus {

@Override
public <T extends AbstractContainerMenu> DeferredObject<MenuType<T>> registerMenu(ResourceLocation identifier, BalmMenuFactory<T> factory) {
return new DeferredObject<>(identifier, () -> ScreenHandlerRegistry.registerExtended(identifier, factory::create)).resolveImmediately();
return new DeferredObject<>(identifier, () -> {
// return TODO Registry.register(Registries.MENU, identifier, MenuType.ANVIL);
return (MenuType<T>) MenuType.ANVIL;
}).resolveImmediately();
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package net.blay09.mods.balm.fabric.network;

import net.blay09.mods.balm.api.client.BalmClient;
import net.blay09.mods.balm.api.menu.BalmMenuProvider;
import net.blay09.mods.balm.api.network.BalmNetworking;
import net.blay09.mods.balm.api.network.ClientboundMessageRegistration;
import net.blay09.mods.balm.api.network.MessageRegistration;
import net.blay09.mods.balm.api.network.ServerboundMessageRegistration;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -25,21 +25,16 @@
import net.minecraft.world.inventory.AbstractContainerMenu;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class FabricBalmNetworking implements BalmNetworking {

private static final Map<Class<?>, MessageRegistration<?>> messagesByClass = new HashMap<>();
private static final Map<ResourceLocation, MessageRegistration<?>> messagesByIdentifier = new HashMap<>();
private static final Map<CustomPacketPayload.Type<? extends CustomPacketPayload>, MessageRegistration<RegistryFriendlyByteBuf, ? extends CustomPacketPayload>> messagesByType = new HashMap<>();

private static final List<ClientboundMessageRegistration<?>> clientMessageRegistrations = new ArrayList<>();

private static Player replyPlayer;
private static PacketSender replyPacketSender;

@Override
public void allowClientOnly(String modId) {
Expand All @@ -51,11 +46,11 @@ public void allowServerOnly(String modId) {

@Override
public void openGui(Player player, MenuProvider menuProvider) {
if (menuProvider instanceof BalmMenuProvider balmMenuProvider) {
player.openMenu(new ExtendedScreenHandlerFactory() {
if (menuProvider instanceof BalmMenuProvider<?> balmMenuProvider) {
player.openMenu(new ExtendedScreenHandlerFactory<>() {
@Override
public void writeScreenOpeningData(ServerPlayer player, FriendlyByteBuf buf) {
balmMenuProvider.writeScreenOpeningData(player, buf);
public Object getScreenOpeningData(ServerPlayer player) {
return balmMenuProvider.getScreenOpeningData(player);
}

@Override
Expand All @@ -75,108 +70,76 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe
}

@Override
public <T> void reply(T message) {
if (replyPlayer == null) {
throw new IllegalStateException("No player to reply to");
public <T extends CustomPacketPayload> void reply(T message) {
if (replyPacketSender == null) {
throw new IllegalStateException("No context to reply to");
}

sendTo(replyPlayer, message);
replyPacketSender.sendPacket(message);
}

@SuppressWarnings("unchecked")
@Override
public <T> void sendTo(Player player, T message) {
MessageRegistration<T> messageRegistration = (MessageRegistration<T>) messagesByClass.get(message.getClass());
ResourceLocation identifier = messageRegistration.getIdentifier();
FriendlyByteBuf buf = PacketByteBufs.create();
messageRegistration.getEncodeFunc().accept(message, buf);
ServerPlayNetworking.send((ServerPlayer) player, identifier, buf);
public <T extends CustomPacketPayload> void sendTo(Player player, T message) {
ServerPlayNetworking.send((ServerPlayer) player, message);
}

@SuppressWarnings("unchecked")
@Override
public <T> void sendToTracking(ServerLevel world, BlockPos pos, T message) {
MessageRegistration<T> messageRegistration = (MessageRegistration<T>) messagesByClass.get(message.getClass());
ResourceLocation identifier = messageRegistration.getIdentifier();
FriendlyByteBuf buf = PacketByteBufs.create();
messageRegistration.getEncodeFunc().accept(message, buf);
public <T extends CustomPacketPayload> void sendToTracking(ServerLevel world, BlockPos pos, T message) {
for (ServerPlayer player : PlayerLookup.tracking(world, pos)) {
ServerPlayNetworking.send(player, identifier, buf);
ServerPlayNetworking.send(player, message);
}
}

@SuppressWarnings("unchecked")
@Override
public <T> void sendToTracking(Entity entity, T message) {
MessageRegistration<T> messageRegistration = (MessageRegistration<T>) messagesByClass.get(message.getClass());
ResourceLocation identifier = messageRegistration.getIdentifier();
FriendlyByteBuf buf = PacketByteBufs.create();
messageRegistration.getEncodeFunc().accept(message, buf);
public <T extends CustomPacketPayload> void sendToTracking(Entity entity, T message) {
for (ServerPlayer player : PlayerLookup.tracking(entity)) {
ServerPlayNetworking.send(player, identifier, buf);
ServerPlayNetworking.send(player, message);
}
}

@SuppressWarnings("unchecked")
@Override
public <T> void sendToAll(MinecraftServer server, T message) {
MessageRegistration<T> messageRegistration = (MessageRegistration<T>) messagesByClass.get(message.getClass());
ResourceLocation identifier = messageRegistration.getIdentifier();
FriendlyByteBuf buf = PacketByteBufs.create();
messageRegistration.getEncodeFunc().accept(message, buf);
public <T extends CustomPacketPayload> void sendToAll(MinecraftServer server, T message) {
for (ServerPlayer player : PlayerLookup.all(server)) {
ServerPlayNetworking.send(player, identifier, buf);
ServerPlayNetworking.send(player, message);
}
}

@SuppressWarnings("unchecked")
@Override
public <T> void sendToServer(T message) {
MessageRegistration<T> messageRegistration = (MessageRegistration<T>) messagesByClass.get(message.getClass());
ResourceLocation identifier = messageRegistration.getIdentifier();
FriendlyByteBuf buf = PacketByteBufs.create();
messageRegistration.getEncodeFunc().accept(message, buf);
ClientPlayNetworking.send(identifier, buf);
public <T extends CustomPacketPayload> void sendToServer(T message) {
ClientPlayNetworking.send(message);
}

@Override
public <T> void registerClientboundPacket(ResourceLocation identifier, Class<T> clazz, BiConsumer<T, FriendlyByteBuf> encodeFunc, Function<FriendlyByteBuf, T> decodeFunc, BiConsumer<Player, T> handler) {
ClientboundMessageRegistration<T> messageRegistration = new ClientboundMessageRegistration<>(identifier, clazz, encodeFunc, decodeFunc, handler);

messagesByClass.put(clazz, messageRegistration);
messagesByIdentifier.put(identifier, messageRegistration);

clientMessageRegistrations.add(messageRegistration);
public <T extends CustomPacketPayload> void registerClientboundPacket(ResourceLocation identifier, Class<T> clazz, BiConsumer<RegistryFriendlyByteBuf, T> encodeFunc, Function<RegistryFriendlyByteBuf, T> decodeFunc, BiConsumer<Player, T> handler) {
final var type = new CustomPacketPayload.Type<T>(identifier);
final var messageRegistration = new ClientboundMessageRegistration<>(type, clazz, encodeFunc, decodeFunc, handler);
messagesByType.put(type, messageRegistration);
}

@Override
public <T> void registerServerboundPacket(ResourceLocation identifier, Class<T> clazz, BiConsumer<T, FriendlyByteBuf> encodeFunc, Function<FriendlyByteBuf, T> decodeFunc, BiConsumer<ServerPlayer, T> handler) {
MessageRegistration<T> messageRegistration = new ServerboundMessageRegistration<>(identifier, clazz, encodeFunc, decodeFunc, handler);

messagesByClass.put(clazz, messageRegistration);
messagesByIdentifier.put(identifier, messageRegistration);
ServerPlayNetworking.registerGlobalReceiver(identifier, ((server, player, listener, buf, responseSender) -> {
T message = messageRegistration.getDecodeFunc().apply(buf);
server.execute(() -> {
replyPlayer = player;
handler.accept(player, message);
replyPlayer = null;
});
}));
public <T extends CustomPacketPayload> void registerServerboundPacket(ResourceLocation identifier, Class<T> clazz, BiConsumer<RegistryFriendlyByteBuf, T> encodeFunc, Function<RegistryFriendlyByteBuf, T> decodeFunc, BiConsumer<ServerPlayer, T> handler) {
final var type = new CustomPacketPayload.Type<T>(identifier);
final var messageRegistration = new ServerboundMessageRegistration<>(type, clazz, encodeFunc, decodeFunc, handler);
messagesByType.put(type, messageRegistration);

ServerPlayNetworking.registerGlobalReceiver(type, ((payload, context) -> context.player().getServer().execute(() -> {
replyPacketSender = context.responseSender();
handler.accept(context.player(), payload);
replyPacketSender = null;
})));
}

public static void initializeClientHandlers() {
for (ClientboundMessageRegistration<?> message : clientMessageRegistrations) {
registerClientHandler(message);
for (final var messageRegistration : messagesByType.values()) {
if (messageRegistration instanceof ClientboundMessageRegistration<RegistryFriendlyByteBuf, ?> clientboundMessageRegistration) {
registerClientHandler(clientboundMessageRegistration);
}
}
}

private static <T> void registerClientHandler(ClientboundMessageRegistration<T> messageRegistration) {
ResourceLocation identifier = messageRegistration.getIdentifier();
BiConsumer<Player, T> handler = messageRegistration.getHandler();
ClientPlayNetworking.registerGlobalReceiver(identifier, ((client, listener, buf, responseSender) -> {
T message = messageRegistration.getDecodeFunc().apply(buf);
client.execute(() -> handler.accept(BalmClient.getClientPlayer(), message));
}));
private static <TPayload extends CustomPacketPayload> void registerClientHandler(ClientboundMessageRegistration<RegistryFriendlyByteBuf, TPayload> messageRegistration) {
final var type = messageRegistration.getType();
BiConsumer<Player, TPayload> handler = messageRegistration.getHandler();
ClientPlayNetworking.registerGlobalReceiver(type, ((payload, context) -> context.client().execute(() -> handler.accept(context.player(), payload))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.blay09.mods.balm.api.entity.BalmEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.RandomSource;
Expand Down Expand Up @@ -48,12 +49,6 @@ public boolean growCrop(ItemStack itemStack, Level level, BlockPos pos, Player p
@Override
public CompoundTag getPersistentData(Entity entity) {
CompoundTag persistentData = entity.getPersistentData();
if (entity instanceof Player) {
CompoundTag persistedTag = persistentData.getCompound(Player.PERSISTED_NBT_TAG);
persistentData.put(Player.PERSISTED_NBT_TAG, persistedTag);
persistentData = persistedTag;
}

CompoundTag balmData = persistentData.getCompound("BalmData");
if (balmData.isEmpty()) {
// If we have no data, try to import from Fabric in case the world was migrated
Expand All @@ -68,7 +63,7 @@ public CompoundTag getPersistentData(Entity entity) {

@Override
public void curePotionEffects(LivingEntity entity, ItemStack curativeItem) {
entity.curePotionEffects(curativeItem);
entity.removeAllEffects();
}

@Override
Expand Down Expand Up @@ -113,7 +108,8 @@ public boolean isShield(ItemStack itemStack) {

@Override
public boolean isRepairable(ItemStack itemStack) {
return itemStack.isRepairable();
final var repairCost = itemStack.getItem().components().get(DataComponents.REPAIR_COST);
return repairCost != null && repairCost > 0;
}

@Override
Expand All @@ -128,6 +124,6 @@ public MinecraftServer getServer() {

@Override
public double getBlockReachDistance(Player player) {
return player.getBlockReach();
return 4.5 + (player.isCreative() ? 0.5 : 0);
}
}
Loading

0 comments on commit f90f747

Please sign in to comment.