Skip to content

Commit

Permalink
简单调整
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Mar 5, 2024
1 parent 2a9636e commit 3547160
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.dubhe.anvilcraft.api.events;

import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.event.SubscribeEvent;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.dubhe.anvilcraft.event;
package dev.dubhe.anvilcraft.api.events;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.util.GsonHelper;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -24,6 +25,7 @@
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package dev.dubhe.anvilcraft.event;

import lombok.Getter;
import lombok.Setter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.Level;

public record AnvilFallOnLandEvent(Level level, BlockPos pos, FallingBlockEntity entity) {
@Getter
public class AnvilFallOnLandEvent {
private final Level level;
private final BlockPos pos;
private final FallingBlockEntity entity;
@Setter
private boolean isAnvilDamage;

public AnvilFallOnLandEvent(Level level, BlockPos pos, FallingBlockEntity entity) {
this.level = level;
this.pos = pos;
this.entity = entity;
this.isAnvilDamage = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.dubhe.anvilcraft.event.listener;

import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.data.recipe.ModRecipeTypes;
import dev.dubhe.anvilcraft.data.recipe.anvil.item.ItemAnvilRecipe;
import dev.dubhe.anvilcraft.event.AnvilFallOnLandEvent;
import dev.dubhe.anvilcraft.event.SubscribeEvent;
import dev.dubhe.anvilcraft.api.events.SubscribeEvent;
import dev.dubhe.anvilcraft.inventory.ItemAnvilCraftingContainer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.item.crafting.Recipe;
Expand All @@ -16,8 +15,8 @@
public class AnvilEventListener {
@SubscribeEvent
public void onLand(@NotNull AnvilFallOnLandEvent event) {
Level level = event.level();
ItemAnvilCraftingContainer container = new ItemAnvilCraftingContainer(level, event.pos(), event.entity());
Level level = event.getLevel();
ItemAnvilCraftingContainer container = new ItemAnvilCraftingContainer(level, event.getPos(), event.getEntity());
MinecraftServer server = level.getServer();
if (null == server) return;
for (Recipe<?> recipe : server.getRecipeManager().getRecipes().stream().filter(recipe -> recipe instanceof ItemAnvilRecipe).toList()) {
Expand All @@ -27,5 +26,6 @@ public void onLand(@NotNull AnvilFallOnLandEvent event) {
if (optional.isEmpty()) return;
ItemAnvilRecipe recipe = optional.get();
recipe.craft(container, level);
if (recipe.isAnvilDamage()) event.setAnvilDamage(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
Expand All @@ -18,13 +19,18 @@

@Mixin(FallingBlockEntity.class)
public abstract class FallingBlockEntityMixin {
@Shadow private BlockState blockState;
@Shadow
private BlockState blockState;
@Unique
private final FallingBlockEntity ths = (FallingBlockEntity) (Object) this;

@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;getBlockState(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/level/block/state/BlockState;"), locals = LocalCapture.CAPTURE_FAILHARD)
private void fallOn(CallbackInfo ci, Block block, BlockPos blockPos, boolean bl, boolean bl2, double d) {
if (!this.blockState.is(BlockTags.ANVIL)) return;
AnvilCraft.EVENT_BUS.post(new AnvilFallOnLandEvent(ths.level(), blockPos, ths));
AnvilFallOnLandEvent event = new AnvilFallOnLandEvent(ths.level(), blockPos, ths);
AnvilCraft.EVENT_BUS.post(event);
if (event.isAnvilDamage()) {
this.blockState = AnvilBlock.damage(this.blockState);
}
}
}

0 comments on commit 3547160

Please sign in to comment.