-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64a1cb5
commit 5406d6e
Showing
13 changed files
with
400 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/dev/dubhe/anvilcraft/enchantment/FellingEffect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.dubhe.anvilcraft.enchantment; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.MethodsReturnNonnullByDefault; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.item.enchantment.EnchantedItemInUse; | ||
import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect; | ||
import net.minecraft.world.phys.Vec3; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
@ParametersAreNonnullByDefault | ||
@MethodsReturnNonnullByDefault | ||
public record FellingEffect(int range) implements EnchantmentEntityEffect { | ||
public static final MapCodec<FellingEffect> CODEC = RecordCodecBuilder.mapCodec(it -> | ||
it.group( | ||
Codec.INT.optionalFieldOf("range", 3).forGetter(FellingEffect::range) | ||
).apply(it, FellingEffect::new) | ||
); | ||
|
||
@Override | ||
public void apply(ServerLevel serverLevel, int i, EnchantedItemInUse enchantedItemInUse, Entity entity, Vec3 vec3) { | ||
System.out.println("FELLING!!!"); | ||
} | ||
|
||
@Override | ||
public MapCodec<? extends EnchantmentEntityEffect> codec() { | ||
return CODEC; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/dev/dubhe/anvilcraft/enchantment/HarvestEffect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package dev.dubhe.anvilcraft.enchantment; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.MethodsReturnNonnullByDefault; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.enchantment.EnchantedItemInUse; | ||
import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect; | ||
import net.minecraft.world.level.block.CropBlock; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.phys.Vec3; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Deque; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
@ParametersAreNonnullByDefault | ||
@MethodsReturnNonnullByDefault | ||
public record HarvestEffect(int range) implements EnchantmentEntityEffect { | ||
private static final List<BlockPos> ITERATING_OFFSET = new ArrayList<>(); | ||
|
||
static { | ||
for (int dx = -1; dx <= 1; dx++) { | ||
for (int dz = -1; dz <= 1; dz++) { | ||
ITERATING_OFFSET.add(new BlockPos(dx, 0, dz)); | ||
} | ||
} | ||
} | ||
|
||
public static final MapCodec<HarvestEffect> CODEC = RecordCodecBuilder.mapCodec(it -> | ||
it.group( | ||
Codec.INT.optionalFieldOf("range", 3) | ||
.forGetter(HarvestEffect::range) | ||
).apply(it, HarvestEffect::new) | ||
); | ||
|
||
@Override | ||
public void apply(ServerLevel level, int enchantmentLevel, EnchantedItemInUse enchantedItemInUse, Entity owner, Vec3 vec3) { | ||
ItemStack tool = enchantedItemInUse.itemStack(); | ||
BlockPos pos = BlockPos.containing(vec3); | ||
if (!(owner instanceof Player player)) return; | ||
BlockState targetBlockState = level.getBlockState(pos); | ||
boolean hasValidCropBlock = false; | ||
if (targetBlockState.getBlock() instanceof CropBlock cropBlock) { | ||
if (cropBlock.isMaxAge(targetBlockState)) { | ||
hasValidCropBlock = true; | ||
BlockEntity entity = level.getBlockEntity(pos); | ||
cropBlock.playerWillDestroy(level, pos, targetBlockState, player); | ||
cropBlock.playerDestroy(level, player, pos, targetBlockState, entity, tool); | ||
level.setBlockAndUpdate(pos, cropBlock.getStateForAge(0)); | ||
} | ||
} else return; | ||
int max = enchantmentLevel * 2 + 1; | ||
max *= max; | ||
if (hasValidCropBlock) max -= 1; | ||
Deque<BlockPos> iteratingPos = new ArrayDeque<>(); | ||
iteratingPos.push(pos); | ||
List<BlockPos> vis = new ArrayList<>(); | ||
while (!iteratingPos.isEmpty() && max >= 0) { | ||
BlockPos currentIterating = iteratingPos.pop(); | ||
if (vis.contains(currentIterating)) continue; | ||
vis.add(currentIterating); | ||
max--; | ||
for (BlockPos offset : ITERATING_OFFSET) { | ||
BlockPos currentPos = currentIterating.offset(offset); | ||
iteratingPos.add(currentPos); | ||
} | ||
BlockState state = level.getBlockState(currentIterating); | ||
if (!(state.getBlock() instanceof CropBlock block)) continue; | ||
if (!block.isMaxAge(state)) continue; | ||
BlockState newState = block.getStateForAge(0); | ||
cropBlock.playerWillDestroy(level, pos, targetBlockState, player); | ||
cropBlock.playerDestroy(level, player, pos, targetBlockState, level.getBlockEntity(currentIterating), tool); | ||
level.setBlockAndUpdate(currentIterating, newState); | ||
} | ||
} | ||
|
||
@Override | ||
public MapCodec<? extends EnchantmentEntityEffect> codec() { | ||
return CODEC; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/dev/dubhe/anvilcraft/init/ModEnchantmentEffectComponents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dev.dubhe.anvilcraft.init; | ||
|
||
import dev.dubhe.anvilcraft.AnvilCraft; | ||
import net.minecraft.core.component.DataComponentType; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.world.item.enchantment.ConditionalEffect; | ||
import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
|
||
import java.util.List; | ||
import java.util.function.UnaryOperator; | ||
|
||
public class ModEnchantmentEffectComponents { | ||
|
||
private static final DeferredRegister<DataComponentType<?>> REGISTER = | ||
DeferredRegister.create(Registries.ENCHANTMENT_EFFECT_COMPONENT_TYPE, AnvilCraft.MOD_ID); | ||
|
||
public static final DataComponentType<List<ConditionalEffect<EnchantmentEntityEffect>>> USE_ON_BLOCK = register( | ||
"use_on_block", | ||
(it) -> it.persistent( | ||
ConditionalEffect.codec( | ||
EnchantmentEntityEffect.CODEC, | ||
ModLootContextParamSets.USE_ON_ITEM | ||
).listOf() | ||
) | ||
); | ||
|
||
private static <T> DataComponentType<T> register(String name, UnaryOperator<DataComponentType.Builder<T>> operator) { | ||
DataComponentType<T> dct = operator.apply(DataComponentType.builder()).build(); | ||
REGISTER.register(name, () -> dct); | ||
return dct; | ||
} | ||
|
||
public static void register(IEventBus bus) { | ||
REGISTER.register(bus); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/dev/dubhe/anvilcraft/init/ModEnchantmentEffects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dev.dubhe.anvilcraft.init; | ||
|
||
import com.mojang.serialization.MapCodec; | ||
import dev.dubhe.anvilcraft.AnvilCraft; | ||
import dev.dubhe.anvilcraft.enchantment.FellingEffect; | ||
import dev.dubhe.anvilcraft.enchantment.HarvestEffect; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
|
||
public class ModEnchantmentEffects { | ||
public static final DeferredRegister<MapCodec<? extends EnchantmentEntityEffect>> REGISTER = | ||
DeferredRegister.create(Registries.ENCHANTMENT_ENTITY_EFFECT_TYPE, AnvilCraft.MOD_ID); | ||
static { | ||
REGISTER.register( | ||
"harvest", | ||
() -> HarvestEffect.CODEC | ||
); | ||
REGISTER.register( | ||
"felling", | ||
() -> FellingEffect.CODEC | ||
); | ||
} | ||
|
||
public static void register(IEventBus eventBus){ | ||
REGISTER.register(eventBus); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.