From e5025d7065c6bc282f57755f35592be88f69edfc Mon Sep 17 00:00:00 2001 From: Thomas <1688389+Rakambda@users.noreply.github.com> Date: Tue, 5 Dec 2023 20:57:33 +0100 Subject: [PATCH] Add support to Fabric 1.20.3 (#641) * 23w40a * 23w43b * Update versions * Bump versions * Fix translations * 1.20.3 * NotNull --- .../fallingtree/common/wrapper/IBlock.java | 3 + .../fabric/common/FallingTreeCommonsImpl.java | 393 +++++++++--------- .../fabric/common/wrapper/BlockWrapper.java | 7 + fabric/src/main/resources/fabric.mod.json | 4 +- .../forge/common/FallingTreeCommonsImpl.java | 10 +- .../forge/common/wrapper/BlockWrapper.java | 5 + gradle.properties | 2 +- gradle/libs.versions.toml | 8 +- 8 files changed, 233 insertions(+), 199 deletions(-) diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IBlock.java b/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IBlock.java index 422014c8..9327de50 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IBlock.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/wrapper/IBlock.java @@ -7,4 +7,7 @@ public interface IBlock extends IWrapper{ boolean isAir(); void playerDestroy(@NotNull ILevel level, @NotNull IPlayer player, @NotNull IBlockPos blockPos, @NotNull IBlockState blockState, @Nullable IBlockEntity blockEntity, @NotNull IItemStack itemStack); + + @NotNull + IComponent getAsComponent(); } diff --git a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/FallingTreeCommonsImpl.java b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/FallingTreeCommonsImpl.java index f0642548..739af384 100644 --- a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/FallingTreeCommonsImpl.java +++ b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/FallingTreeCommonsImpl.java @@ -43,6 +43,7 @@ import net.minecraft.world.level.block.Blocks; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Optional; import java.util.Set; @@ -50,195 +51,205 @@ import java.util.stream.Stream; import static java.util.stream.Stream.empty; -public class FallingTreeCommonsImpl extends FallingTreeCommon { - @Getter - private final LeafBreakingHandler leafBreakingHandler; - @Getter - private final ServerPacketHandler serverPacketHandler; - @Getter - private Collection chopperEnchantments; - - public FallingTreeCommonsImpl() { - leafBreakingHandler = new LeafBreakingHandler(this); - chopperEnchantments = new ArrayList<>(); - serverPacketHandler = new FabricServerPacketHandler(this); - } - - @Override - @NotNull - public IComponent translate(@NotNull String key, Object... objects) { - return new ComponentWrapper(Component.translatable(key, null, objects)); - } - - @Override - @NotNull - public Stream getBlock(@NotNull String name) { - try { - var isTag = name.startsWith("#"); - if (isTag) { - name = name.substring(1); - } - var identifier = new ResourceLocation(name); - if (isTag) { - var tag = TagKey.create(Registries.BLOCK, identifier); - return getRegistryTagContent(BuiltInRegistries.BLOCK, tag).map(BlockWrapper::new); - } - return getRegistryElement(BuiltInRegistries.BLOCK, identifier).stream().map(BlockWrapper::new); - } catch (Exception e) { - return empty(); - } - } - - @Override - @NotNull - public Stream getItem(@NotNull String name) { - try { - var isTag = name.startsWith("#"); - if (isTag) { - name = name.substring(1); - } - var identifier = new ResourceLocation(name); - if (isTag) { - var tag = TagKey.create(Registries.ITEM, identifier); - return getRegistryTagContent(BuiltInRegistries.ITEM, tag).map(ItemWrapper::new); - } - return getRegistryElement(BuiltInRegistries.ITEM, identifier).stream().map(ItemWrapper::new); - } catch (Exception e) { - return empty(); - } - } - - @Override - public boolean isLeafBlock(@NotNull IBlock block) { - var isAllowedBlock = registryTagContains(BuiltInRegistries.BLOCK, BlockTags.LEAVES, (Block) block.getRaw()) - || getConfiguration().getTrees().getAllowedLeaveBlocks(this).stream().anyMatch(leaf -> leaf.equals(block)); - if (isAllowedBlock) { - var isDeniedBlock = getConfiguration().getTrees().getDeniedLeaveBlocks(this).stream().anyMatch(leaf -> leaf.equals(block)); - return !isDeniedBlock; - } - return false; - } - - @Override - public boolean isLogBlock(@NotNull IBlock block) { - var isAllowedBlock = getConfiguration().getTrees().getDefaultLogsBlocks(this).stream().anyMatch(log -> log.equals(block)) - || getConfiguration().getTrees().getAllowedLogBlocks(this).stream().anyMatch(log -> log.equals(block)); - if (isAllowedBlock) { - var isDeniedBlock = getConfiguration().getTrees().getDeniedLogBlocks(this).stream().anyMatch(log -> log.equals(block)); - return !isDeniedBlock; - } - return false; - } - - @Override - @NotNull - public Set getAllNonStrippedLogsBlocks() { - return getRegistryTagContent(BuiltInRegistries.BLOCK, BlockTags.LOGS) - .filter(block -> !Optional.of(BuiltInRegistries.BLOCK.getKey(block)) - .map(ResourceLocation::getPath) - .map(name -> name.startsWith("stripped")) - .orElse(false)) - .map(BlockWrapper::new) - .collect(Collectors.toSet()); - } - - @Override - @NotNull - public DirectionCompat asDirectionCompat(@NotNull Direction dir) { - return DirectionCompat.valueOf(dir.name()); - } - - @Override - @NotNull - public Direction asDirection(@NotNull DirectionCompat dir) { - return Direction.valueOf(dir.name()); - } - - @Override - public boolean isNetherWartOrShroomlight(@NotNull IBlock block) { - return registryTagContains(BuiltInRegistries.BLOCK, BlockTags.WART_BLOCKS, (Block) block.getRaw()) - || Blocks.SHROOMLIGHT.equals(block.getRaw()); - } - - @Override - public boolean isMangroveRoots(@NotNull IBlock block) { - return Blocks.MANGROVE_ROOTS.equals(block.getRaw()); - } - - @Override - public boolean checkCanBreakBlock(@NotNull ILevel level, @NotNull IBlockPos blockPos, @NotNull IBlockState blockState, @NotNull IPlayer player) { - return true; - } - - @Override - protected void performDefaultEnchantRegister() { - chopperEnchantments.add(new EnchantmentWrapper(Registry.register( - BuiltInRegistries.ENCHANTMENT, - new ResourceLocation(FallingTree.MOD_ID, "chopper"), - new ChopperEnchantment(this, null) - ))); - } - - @Override - protected void performSpecificEnchantRegister() { - chopperEnchantments.add(new EnchantmentWrapper(Registry.register( - BuiltInRegistries.ENCHANTMENT, - new ResourceLocation(FallingTree.MOD_ID, "chopper_instantaneous"), - new ChopperEnchantment(this, BreakMode.INSTANTANEOUS) - ))); - chopperEnchantments.add(new EnchantmentWrapper(Registry.register( - BuiltInRegistries.ENCHANTMENT, - new ResourceLocation(FallingTree.MOD_ID, "chopper_fall_block"), - new ChopperEnchantment(this, BreakMode.FALL_BLOCK) - ))); - chopperEnchantments.add(new EnchantmentWrapper(Registry.register( - BuiltInRegistries.ENCHANTMENT, - new ResourceLocation(FallingTree.MOD_ID, "chopper_fall_all_block"), - new ChopperEnchantment(this, BreakMode.FALL_ALL_BLOCK) - ))); - chopperEnchantments.add(new EnchantmentWrapper(Registry.register( - BuiltInRegistries.ENCHANTMENT, - new ResourceLocation(FallingTree.MOD_ID, "chopper_fall_item"), - new ChopperEnchantment(this, BreakMode.FALL_ITEM) - ))); - chopperEnchantments.add(new EnchantmentWrapper(Registry.register( - BuiltInRegistries.ENCHANTMENT, - new ResourceLocation(FallingTree.MOD_ID, "chopper_shift_down"), - new ChopperEnchantment(this, BreakMode.SHIFT_DOWN) - ))); - } - - @Override - protected void performCommitEnchantRegister() { - } - - @Override - @NotNull - public IItemStack getEmptyItemStack() { - return new ItemStackWrapper(ItemStack.EMPTY); - } - - @NotNull - private Optional getRegistryElement(Registry registryKey, ResourceLocation identifier) { - return registryKey.getOptional(identifier); - } - - @NotNull - private Stream getRegistryTagContent(@NotNull Registry registry, @NotNull TagKey tag) { - return registry.getTag(tag).stream() - .flatMap(a -> a.stream().map(Holder::value)); - } - - private boolean registryTagContains(@NotNull Registry registry, @NotNull TagKey tag, @NotNull T element) { - return getRegistryTagContent(registry, tag).anyMatch(element::equals); - } - - public void register() { - getServerPacketHandler().registerServer(); - - ServerTickEvents.END_SERVER_TICK.register(new LeafBreakingListener(this)); - PlayerBlockBreakEvents.BEFORE.register(new BlockBreakListener(this)); - - CommandRegistrationCallback.EVENT.register(new ServerCommandRegistrationListener(this)); - } +public class FallingTreeCommonsImpl extends FallingTreeCommon{ + @Getter + private final LeafBreakingHandler leafBreakingHandler; + @Getter + private final ServerPacketHandler serverPacketHandler; + @Getter + private Collection chopperEnchantments; + + public FallingTreeCommonsImpl(){ + leafBreakingHandler = new LeafBreakingHandler(this); + chopperEnchantments = new ArrayList<>(); + serverPacketHandler = new FabricServerPacketHandler(this); + } + + @Override + @NotNull + public IComponent translate(@NotNull String key, Object... objects){ + Object[] vars = Arrays.stream(objects) + .map(o -> { + if(o instanceof IComponent component){ + return component.getRaw(); + } + return o; + }) + .toArray(); + return new ComponentWrapper(Component.translatable(key, vars)); + } + + @Override + @NotNull + public Stream getBlock(@NotNull String name){ + try{ + var isTag = name.startsWith("#"); + if(isTag){ + name = name.substring(1); + } + var identifier = new ResourceLocation(name); + if(isTag){ + var tag = TagKey.create(Registries.BLOCK, identifier); + return getRegistryTagContent(BuiltInRegistries.BLOCK, tag).map(BlockWrapper::new); + } + return getRegistryElement(BuiltInRegistries.BLOCK, identifier).stream().map(BlockWrapper::new); + } + catch(Exception e){ + return empty(); + } + } + + @Override + @NotNull + public Stream getItem(@NotNull String name){ + try{ + var isTag = name.startsWith("#"); + if(isTag){ + name = name.substring(1); + } + var identifier = new ResourceLocation(name); + if(isTag){ + var tag = TagKey.create(Registries.ITEM, identifier); + return getRegistryTagContent(BuiltInRegistries.ITEM, tag).map(ItemWrapper::new); + } + return getRegistryElement(BuiltInRegistries.ITEM, identifier).stream().map(ItemWrapper::new); + } + catch(Exception e){ + return empty(); + } + } + + @Override + public boolean isLeafBlock(@NotNull IBlock block){ + var isAllowedBlock = registryTagContains(BuiltInRegistries.BLOCK, BlockTags.LEAVES, (Block) block.getRaw()) + || getConfiguration().getTrees().getAllowedLeaveBlocks(this).stream().anyMatch(leaf -> leaf.equals(block)); + if(isAllowedBlock){ + var isDeniedBlock = getConfiguration().getTrees().getDeniedLeaveBlocks(this).stream().anyMatch(leaf -> leaf.equals(block)); + return !isDeniedBlock; + } + return false; + } + + @Override + public boolean isLogBlock(@NotNull IBlock block){ + var isAllowedBlock = getConfiguration().getTrees().getDefaultLogsBlocks(this).stream().anyMatch(log -> log.equals(block)) + || getConfiguration().getTrees().getAllowedLogBlocks(this).stream().anyMatch(log -> log.equals(block)); + if(isAllowedBlock){ + var isDeniedBlock = getConfiguration().getTrees().getDeniedLogBlocks(this).stream().anyMatch(log -> log.equals(block)); + return !isDeniedBlock; + } + return false; + } + + @Override + @NotNull + public Set getAllNonStrippedLogsBlocks(){ + return getRegistryTagContent(BuiltInRegistries.BLOCK, BlockTags.LOGS) + .filter(block -> !Optional.of(BuiltInRegistries.BLOCK.getKey(block)) + .map(ResourceLocation::getPath) + .map(name -> name.startsWith("stripped")) + .orElse(false)) + .map(BlockWrapper::new) + .collect(Collectors.toSet()); + } + + @Override + @NotNull + public DirectionCompat asDirectionCompat(@NotNull Direction dir){ + return DirectionCompat.valueOf(dir.name()); + } + + @Override + @NotNull + public Direction asDirection(@NotNull DirectionCompat dir){ + return Direction.valueOf(dir.name()); + } + + @Override + public boolean isNetherWartOrShroomlight(@NotNull IBlock block){ + return registryTagContains(BuiltInRegistries.BLOCK, BlockTags.WART_BLOCKS, (Block) block.getRaw()) + || Blocks.SHROOMLIGHT.equals(block.getRaw()); + } + + @Override + public boolean isMangroveRoots(@NotNull IBlock block){ + return Blocks.MANGROVE_ROOTS.equals(block.getRaw()); + } + + @Override + public boolean checkCanBreakBlock(@NotNull ILevel level, @NotNull IBlockPos blockPos, @NotNull IBlockState blockState, @NotNull IPlayer player){ + return true; + } + + @Override + protected void performDefaultEnchantRegister(){ + chopperEnchantments.add(new EnchantmentWrapper(Registry.register( + BuiltInRegistries.ENCHANTMENT, + new ResourceLocation(FallingTree.MOD_ID, "chopper"), + new ChopperEnchantment(this, null) + ))); + } + + @Override + protected void performSpecificEnchantRegister(){ + chopperEnchantments.add(new EnchantmentWrapper(Registry.register( + BuiltInRegistries.ENCHANTMENT, + new ResourceLocation(FallingTree.MOD_ID, "chopper_instantaneous"), + new ChopperEnchantment(this, BreakMode.INSTANTANEOUS) + ))); + chopperEnchantments.add(new EnchantmentWrapper(Registry.register( + BuiltInRegistries.ENCHANTMENT, + new ResourceLocation(FallingTree.MOD_ID, "chopper_fall_block"), + new ChopperEnchantment(this, BreakMode.FALL_BLOCK) + ))); + chopperEnchantments.add(new EnchantmentWrapper(Registry.register( + BuiltInRegistries.ENCHANTMENT, + new ResourceLocation(FallingTree.MOD_ID, "chopper_fall_all_block"), + new ChopperEnchantment(this, BreakMode.FALL_ALL_BLOCK) + ))); + chopperEnchantments.add(new EnchantmentWrapper(Registry.register( + BuiltInRegistries.ENCHANTMENT, + new ResourceLocation(FallingTree.MOD_ID, "chopper_fall_item"), + new ChopperEnchantment(this, BreakMode.FALL_ITEM) + ))); + chopperEnchantments.add(new EnchantmentWrapper(Registry.register( + BuiltInRegistries.ENCHANTMENT, + new ResourceLocation(FallingTree.MOD_ID, "chopper_shift_down"), + new ChopperEnchantment(this, BreakMode.SHIFT_DOWN) + ))); + } + + @Override + protected void performCommitEnchantRegister(){ + } + + @Override + @NotNull + public IItemStack getEmptyItemStack(){ + return new ItemStackWrapper(ItemStack.EMPTY); + } + + @NotNull + private Optional getRegistryElement(Registry registryKey, ResourceLocation identifier){ + return registryKey.getOptional(identifier); + } + + @NotNull + private Stream getRegistryTagContent(@NotNull Registry registry, @NotNull TagKey tag){ + return registry.getTag(tag).stream() + .flatMap(a -> a.stream().map(Holder::value)); + } + + private boolean registryTagContains(@NotNull Registry registry, @NotNull TagKey tag, @NotNull T element){ + return getRegistryTagContent(registry, tag).anyMatch(element::equals); + } + + public void register(){ + getServerPacketHandler().registerServer(); + + ServerTickEvents.END_SERVER_TICK.register(new LeafBreakingListener(this)); + PlayerBlockBreakEvents.BEFORE.register(new BlockBreakListener(this)); + + CommandRegistrationCallback.EVENT.register(new ServerCommandRegistrationListener(this)); + } } diff --git a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/BlockWrapper.java b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/BlockWrapper.java index 9f9bf153..aa53faea 100644 --- a/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/BlockWrapper.java +++ b/fabric/src/main/java/fr/rakambda/fallingtree/fabric/common/wrapper/BlockWrapper.java @@ -4,6 +4,7 @@ import fr.rakambda.fallingtree.common.wrapper.IBlockEntity; import fr.rakambda.fallingtree.common.wrapper.IBlockPos; import fr.rakambda.fallingtree.common.wrapper.IBlockState; +import fr.rakambda.fallingtree.common.wrapper.IComponent; import fr.rakambda.fallingtree.common.wrapper.IItemStack; import fr.rakambda.fallingtree.common.wrapper.ILevel; import fr.rakambda.fallingtree.common.wrapper.IPlayer; @@ -39,6 +40,12 @@ public void playerDestroy(@NotNull ILevel level, @NotNull IPlayer player, @NotNu raw.playerDestroy((Level) level.getRaw(), (Player) player.getRaw(), (BlockPos) blockPos.getRaw(), (BlockState) blockState.getRaw(), entity, (ItemStack) itemStack.getRaw()); } + @Override + @NotNull + public IComponent getAsComponent(){ + return new ComponentWrapper(raw.getName()); + } + @Override public boolean equals(Object obj){ if(!(obj instanceof IBlock block)){ diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index 5c2e4f04..9f14ec85 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -32,9 +32,9 @@ ], "accessWidener" : "fallingtree.accesswidener", "depends": { - "fabricloader": ">=0.13.2", + "fabricloader": ">=0.15.0", "fabric-resource-loader-v0": "*", - "fabric-api": ">=0.20", + "fabric-api": ">=0.91", "java": ">=17" }, "suggests": { diff --git a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/FallingTreeCommonsImpl.java b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/FallingTreeCommonsImpl.java index 8b3e2861..19e2038a 100644 --- a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/FallingTreeCommonsImpl.java +++ b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/FallingTreeCommonsImpl.java @@ -53,7 +53,15 @@ public FallingTreeCommonsImpl(){ @Override @NotNull public IComponent translate(@NotNull String key, Object... objects){ - return new ComponentWrapper(Component.translatable(key, objects)); + Object[] vars = Arrays.stream(objects) + .map(o -> { + if(o instanceof IComponent component){ + return component.getRaw(); + } + return o; + }) + .toArray(); + return new ComponentWrapper(Component.translatable(key, vars)); } @Override diff --git a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/BlockWrapper.java b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/BlockWrapper.java index d0a5c3cf..5ef9f61a 100644 --- a/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/BlockWrapper.java +++ b/forge/src/main/java/fr/rakambda/fallingtree/forge/common/wrapper/BlockWrapper.java @@ -39,6 +39,11 @@ public void playerDestroy(@NotNull ILevel level, @NotNull IPlayer player, @NotNu raw.playerDestroy((Level) level.getRaw(), (Player) player.getRaw(), (BlockPos) blockPos.getRaw(), (BlockState) blockState.getRaw(), entity, (ItemStack) itemStack.getRaw()); } + @Override + public IComponent getAsComponent(){ + return new ComponentWrapper(raw.getName()); + } + @Override public boolean equals(Object obj){ if(!(obj instanceof IBlock block)){ diff --git a/gradle.properties b/gradle.properties index efabbf38..403cc7c7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.daemon=false modId=fallingtree modName=FallingTree includeFabric=true -includeForge=true +includeForge=false # Repository repoUser=Rakambda repoName=FallingTree diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5f317cf3..e7fa0eee 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,14 +7,14 @@ gson-version = "2.10.1" log4j2-version = "2.22.0" # Minecraft / Loaders -minecraftVersion = "1.20.2" -fabric-loader-version = "0.14.22" -fabric-api-version = "0.89.1+1.20.2" +minecraftVersion = "1.20.3" +fabric-loader-version = "0.15.0" +fabric-api-version = "0.91.1+1.20.3" forge-version = "1.20.2-48.0.1" forgeMappingsVersion = "1.20.2" # Mod dependencies -modmenu-version = "8.0.0" +modmenu-version = "9.0.0-pre.1" clothConfigVersion = "12.0.111" fabric-loom-version = "1.3.2"