Skip to content

Commit

Permalink
Forge & NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Jun 15, 2024
1 parent 2a1b3cc commit 470abab
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 429 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ Check out the [wiki](https://github.com/Rakambda/FallingTree/wiki) for some more

* For Fabric users, **Fabric API** is required.
* For Neoforge/Forge users, **Cloth Config API** is optional on the client and allows you to have a config GUI in game.
Starting Minecraft 1.21, enchantments needs to be added separately through a datapack [available here](https://modrinth.com/datapack/fallingtree-enchantments).

NOTE: On servers this mod can be server-side only, client isn't required (except if you use the change speed feature and/or enchants configurations).
26 changes: 12 additions & 14 deletions forge/src/main/java/fr/rakambda/fallingtree/forge/FallingTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;

@Log4j2
Expand All @@ -15,20 +14,19 @@ public class FallingTree{
private static final FallingTreeCommonsImpl mod = new FallingTreeCommonsImpl();

public FallingTree(){
if(ModList.get().isLoaded("cloth_config")){
// try{
// Class.forName("fr.rakambda.fallingtree.forge.client.cloth.ClothConfigHook")
// .asSubclass(ClothConfigHook.class)
// .getConstructor(FallingTreeCommon.class)
// .newInstance(mod)
// .load();
// }
// catch(ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e){
// log.error("Failed to hook into ClothConfig", e);
// }
}
// if(ModList.get().isLoaded("cloth_config")){
// try{
// Class.forName("fr.rakambda.fallingtree.forge.client.cloth.ClothConfigHook")
// .asSubclass(ClothConfigHook.class)
// .getConstructor(FallingTreeCommon.class)
// .newInstance(mod)
// .load();
// }
// catch(ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e){
// log.error("Failed to hook into ClothConfig", e);
// }
// }

mod.registerEnchant();
mod.registerForge(MinecraftForge.EVENT_BUS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.rakambda.fallingtree.forge;

import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

public class FallingTreeUtils{
@NotNull
public static ResourceLocation id(@NotNull String name){
return ResourceLocation.fromNamespaceAndPath(FallingTree.MOD_ID, name);
}

@NotNull
public static ResourceLocation idExternal(@NotNull String fullName){
return ResourceLocation.parse(fullName);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package fr.rakambda.fallingtree.forge.common;

import fr.rakambda.fallingtree.common.FallingTreeCommon;
import fr.rakambda.fallingtree.common.config.enums.BreakMode;
import fr.rakambda.fallingtree.common.leaf.LeafBreakingHandler;
import fr.rakambda.fallingtree.common.network.ServerPacketHandler;
import fr.rakambda.fallingtree.common.wrapper.*;
import fr.rakambda.fallingtree.common.wrapper.DirectionCompat;
import fr.rakambda.fallingtree.common.wrapper.IBlock;
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.IItem;
import fr.rakambda.fallingtree.common.wrapper.IItemStack;
import fr.rakambda.fallingtree.common.wrapper.ILevel;
import fr.rakambda.fallingtree.common.wrapper.IPlayer;
import fr.rakambda.fallingtree.forge.client.event.PlayerLeaveListener;
import fr.rakambda.fallingtree.forge.common.wrapper.*;
import fr.rakambda.fallingtree.forge.event.*;
import fr.rakambda.fallingtree.forge.common.wrapper.BlockWrapper;
import fr.rakambda.fallingtree.forge.common.wrapper.ComponentWrapper;
import fr.rakambda.fallingtree.forge.common.wrapper.ItemStackWrapper;
import fr.rakambda.fallingtree.forge.common.wrapper.ItemWrapper;
import fr.rakambda.fallingtree.forge.event.BlockBreakListener;
import fr.rakambda.fallingtree.forge.event.FallingTreeBlockBreakEvent;
import fr.rakambda.fallingtree.forge.event.LeafBreakingListener;
import fr.rakambda.fallingtree.forge.event.ServerCommandRegistrationListener;
import fr.rakambda.fallingtree.forge.network.ForgePacketHandler;
import fr.rakambda.fallingtree.forge.network.PlayerJoinListener;
import lombok.Getter;
Expand All @@ -20,34 +35,48 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static fr.rakambda.fallingtree.forge.FallingTreeUtils.id;
import static fr.rakambda.fallingtree.forge.FallingTreeUtils.idExternal;
import static java.util.stream.Stream.empty;

public class FallingTreeCommonsImpl extends FallingTreeCommon<Direction>{
@Getter
private final LeafBreakingHandler leafBreakingHandler;
private final ForgePacketHandler packetHandler;
@Getter
private Collection<IEnchantment> chopperEnchantments;
private final TagKey<Enchantment> chopperEnchantmentTag;
@Getter
private final Map<BreakMode, TagKey<Enchantment>> breakModeChopperEnchantmentTag;

public FallingTreeCommonsImpl(){
leafBreakingHandler = new LeafBreakingHandler(this);
chopperEnchantments = new ArrayList<>();
packetHandler = new ForgePacketHandler(this);

chopperEnchantmentTag = TagKey.create(Registries.ENCHANTMENT, id("chopper_all"));

breakModeChopperEnchantmentTag = new HashMap<>();
breakModeChopperEnchantmentTag.put(BreakMode.FALL_ALL_BLOCK, TagKey.create(Registries.ENCHANTMENT, id("chopper_fall_all_block")));
breakModeChopperEnchantmentTag.put(BreakMode.FALL_BLOCK, TagKey.create(Registries.ENCHANTMENT, id("chopper_fall_block")));
breakModeChopperEnchantmentTag.put(BreakMode.FALL_ITEM, TagKey.create(Registries.ENCHANTMENT, id("chopper_fall_item")));
breakModeChopperEnchantmentTag.put(BreakMode.INSTANTANEOUS, TagKey.create(Registries.ENCHANTMENT, id("chopper_instantaneous")));
breakModeChopperEnchantmentTag.put(BreakMode.SHIFT_DOWN, TagKey.create(Registries.ENCHANTMENT, id("chopper_shift_down")));
}

@Override
Expand Down Expand Up @@ -78,7 +107,7 @@ public Stream<IBlock> getBlock(@NotNull String name){
if(isTag){
name = name.substring(1);
}
var resourceLocation = new ResourceLocation(name);
var resourceLocation = idExternal(name);
if(isTag){
var tag = TagKey.create(Registries.BLOCK, resourceLocation);
return getRegistryTagContent(ForgeRegistries.BLOCKS, tag).map(BlockWrapper::new);
Expand All @@ -98,7 +127,7 @@ public Stream<IItem> getItem(@NotNull String name){
if(isTag){
name = name.substring(1);
}
var resourceLocation = new ResourceLocation(name);
var resourceLocation = idExternal(name);
if(isTag){
var tag = TagKey.create(Registries.ITEM, resourceLocation);
return getRegistryTagContent(ForgeRegistries.ITEMS, tag).map(ItemWrapper::new);
Expand All @@ -113,7 +142,7 @@ public Stream<IItem> getItem(@NotNull String name){
@Override
public boolean isLeafBlock(@NotNull IBlock block){
var isAllowedBlock = registryTagContains(ForgeRegistries.BLOCKS, BlockTags.LEAVES, (Block) block.getRaw())
|| getConfiguration().getTrees().getAllowedLeaveBlocks(this).stream().anyMatch(leaf -> leaf.equals(block));
|| 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;
Expand All @@ -124,7 +153,7 @@ public boolean isLeafBlock(@NotNull IBlock block){
@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));
|| 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;
Expand Down Expand Up @@ -159,7 +188,7 @@ public Direction asDirection(@NotNull DirectionCompat dir){
@Override
public boolean isNetherWartOrShroomlight(@NotNull IBlock block){
return registryTagContains(ForgeRegistries.BLOCKS, BlockTags.WART_BLOCKS, (Block) block.getRaw())
|| Blocks.SHROOMLIGHT.equals(block.getRaw());
|| Blocks.SHROOMLIGHT.equals(block.getRaw());
}

@Override
Expand All @@ -172,37 +201,12 @@ public boolean checkCanBreakBlock(@NotNull ILevel level, @NotNull IBlockPos bloc
return !MinecraftForge.EVENT_BUS.post(new FallingTreeBlockBreakEvent((Level) level.getRaw(), (BlockPos) blockPos.getRaw(), (BlockState) blockState.getRaw(), (Player) player.getRaw()));
}

@Override
protected void performDefaultEnchantRegister(){
FallingTreeEnchantments.registerDefault();
}

@Override
protected void performSpecificEnchantRegister(){
FallingTreeEnchantments.registerSpecific();
}

@Override
protected void performCommitEnchantRegister(){
FallingTreeEnchantments.commit(FMLJavaModLoadingContext.get().getModEventBus());

Stream.of(FallingTreeEnchantments.CHOPPER_ENCHANTMENT,
FallingTreeEnchantments.CHOPPER_INSTANTANEOUS_ENCHANTMENT,
FallingTreeEnchantments.CHOPPER_FALL_BLOCK_ENCHANTMENT,
FallingTreeEnchantments.CHOPPER_FALL_ITEM_ENCHANTMENT,
FallingTreeEnchantments.CHOPPER_SHIFT_DOWN_ENCHANTMENT
)
.filter(Objects::nonNull)
.map(EnchantmentWrapper::new)
.forEach(chopperEnchantments::add);
}

@Override
@NotNull
public IItemStack getEmptyItemStack() {
public IItemStack getEmptyItemStack(){
return new ItemStackWrapper(ItemStack.EMPTY);
}

@NotNull
private <T> Optional<T> getRegistryElement(IForgeRegistry<T> registryKey, ResourceLocation identifier){
return registryKey.getHolder(identifier).map(Holder::value);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package fr.rakambda.fallingtree.forge.common.wrapper;

import fr.rakambda.fallingtree.common.wrapper.IEnchantment;
import fr.rakambda.fallingtree.common.config.enums.BreakMode;
import fr.rakambda.fallingtree.common.wrapper.IItem;
import fr.rakambda.fallingtree.common.wrapper.IItemStack;
import fr.rakambda.fallingtree.common.wrapper.IPlayer;
import fr.rakambda.fallingtree.forge.FallingTree;
import fr.rakambda.fallingtree.forge.compat.TetraCompat;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraftforge.common.ToolActions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Optional;

@RequiredArgsConstructor
Expand Down Expand Up @@ -52,7 +50,6 @@ public void damage(int amount, @NotNull IPlayer player){
raw.hurtAndBreak(amount, (Player) player.getRaw(), EquipmentSlot.MAINHAND);
TetraCompat.tickHoningProgression(this, player);
}


@Override
@NotNull
Expand All @@ -61,30 +58,19 @@ public IItem getItem(){
}

@Override
public int getEnchantLevel(@Nullable IEnchantment enchantment){
if(enchantment == null){
return 0;
}
return raw.getEnchantmentLevel((Enchantment) enchantment.getRaw());
public boolean hasChopperEnchant(){
return EnchantmentHelper.hasTag(raw, FallingTree.getMod().getChopperEnchantmentTag());
}

@Override
public boolean hasOneOfEnchantAtLeast(@NotNull Collection<IEnchantment> enchantments, int minLevel){
for(var enchantment : enchantments){
var key = (Enchantment) enchantment.getRaw();
if(raw.getEnchantmentLevel(key) >= minLevel){
return true;
}
public Optional<BreakMode> getBreakModeFromEnchant(){
if(!hasChopperEnchant()){
return Optional.empty();
}
return false;
}

@NotNull
public Optional<IEnchantment> getAnyEnchant(@NotNull Collection<IEnchantment> enchantments){
for(var enchantment : enchantments){
var key = (Enchantment) enchantment.getRaw();
if(raw.getEnchantmentLevel(key) > 0){
return Optional.of(enchantment);
var tags = FallingTree.getMod().getBreakModeChopperEnchantmentTag();
for(var breakMode : tags.keySet()){
if(EnchantmentHelper.hasTag(raw, tags.get(breakMode))){
return Optional.of(breakMode);
}
}
return Optional.empty();
Expand Down

This file was deleted.

Loading

0 comments on commit 470abab

Please sign in to comment.