diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerEndTickEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerEndTickEvent.java deleted file mode 100644 index 136e5bf53..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerEndTickEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import net.minecraft.server.MinecraftServer; - -public class ServerEndTickEvent extends ServerEvent { - public ServerEndTickEvent(MinecraftServer server) { - super(server); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerLevelLoadEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerLevelLoadEvent.java deleted file mode 100644 index 381279695..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerLevelLoadEvent.java +++ /dev/null @@ -1,15 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import lombok.Getter; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerLevel; - -@Getter -public class ServerLevelLoadEvent extends ServerEvent { - private final ServerLevel level; - - public ServerLevelLoadEvent(MinecraftServer server, ServerLevel level) { - super(server); - this.level = level; - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerLevelUnloadEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerLevelUnloadEvent.java deleted file mode 100644 index d40222603..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerLevelUnloadEvent.java +++ /dev/null @@ -1,15 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import lombok.Getter; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerLevel; - -@Getter -public class ServerLevelUnloadEvent extends ServerEvent { - private final ServerLevel level; - - public ServerLevelUnloadEvent(MinecraftServer server, ServerLevel level) { - super(server); - this.level = level; - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartTickEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartTickEvent.java deleted file mode 100644 index 356a381b1..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartTickEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import net.minecraft.server.MinecraftServer; - -public class ServerStartTickEvent extends ServerEvent { - public ServerStartTickEvent(MinecraftServer server) { - super(server); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartingEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartingEvent.java deleted file mode 100644 index 484e0ec96..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartingEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import net.minecraft.server.MinecraftServer; - -public class ServerStartingEvent extends ServerEvent { - public ServerStartingEvent(MinecraftServer server) { - super(server); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStoppedEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStoppedEvent.java deleted file mode 100644 index 8f8537618..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStoppedEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import net.minecraft.server.MinecraftServer; - -public class ServerStoppedEvent extends ServerEvent { - public ServerStoppedEvent(MinecraftServer server) { - super(server); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStoppingEvent.java b/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStoppingEvent.java deleted file mode 100644 index ae6e855a8..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStoppingEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.dubhe.anvilcraft.api.event.server; - -import net.minecraft.server.MinecraftServer; - -public class ServerStoppingEvent extends ServerEvent { - public ServerStoppingEvent(MinecraftServer server) { - super(server); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/registry/AnvilCraftRegistrate.java b/common/src/main/java/dev/dubhe/anvilcraft/api/registry/AnvilCraftRegistrate.java index 4c7565831..0a0ced6fe 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/registry/AnvilCraftRegistrate.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/registry/AnvilCraftRegistrate.java @@ -9,7 +9,7 @@ import com.tterrag.registrate.util.nullness.NonNullFunction; import com.tterrag.registrate.util.nullness.NonNullSupplier; import dev.architectury.injectables.annotations.ExpectPlatform; -import dev.dubhe.anvilcraft.util.FormattingUtil; +import dev.dubhe.anvilcraft.util.IFormattingUtil; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.CreativeModeTab; @@ -42,7 +42,7 @@ public static AnvilCraftRegistrate create(String modId) { String name, NonNullFunction factory ) { - return super.item(name, factory).lang(FormattingUtil.toEnglishName(name.replaceAll("/.", "_"))); + return super.item(name, factory).lang(IFormattingUtil.toEnglishName(name.replaceAll("/.", "_"))); } private RegistryEntry currentTab; diff --git a/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/ConfigScreenLang.java b/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/ConfigScreenLang.java index d7c57c053..03088768e 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/ConfigScreenLang.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/data/generator/lang/ConfigScreenLang.java @@ -4,7 +4,7 @@ import com.tterrag.registrate.providers.RegistrateLangProvider; import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.config.AnvilCraftConfig; -import dev.dubhe.anvilcraft.util.FormattingUtil; +import dev.dubhe.anvilcraft.util.IFormattingUtil; import me.shedaniel.autoconfig.ConfigData; import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment; import org.jetbrains.annotations.NotNull; @@ -36,7 +36,7 @@ private static void readConfigClass( if (field.isAnnotationPresent(SerializedName.class)) { name = field.getAnnotation(SerializedName.class).value(); } else { - name = FormattingUtil.toEnglishName(FormattingUtil.toLowerCaseUnder(fieldName)); + name = IFormattingUtil.toEnglishName(IFormattingUtil.toLowerCaseUnder(fieldName)); } provider.add(OPTION_STRING.formatted(AnvilCraft.MOD_ID, fieldName), name); if (field.isAnnotationPresent(Comment.class)) { diff --git a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilRecipe.java b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilRecipe.java index 162c8ff6f..ef0da22c5 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilRecipe.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilRecipe.java @@ -13,7 +13,7 @@ import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasBlockIngredient; import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasItem; import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasItemIngredient; -import dev.dubhe.anvilcraft.util.IItemStackInjector; +import dev.dubhe.anvilcraft.util.IItemStackUtil; import lombok.Getter; import net.minecraft.advancements.Advancement; import net.minecraft.advancements.AdvancementRewards; @@ -138,7 +138,7 @@ public enum Serializer implements RecipeSerializer { public @NotNull AnvilRecipe fromJson(@NotNull ResourceLocation recipeId, @NotNull JsonObject serializedRecipe) { ItemStack icon = ItemStack.EMPTY; if (serializedRecipe.has("icon")) { - icon = IItemStackInjector.fromJson(serializedRecipe.get("icon")); + icon = IItemStackUtil.fromJson(serializedRecipe.get("icon")); } AnvilRecipe recipe = new AnvilRecipe(recipeId, icon); JsonArray predicates = GsonHelper.getAsJsonArray(serializedRecipe, "predicates"); @@ -677,12 +677,11 @@ static class Result implements FinishedRecipe { } @Override - @SuppressWarnings("UnreachableCode") public void serializeRecipeData(@NotNull JsonObject json) { if (!this.group.isEmpty()) { json.addProperty("group", this.group); } - json.add("icon", ((IItemStackInjector) (Object) this.icon).anvilcraft$toJson()); + json.add("icon", IItemStackUtil.toJson(this.icon)); JsonArray predicates = new JsonArray(); for (RecipePredicate predicate : this.predicates) { predicates.add(predicate.toJson()); diff --git a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SetBlock.java b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SetBlock.java index 8b7ec5c4a..9cf9c945c 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SetBlock.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SetBlock.java @@ -8,7 +8,7 @@ import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilCraftingContainer; import dev.dubhe.anvilcraft.data.recipe.anvil.RecipeOutcome; -import dev.dubhe.anvilcraft.util.IBlockStateInjector; +import dev.dubhe.anvilcraft.util.IBlockStateUtil; import lombok.Getter; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; @@ -45,7 +45,7 @@ public SetBlock(Vec3 offset, double chance, BlockState result) { public SetBlock(@NotNull FriendlyByteBuf buffer) { this.offset = new Vec3(buffer.readVector3f()); this.chance = buffer.readDouble(); - this.result = IBlockStateInjector.fromJson(AnvilCraft.GSON.fromJson(buffer.readUtf(), JsonElement.class)); + this.result = IBlockStateUtil.fromJson(AnvilCraft.GSON.fromJson(buffer.readUtf(), JsonElement.class)); } /** @@ -67,7 +67,7 @@ public SetBlock(@NotNull JsonObject serializedRecipe) { if (serializedRecipe.has("chance")) { this.chance = GsonHelper.getAsDouble(serializedRecipe, "chance"); } else this.chance = 1.0; - this.result = IBlockStateInjector.fromJson(GsonHelper.getAsJsonObject(serializedRecipe, "result")); + this.result = IBlockStateUtil.fromJson(GsonHelper.getAsJsonObject(serializedRecipe, "result")); } @Override @@ -86,7 +86,7 @@ public void toNetwork(@NotNull FriendlyByteBuf buffer) { buffer.writeUtf(this.getType()); buffer.writeVector3f(this.offset.toVector3f()); buffer.writeDouble(this.chance); - buffer.writeUtf(((IBlockStateInjector) this.result).anvilcraft$toJson().toString()); + buffer.writeUtf(IBlockStateUtil.toJson(this.result).toString()); } @Override @@ -98,7 +98,7 @@ public JsonElement toJson() { object.addProperty("type", this.getType()); object.add("offset", offset); object.addProperty("chance", this.chance); - object.add("result", ((IBlockStateInjector) this.result).anvilcraft$toJson()); + object.add("result", IBlockStateUtil.toJson(this.result)); return object; } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SpawnItem.java b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SpawnItem.java index 21784f461..c4775b960 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SpawnItem.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/outcome/SpawnItem.java @@ -7,7 +7,7 @@ import com.google.gson.JsonSyntaxException; import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilCraftingContainer; import dev.dubhe.anvilcraft.data.recipe.anvil.RecipeOutcome; -import dev.dubhe.anvilcraft.util.IItemStackInjector; +import dev.dubhe.anvilcraft.util.IItemStackUtil; import lombok.Getter; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; @@ -67,7 +67,7 @@ public SpawnItem(@NotNull JsonObject serializedRecipe) { if (serializedRecipe.has("chance")) { this.chance = GsonHelper.getAsDouble(serializedRecipe, "chance"); } else this.chance = 1.0; - this.result = IItemStackInjector.fromJson(GsonHelper.getAsJsonObject(serializedRecipe, "result")); + this.result = IItemStackUtil.fromJson(GsonHelper.getAsJsonObject(serializedRecipe, "result")); } @Override @@ -98,7 +98,7 @@ public JsonElement toJson() { object.addProperty("type", this.getType()); object.add("offset", offset); object.addProperty("chance", this.chance); - object.add("result", ((IItemStackInjector) (Object) this.result).anvilcraft$toJson()); + object.add("result", IItemStackUtil.toJson(this.result)); return object; } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java b/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java index c553f1114..8fb017d2b 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java @@ -1,7 +1,7 @@ package dev.dubhe.anvilcraft.item; import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.util.BlockHighlightUtil; +import dev.dubhe.anvilcraft.util.IBlockHighlightUtil; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.ClickEvent; @@ -69,7 +69,7 @@ public GeodeItem(Properties properties) { player.sendSystemMessage(Component.translatable( "item.anvilcraft.geode.find", component )); - BlockHighlightUtil.highlightBlock(level, offsetPos); + IBlockHighlightUtil.highlightBlock(level, offsetPos); break block; } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/AbstractCauldronBlockMixin.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/AbstractCauldronBlockMixin.java index 247067663..c779d0ab3 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/AbstractCauldronBlockMixin.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/mixin/AbstractCauldronBlockMixin.java @@ -2,7 +2,6 @@ import dev.dubhe.anvilcraft.init.ModBlocks; -import dev.dubhe.anvilcraft.util.IBucketPickupInjector; import net.minecraft.core.BlockPos; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundEvents; @@ -11,6 +10,7 @@ import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.block.AbstractCauldronBlock; import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.BucketPickup; import net.minecraft.world.level.block.LayeredCauldronBlock; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; @@ -19,7 +19,7 @@ import java.util.Optional; @Mixin(AbstractCauldronBlock.class) -abstract class AbstractCauldronBlockMixin implements IBucketPickupInjector { +public abstract class AbstractCauldronBlockMixin implements BucketPickup { @Override public @NotNull ItemStack pickupBlock( @NotNull LevelAccessor level, @NotNull BlockPos pos, @NotNull BlockState state diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java index 851b1f1db..9cadc0df0 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/mixin/BeaconMenuMixin.java @@ -24,7 +24,7 @@ import java.util.Optional; @Mixin(BeaconMenu.class) -public class BeaconMenuMixin { +public abstract class BeaconMenuMixin { @Shadow @Final private BeaconMenu.PaymentSlot paymentSlot; diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/BlockStateInjector.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/BlockStateInjector.java deleted file mode 100644 index ae1296283..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/BlockStateInjector.java +++ /dev/null @@ -1,40 +0,0 @@ -package dev.dubhe.anvilcraft.mixin; - -import com.google.common.collect.ImmutableMap; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.mojang.serialization.MapCodec; -import dev.dubhe.anvilcraft.util.IBlockStateInjector; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.state.BlockBehaviour; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.block.state.properties.Property; -import org.spongepowered.asm.mixin.Mixin; - -import java.util.stream.Collectors; - -@Mixin(BlockState.class) -abstract class BlockStateInjector extends BlockBehaviour.BlockStateBase implements IBlockStateInjector { - protected BlockStateInjector( - Block owner, ImmutableMap, Comparable> values, MapCodec propertiesCodec - ) { - super(owner, values, propertiesCodec); - } - - @Override - public JsonElement anvilcraft$toJson() { - JsonObject object = new JsonObject(); - object.addProperty("block", BuiltInRegistries.BLOCK.getKey(this.getBlock()).toString()); - if (!this.getValues().isEmpty()) { - String stringBuilder = '[' - + this.getValues() - .entrySet() - .stream() - .map(PROPERTY_ENTRY_TO_STRING_FUNCTION).collect(Collectors.joining(",")) - + ']'; - object.addProperty("state", stringBuilder); - } - return object; - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/ItemStackInjector.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/ItemStackInjector.java deleted file mode 100644 index 2ad406886..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/ItemStackInjector.java +++ /dev/null @@ -1,42 +0,0 @@ -package dev.dubhe.anvilcraft.mixin; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import dev.dubhe.anvilcraft.util.IItemStackInjector; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(ItemStack.class) -abstract class ItemStackInjector implements IItemStackInjector { - @Shadow - public abstract boolean hasTag(); - - @Shadow - public abstract @Nullable CompoundTag getTag(); - - @Shadow - public abstract int getCount(); - - @Shadow - public abstract void setTag(@Nullable CompoundTag compoundTag); - - @Shadow - public abstract CompoundTag getOrCreateTag(); - - @Shadow - public abstract Item getItem(); - - @Override - public JsonElement anvilcraft$toJson() { - JsonObject object = new JsonObject(); - object.addProperty("item", BuiltInRegistries.ITEM.getKey(this.getItem()).toString()); - if (this.getCount() > 1) object.addProperty("count", this.getCount()); - if (this.hasTag()) object.addProperty("data", this.getOrCreateTag().toString()); - return object; - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/MinecraftServerMixin.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/MinecraftServerMixin.java deleted file mode 100644 index 6e5823d7c..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/MinecraftServerMixin.java +++ /dev/null @@ -1,191 +0,0 @@ -package dev.dubhe.anvilcraft.mixin.event; - -import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerEndTickEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerLevelLoadEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerLevelUnloadEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerStartDataPackReloadEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerStartTickEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerStartedEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerStartingEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerStoppedEvent; -import dev.dubhe.anvilcraft.api.event.server.ServerStoppingEvent; -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceKey; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.server.level.progress.ChunkProgressListener; -import net.minecraft.world.RandomSequences; -import net.minecraft.world.level.CustomSpawner; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.border.WorldBorder; -import net.minecraft.world.level.dimension.LevelStem; -import net.minecraft.world.level.levelgen.WorldOptions; -import net.minecraft.world.level.storage.DerivedLevelData; -import net.minecraft.world.level.storage.DimensionDataStorage; -import net.minecraft.world.level.storage.ServerLevelData; -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.function.BooleanSupplier; - -@Mixin(value = MinecraftServer.class) -abstract class MinecraftServerMixin { - @Shadow - private MinecraftServer.ReloadableResources resources; - - @Unique - private final MinecraftServer server = (MinecraftServer) (Object) this; - - @Inject( - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/server/MinecraftServer;initServer()Z" - ), - method = "runServer" - ) - private void beforeSetupServer(CallbackInfo info) { - AnvilCraft.EVENT_BUS.post(new ServerStartingEvent(server)); - } - - @Inject( - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/server/MinecraftServer;buildServerStatus()" - + "Lnet/minecraft/network/protocol/status/ServerStatus;" - ), - method = "runServer" - ) - private void afterSetupServer(CallbackInfo info) { - AnvilCraft.EVENT_BUS.post(new ServerStartedEvent(server)); - } - - @Inject( - at = @At("HEAD"), method = "stopServer") - private void beforeShutdownServer(CallbackInfo info) { - AnvilCraft.EVENT_BUS.post(new ServerStoppingEvent(server)); - } - - @Inject( - at = @At("TAIL"), method = "stopServer") - private void afterShutdownServer(CallbackInfo info) { - AnvilCraft.EVENT_BUS.post(new ServerStoppedEvent(server)); - } - - @Inject( - method = "tickServer", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/server/MinecraftServer;tickChildren(Ljava/util/function/BooleanSupplier;)V" - ) - ) - private void onStartTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { - AnvilCraft.EVENT_BUS.post(new ServerStartTickEvent(server)); - } - - @Inject( - at = @At("TAIL"), method = "tickServer") - private void onEndTick(BooleanSupplier shouldKeepTicking, CallbackInfo info) { - AnvilCraft.EVENT_BUS.post(new ServerEndTickEvent(server)); - } - - @Inject( - method = "createLevels", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/server/level/ServerLevel;getDataStorage()" - + "Lnet/minecraft/world/level/storage/DimensionDataStorage;" - ), - locals = LocalCapture.CAPTURE_FAILHARD) - private void onLoadLevel( - ChunkProgressListener listener, - CallbackInfo ci, - ServerLevelData serverLevelData, - boolean bl, - Registry registry, - WorldOptions worldOptions, - long l, - long m, - List list, - LevelStem levelStem, - ServerLevel serverLevel - ) { - AnvilCraft.EVENT_BUS.post(new ServerLevelLoadEvent(server, serverLevel)); - } - - @Inject( - method = "createLevels", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/level/border/WorldBorder;" - + "addListener(Lnet/minecraft/world/level/border/BorderChangeListener;)V" - ), - locals = LocalCapture.CAPTURE_FAILHARD) - private void onLoadLevel( - ChunkProgressListener listener, - CallbackInfo ci, - ServerLevelData serverLevelData, - boolean bl, - Registry registry, - WorldOptions worldOptions, - long l, long m, - List list, - LevelStem levelStem, - ServerLevel serverLevel, - DimensionDataStorage dimensionDataStorage, - WorldBorder worldBorder, - RandomSequences randomSequences, - Iterator, LevelStem>> var16, - Map.Entry, LevelStem> entry, - ResourceKey resourceKey, - ResourceKey resourceKey2, - DerivedLevelData derivedLevelData, - ServerLevel serverLevel2 - ) { - AnvilCraft.EVENT_BUS.post(new ServerLevelLoadEvent(server, serverLevel2)); - } - - @Inject( - method = "stopServer", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/server/level/ServerLevel;close()V" - ), - locals = LocalCapture.CAPTURE_FAILEXCEPTION - ) - private void onUnloadLevelAtShutdown(CallbackInfo ci, Iterator levels, ServerLevel level) { - AnvilCraft.EVENT_BUS.post(new ServerLevelUnloadEvent(server, level)); - } - - @Inject( - method = "reloadResources", at = @At("HEAD")) - private void startResourceReload( - Collection collection, CallbackInfoReturnable> cir - ) { - AnvilCraft.EVENT_BUS.post(new ServerStartDataPackReloadEvent(server, this.resources.resourceManager())); - } - - @Inject( - method = "reloadResources", at = @At("TAIL")) - private void endResourceReload( - Collection collection, @NotNull CallbackInfoReturnable> cir - ) { - cir.getReturnValue().handleAsync((value, throwable) -> { - AnvilCraft.EVENT_BUS.post(new ServerEndDataPackReloadEvent(server, this.resources.resourceManager())); - return value; - }, (MinecraftServer) (Object) this); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java b/common/src/main/java/dev/dubhe/anvilcraft/util/IBlockHighlightUtil.java similarity index 79% rename from common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java rename to common/src/main/java/dev/dubhe/anvilcraft/util/IBlockHighlightUtil.java index bc0014476..d5c9bda1f 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/IBlockHighlightUtil.java @@ -5,7 +5,7 @@ import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.blaze3d.vertex.VertexFormat; import dev.dubhe.anvilcraft.AnvilCraft; -import lombok.Getter; +import lombok.Data; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Camera; @@ -24,9 +24,12 @@ import java.util.Map; import java.util.OptionalDouble; -public class BlockHighlightUtil { +/** + * 方块高亮 + */ +public interface IBlockHighlightUtil { @Environment(EnvType.CLIENT) - public static final RenderType NO_DEPTH = + RenderType NO_DEPTH = RenderType.create(AnvilCraft.MOD_ID + "_no_depth", DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.LINES, 256, true, true, RenderType.CompositeState.builder() .setShaderState(RenderStateShard.RENDERTYPE_LINES_SHADER) @@ -36,10 +39,9 @@ public class BlockHighlightUtil { .setLayeringState(RenderStateShard.VIEW_OFFSET_Z_LAYERING) .setLineState(new RenderStateShard.LineStateShard(OptionalDouble.of(2))) .createCompositeState(true)); - public static final Map SUBCHUNKS = new HashMap<>(); + Map SUBCHUNKS = new HashMap<>(); - @Getter - private static Level level = null; + LevelData level = new LevelData(); /** * 高亮方块 @@ -47,11 +49,12 @@ public class BlockHighlightUtil { * @param level 维度 * @param pos 位置 */ - public static void highlightBlock(Level level, BlockPos pos) { - if (BlockHighlightUtil.getLevel() != level) { - BlockHighlightUtil.setLevel(level); + static void highlightBlock(Level level, BlockPos pos) { + if (IBlockHighlightUtil.getLevel() != level) { + IBlockHighlightUtil.setLevel(level); SUBCHUNKS.clear(); } + if (level == null) return; SUBCHUNKS.put(new Vector3i(Math.floorDiv(pos.getX(), 16), Math.floorDiv(pos.getY(), 16), Math.floorDiv(pos.getZ(), 16)), level.getGameTime()); } @@ -65,15 +68,15 @@ public static void highlightBlock(Level level, BlockPos pos) { * @param camera 相机 */ @Environment(EnvType.CLIENT) - public static void render( + static void render( Level level, @NotNull MultiBufferSource consumers, @NotNull PoseStack poseStack, @NotNull Camera camera ) { - VertexConsumer consumer = consumers.getBuffer(BlockHighlightUtil.NO_DEPTH); + VertexConsumer consumer = consumers.getBuffer(IBlockHighlightUtil.NO_DEPTH); Vec3 cameraPos = camera.getPosition(); int color = 0xFF8932B8; poseStack.pushPose(); poseStack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z); - for (var iterator = BlockHighlightUtil.SUBCHUNKS.entrySet().iterator(); iterator.hasNext(); ) { + for (var iterator = IBlockHighlightUtil.SUBCHUNKS.entrySet().iterator(); iterator.hasNext(); ) { var entry = iterator.next(); Vector3ic subchunk = entry.getKey(); Long moment = entry.getValue(); @@ -101,7 +104,16 @@ public static void render( poseStack.popPose(); } - public static void setLevel(Level level) { - BlockHighlightUtil.level = level; + static void setLevel(Level level) { + IBlockHighlightUtil.level.setLevel(level); + } + + static Level getLevel() { + return IBlockHighlightUtil.level.getLevel(); + } + + @Data + class LevelData { + Level level; } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/IBlockStateInjector.java b/common/src/main/java/dev/dubhe/anvilcraft/util/IBlockStateUtil.java similarity index 77% rename from common/src/main/java/dev/dubhe/anvilcraft/util/IBlockStateInjector.java rename to common/src/main/java/dev/dubhe/anvilcraft/util/IBlockStateUtil.java index 790b0a483..3db962e54 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/IBlockStateInjector.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/IBlockStateUtil.java @@ -19,12 +19,15 @@ import org.jetbrains.annotations.NotNull; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; +import static net.minecraft.world.level.block.state.StateHolder.PROPERTY_ENTRY_TO_STRING_FUNCTION; + /** * 方块状态注入 */ -public interface IBlockStateInjector { +public interface IBlockStateUtil { /** * 从 Json 读取 * @@ -51,8 +54,25 @@ public interface IBlockStateInjector { return blockResult.blockState(); } - default JsonElement anvilcraft$toJson() { - return new JsonObject(); + /** + * 序列化方块状态 + * + * @param state 方块状态 + * @return 序列化JSON + */ + static @NotNull JsonElement toJson(@NotNull BlockState state) { + JsonObject object = new JsonObject(); + object.addProperty("block", BuiltInRegistries.BLOCK.getKey(state.getBlock()).toString()); + if (!state.getValues().isEmpty()) { + String stringBuilder = '[' + + state.getValues() + .entrySet() + .stream() + .map(PROPERTY_ENTRY_TO_STRING_FUNCTION).collect(Collectors.joining(",")) + + ']'; + object.addProperty("state", stringBuilder); + } + return object; } class BlockHolderLookup implements HolderLookup, HolderOwner { diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/IBucketPickupInjector.java b/common/src/main/java/dev/dubhe/anvilcraft/util/IBucketPickupInjector.java deleted file mode 100644 index aae270d02..000000000 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/IBucketPickupInjector.java +++ /dev/null @@ -1,32 +0,0 @@ -package dev.dubhe.anvilcraft.util; - -import net.minecraft.core.BlockPos; -import net.minecraft.sounds.SoundEvent; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; -import net.minecraft.world.level.LevelAccessor; -import net.minecraft.world.level.block.BucketPickup; -import net.minecraft.world.level.block.state.BlockState; -import org.jetbrains.annotations.NotNull; - -import java.util.Optional; - -/** - * 桶拾取注入 - */ -public interface IBucketPickupInjector extends BucketPickup { - @Override - default @NotNull ItemStack pickupBlock( - @NotNull LevelAccessor level, - @NotNull BlockPos pos, - @NotNull BlockState state - ) { - return Items.BUCKET.getDefaultInstance(); - } - - @Override - default @NotNull Optional getPickupSound() { - return Optional.of(SoundEvents.EMPTY); - } -} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/FormattingUtil.java b/common/src/main/java/dev/dubhe/anvilcraft/util/IFormattingUtil.java similarity index 86% rename from common/src/main/java/dev/dubhe/anvilcraft/util/FormattingUtil.java rename to common/src/main/java/dev/dubhe/anvilcraft/util/IFormattingUtil.java index 31249d218..e9b3ea04f 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/FormattingUtil.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/IFormattingUtil.java @@ -8,15 +8,18 @@ import java.util.Locale; import java.util.stream.Collectors; +/** + * 格式化工具 + */ @SuppressWarnings({"UnnecessaryUnicodeEscape", "unused"}) -public class FormattingUtil { +public interface IFormattingUtil { /** * 与 .to(LOWER_UNDERSCORE, string) 几乎相同,但它也会在单词和数字之间插入下划线。 * * @param string 任何带有 ASCII 字符的字符串。 * @return 全小写的字符串,在单词/数字边界前插入下划线:“maragingSteel300” -> “maraging_steel_300” */ - public static @NotNull String toLowerCaseUnderscore(@NotNull String string) { + static @NotNull String toLowerCaseUnderscore(@NotNull String string) { StringBuilder result = new StringBuilder(); for (int i = 0; i < string.length(); i++) { if (i != 0 && (Character.isUpperCase(string.charAt(i)) || ( @@ -33,14 +36,14 @@ public class FormattingUtil { * @param string 任何带有 ASCII 字符的字符串。 * @return 全小写的字符串,在单词/数字边界前插入下划线:“maragingSteel300” -> “maraging_steel_300” */ - public static @NotNull String toLowerCaseUnder(String string) { + static @NotNull String toLowerCaseUnder(String string) { return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, string); } /** * apple_orange.juice => Apple Orange (Juice) */ - public static String toEnglishName(@NotNull Object internalName) { + static String toEnglishName(@NotNull Object internalName) { return Arrays.stream(internalName.toString().toLowerCase(Locale.ROOT).split("_")) .map(StringUtils::capitalize) .collect(Collectors.joining(" ")); diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/IItemStackInjector.java b/common/src/main/java/dev/dubhe/anvilcraft/util/IItemStackUtil.java similarity index 74% rename from common/src/main/java/dev/dubhe/anvilcraft/util/IItemStackInjector.java rename to common/src/main/java/dev/dubhe/anvilcraft/util/IItemStackUtil.java index 16201fe10..ee768c5dd 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/IItemStackInjector.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/IItemStackUtil.java @@ -4,6 +4,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.TagParser; import net.minecraft.util.GsonHelper; @@ -15,7 +16,7 @@ /** * 物品堆栈注入器 */ -public interface IItemStackInjector { +public interface IItemStackUtil { /** * 从 json 读取 * @@ -45,6 +46,20 @@ public interface IItemStackInjector { return stack; } + /** + * 序列化 {@link ItemStack} + * + * @param stack 物品 + * @return 序列化Json + */ + static @NotNull JsonElement toJson(@NotNull ItemStack stack) { + JsonObject object = new JsonObject(); + object.addProperty("item", BuiltInRegistries.ITEM.getKey(stack.getItem()).toString()); + if (stack.getCount() > 1) object.addProperty("count", stack.getCount()); + if (stack.hasTag()) object.addProperty("data", stack.getOrCreateTag().toString()); + return object; + } + default JsonElement anvilcraft$toJson() { return new JsonObject(); } diff --git a/common/src/main/resources/anvilcraft-common.mixins.json b/common/src/main/resources/anvilcraft-common.mixins.json index a5cc71e1c..9fc891070 100644 --- a/common/src/main/resources/anvilcraft-common.mixins.json +++ b/common/src/main/resources/anvilcraft-common.mixins.json @@ -8,18 +8,13 @@ "AnvilBlockMixin", "AxeItemMixin", "BeaconMenuMixin", - "BlockStateInjector", "BucketItemMixin", "ClientboundUpdateRecipesPacketMixin", "DispenseItemEmptyBottleBehaviorMixin", "DispenseItemEmptyBucketBehaviorMixin", "DispenseItemWaterBottleBehaviorMixin", "ItemEntityMixin", - "ItemStackInjector", - "SolidBucketItemMixin", - "event.FallingBlockEntityMixin", - "event.LightningBoltMixin", - "event.MinecraftServerMixin" + "SolidBucketItemMixin" ], "client": [ ], diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/api/event/fabric/AnvilEvent.java b/fabric/src/main/java/dev/dubhe/anvilcraft/api/event/fabric/AnvilEvent.java new file mode 100644 index 000000000..9bb5becd7 --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/api/event/fabric/AnvilEvent.java @@ -0,0 +1,41 @@ +package dev.dubhe.anvilcraft.api.event.fabric; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.item.FallingBlockEntity; +import net.minecraft.world.level.Level; + +import java.util.Arrays; + +public class AnvilEvent { + public static final Event ON_LAND = EventFactory.createArrayBacked(OnLand.class, + callbacks -> (level, pos, entity, fallDistance) -> { + boolean isAnvilDamage = false; + for (OnLand callback : callbacks) { + if (callback.onLand(level, pos, entity, fallDistance)) isAnvilDamage = true; + } + return isAnvilDamage; + }); + public static Event HURT_ENTITY = EventFactory.createArrayBacked(HurtEntity.class, + callbacks -> (entity, pos, level, hurtedEntity, damage) -> + Arrays.stream(callbacks).forEach(hurtEntity -> hurtEntity.hurt(entity, pos, level, hurtedEntity, damage)) + ); + + /** + * 铁砧落地 + */ + @FunctionalInterface + public interface OnLand { + boolean onLand(Level level, BlockPos pos, FallingBlockEntity entity, float fallDistance); + } + + /** + * 铁砧伤害实体 + */ + @FunctionalInterface + public interface HurtEntity { + void hurt(FallingBlockEntity entity, BlockPos pos, Level level, Entity hurtedEntity, float damage); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/api/event/fabric/LightningBoltEvent.java b/fabric/src/main/java/dev/dubhe/anvilcraft/api/event/fabric/LightningBoltEvent.java new file mode 100644 index 000000000..421f355af --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/api/event/fabric/LightningBoltEvent.java @@ -0,0 +1,27 @@ +package dev.dubhe.anvilcraft.api.event.fabric; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.LightningBolt; +import net.minecraft.world.level.Level; + +/** + * 闪电事件 + */ +public class LightningBoltEvent { + public static final Event LIGHTNING_STRIKE = EventFactory.createArrayBacked(LightningStrike.class, + callbacks -> ((level, pos, entity) -> { + for (LightningStrike callback : callbacks) { + callback.strike(level, pos, entity); + } + })); + + /** + * 雷击 + */ + @FunctionalInterface + public interface LightningStrike { + void strike(Level level, BlockPos pos, LightningBolt entity); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java b/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java index ecb137dee..cf1efa361 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java @@ -1,7 +1,7 @@ package dev.dubhe.anvilcraft.client.fabric; import dev.dubhe.anvilcraft.init.ModBlocks; -import dev.dubhe.anvilcraft.util.BlockHighlightUtil; +import dev.dubhe.anvilcraft.util.IBlockHighlightUtil; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; @@ -15,10 +15,10 @@ public void onInitializeClient() { BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.RESIN_BLOCK.get(), RenderType.translucent()); BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.AMBER_BLOCK.get(), RenderType.translucent()); WorldRenderEvents.AFTER_ENTITIES.register(context -> { - if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return; + if (IBlockHighlightUtil.SUBCHUNKS.isEmpty()) return; MultiBufferSource consumers = context.consumers(); if (consumers == null) return; - BlockHighlightUtil.render(context.world(), consumers, context.matrixStack(), context.camera()); + IBlockHighlightUtil.render(context.world(), consumers, context.matrixStack(), context.camera()); }); } } diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/AnvilEntityEvent.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/AnvilEntityEvent.java new file mode 100644 index 000000000..0a8b4f78c --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/AnvilEntityEvent.java @@ -0,0 +1,30 @@ +package dev.dubhe.anvilcraft.event.fabric; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.entity.AnvilFallOnLandEvent; +import dev.dubhe.anvilcraft.api.event.entity.AnvilHurtEntityEvent; +import dev.dubhe.anvilcraft.api.event.fabric.AnvilEvent; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.item.FallingBlockEntity; +import net.minecraft.world.level.Level; + +public class AnvilEntityEvent { + /** + * 初始化 + */ + public static void init() { + AnvilEvent.ON_LAND.register(AnvilEntityEvent::onLand); + AnvilEvent.HURT_ENTITY.register(AnvilEntityEvent::hurt); + } + + private static boolean onLand(Level level, BlockPos pos, FallingBlockEntity entity, float fallDistance) { + AnvilFallOnLandEvent event = new AnvilFallOnLandEvent(level, pos, entity, fallDistance); + AnvilCraft.EVENT_BUS.post(event); + return event.isAnvilDamage(); + } + + private static void hurt(FallingBlockEntity entity, BlockPos pos, Level level, Entity hurtedEntity, float damage) { + AnvilCraft.EVENT_BUS.post(new AnvilHurtEntityEvent(entity, pos, level, hurtedEntity, damage)); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/listener/fabric/AnvilHammerListener.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/BlockEvent.java similarity index 70% rename from fabric/src/main/java/dev/dubhe/anvilcraft/listener/fabric/AnvilHammerListener.java rename to fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/BlockEvent.java index 4e85630f1..c97c2dc67 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/listener/fabric/AnvilHammerListener.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/BlockEvent.java @@ -1,30 +1,29 @@ -package dev.dubhe.anvilcraft.listener.fabric; +package dev.dubhe.anvilcraft.event.fabric; import dev.dubhe.anvilcraft.item.AnvilHammerItem; import net.fabricmc.fabric.api.event.player.AttackBlockCallback; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; -public class AnvilHammerListener { +public class BlockEvent { + /** + * 初始化 + */ + public static void init() { + AttackBlockCallback.EVENT.register(BlockEvent::anvilHammer); + } + private static InteractionResult anvilHammer( - @NotNull Player player, Level level, InteractionHand hand, BlockPos blockPos + @NotNull Player player, Level level, InteractionHand hand, BlockPos blockPos, Direction direction ) { if (player.getItemInHand(hand).getItem() instanceof AnvilHammerItem anvilHammer) { anvilHammer.useAttackBlock(player, blockPos, level); } return InteractionResult.PASS; } - - /** - * 注册事件 - */ - public static void register() { - AttackBlockCallback.EVENT.register( - (player, level, hand, pos, direction) -> anvilHammer(player, level, hand, pos) - ); - } } diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/CommandEvent.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/CommandEvent.java new file mode 100644 index 000000000..ef6896688 --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/CommandEvent.java @@ -0,0 +1,15 @@ +package dev.dubhe.anvilcraft.event.fabric; + +import dev.dubhe.anvilcraft.init.ModCommands; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; + +public class CommandEvent { + /** + * 初始化 + */ + public static void init() { + CommandRegistrationCallback.EVENT.register( + (dispatcher, registryAccess, environment) -> ModCommands.register(dispatcher) + ); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/LightningEvent.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/LightningEvent.java new file mode 100644 index 000000000..1e246e208 --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/LightningEvent.java @@ -0,0 +1,21 @@ +package dev.dubhe.anvilcraft.event.fabric; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.entity.LightningStrikeEvent; +import dev.dubhe.anvilcraft.api.event.fabric.LightningBoltEvent; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.LightningBolt; +import net.minecraft.world.level.Level; + +public class LightningEvent { + /** + * 初始化 + */ + public static void init() { + LightningBoltEvent.LIGHTNING_STRIKE.register(LightningEvent::onLightningStrike); + } + + public static void onLightningStrike(Level level, BlockPos pos, LightningBolt entity) { + AnvilCraft.EVENT_BUS.post(new LightningStrikeEvent(entity, pos, level)); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/listener/fabric/LootTableListener.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/LootTableEvent.java similarity index 67% rename from fabric/src/main/java/dev/dubhe/anvilcraft/listener/fabric/LootTableListener.java rename to fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/LootTableEvent.java index 084c2c876..fe63828af 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/listener/fabric/LootTableListener.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/LootTableEvent.java @@ -1,6 +1,5 @@ -package dev.dubhe.anvilcraft.listener.fabric; +package dev.dubhe.anvilcraft.event.fabric; -import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.init.ModItems; import net.fabricmc.fabric.api.loot.v2.LootTableEvents; import net.minecraft.world.level.block.Blocks; @@ -11,22 +10,22 @@ import net.minecraft.world.level.storage.loot.providers.number.ConstantValue; import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator; -public class LootTableListener { +public class LootTableEvent { /** - * 修改战利品表 + * 初始化 */ - public static void register() { + public static void init() { LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> { if (Blocks.BUDDING_AMETHYST.getLootTable().equals(id)) { tableBuilder.withPool(new LootPool.Builder() - .add(LootItem.lootTableItem(ModItems.GEODE)) - .apply(SetItemCountFunction.setCount(ConstantValue.exactly(1))) + .add(LootItem.lootTableItem(ModItems.GEODE)) + .apply(SetItemCountFunction.setCount(ConstantValue.exactly(1))) ); } if (BuiltInLootTables.SPAWN_BONUS_CHEST.equals(id)) { tableBuilder.withPool(new LootPool.Builder() - .add(LootItem.lootTableItem(ModItems.GEODE)) - .apply(SetItemCountFunction.setCount(UniformGenerator.between(3, 6))) + .add(LootItem.lootTableItem(ModItems.GEODE)) + .apply(SetItemCountFunction.setCount(UniformGenerator.between(3, 6))) ); } }); diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ModFabricEvents.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ModFabricEvents.java new file mode 100644 index 000000000..405ff50f2 --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ModFabricEvents.java @@ -0,0 +1,15 @@ +package dev.dubhe.anvilcraft.event.fabric; + +public class ModFabricEvents { + /** + * 初始化 + */ + public static void init() { + ServerLifecycleEvent.init(); + BlockEvent.init(); + LootTableEvent.init(); + LightningEvent.init(); + AnvilEntityEvent.init(); + CommandEvent.init(); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEvent.java b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEvent.java new file mode 100644 index 000000000..c605e78c2 --- /dev/null +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/event/fabric/ServerLifecycleEvent.java @@ -0,0 +1,30 @@ +package dev.dubhe.anvilcraft.event.fabric; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent; +import dev.dubhe.anvilcraft.api.event.server.ServerStartedEvent; +import dev.dubhe.anvilcraft.utils.fabric.ServerHooks; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.packs.resources.CloseableResourceManager; + +public class ServerLifecycleEvent { + /** + * 初始化 + */ + public static void init() { + ServerLifecycleEvents.SERVER_STARTED.register(ServerLifecycleEvent::serverStarted); + ServerLifecycleEvents.END_DATA_PACK_RELOAD.register(ServerLifecycleEvent::endDataPackReload); + } + + public static void serverStarted(MinecraftServer server) { + ServerHooks.setServer(server); + AnvilCraft.EVENT_BUS.post(new ServerStartedEvent(server)); + } + + public static void endDataPackReload( + MinecraftServer server, CloseableResourceManager resourceManager, boolean success + ) { + AnvilCraft.EVENT_BUS.post(new ServerEndDataPackReloadEvent(server, resourceManager)); + } +} diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/fabric/AnvilCraftFabric.java b/fabric/src/main/java/dev/dubhe/anvilcraft/fabric/AnvilCraftFabric.java index dc49a06da..44ffdf0e2 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/fabric/AnvilCraftFabric.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/fabric/AnvilCraftFabric.java @@ -1,24 +1,15 @@ package dev.dubhe.anvilcraft.fabric; import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.init.ModCommands; +import dev.dubhe.anvilcraft.event.fabric.ModFabricEvents; import dev.dubhe.anvilcraft.init.fabric.ModRecipeTypesFabric; -import dev.dubhe.anvilcraft.listener.fabric.AnvilHammerListener; -import dev.dubhe.anvilcraft.listener.fabric.LootTableListener; -import dev.dubhe.anvilcraft.utils.fabric.ServerHooks; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; public class AnvilCraftFabric implements ModInitializer { @Override public void onInitialize() { AnvilCraft.init(); - ServerHooks.init(); ModRecipeTypesFabric.register(); - AnvilHammerListener.register(); - LootTableListener.register(); - CommandRegistrationCallback.EVENT.register( - (dispatcher, registryAccess, environment) -> ModCommands.register(dispatcher) - ); + ModFabricEvents.init(); } } \ No newline at end of file diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/FallingBlockEntityMixin.java b/fabric/src/main/java/dev/dubhe/anvilcraft/mixin/fabric/FallingBlockEntityMixin.java similarity index 85% rename from common/src/main/java/dev/dubhe/anvilcraft/mixin/event/FallingBlockEntityMixin.java rename to fabric/src/main/java/dev/dubhe/anvilcraft/mixin/fabric/FallingBlockEntityMixin.java index 72702d0ff..7f17f2edd 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/FallingBlockEntityMixin.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/mixin/fabric/FallingBlockEntityMixin.java @@ -1,8 +1,6 @@ -package dev.dubhe.anvilcraft.mixin.event; +package dev.dubhe.anvilcraft.mixin.fabric; -import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.api.event.entity.AnvilFallOnLandEvent; -import dev.dubhe.anvilcraft.api.event.entity.AnvilHurtEntityEvent; +import dev.dubhe.anvilcraft.api.event.fabric.AnvilEvent; import dev.dubhe.anvilcraft.init.ModBlocks; import net.minecraft.core.BlockPos; import net.minecraft.tags.BlockTags; @@ -65,10 +63,9 @@ private void anvilPerFallOnGround(CallbackInfo ci, Block block) { private void anvilFallOnGround(CallbackInfo ci, Block block, BlockPos blockPos) { if (this.level().isClientSide()) return; if (!this.blockState.is(BlockTags.ANVIL)) return; - AnvilFallOnLandEvent event = new AnvilFallOnLandEvent(this.level(), - blockPos, (FallingBlockEntity) (Object) this, this.anvilcraft$fallDistance); - AnvilCraft.EVENT_BUS.post(event); - if (event.isAnvilDamage()) { + boolean isAnvilDamage = AnvilEvent.ON_LAND.invoker() + .onLand(this.level(), blockPos, (FallingBlockEntity) (Object) this, this.anvilcraft$fallDistance); + if (isAnvilDamage) { BlockState state = this.blockState.is(ModBlocks.ROYAL_ANVIL.get()) ? this.blockState : AnvilBlock.damage(this.blockState); @@ -92,8 +89,7 @@ private static boolean anvilHurtEntity(@NotNull Entity instance, DamageSource so boolean bl = instance.hurt(source, amount); Entity directEntity = source.getDirectEntity(); if (!bl || !(directEntity instanceof FallingBlockEntity entity)) return bl; - AnvilCraft.EVENT_BUS.post(new AnvilHurtEntityEvent(entity, entity.getOnPos(), - entity.level(), instance, amount)); + AnvilEvent.HURT_ENTITY.invoker().hurt(entity, entity.getOnPos(), entity.level(), instance, amount); return true; } } diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/LightningBoltMixin.java b/fabric/src/main/java/dev/dubhe/anvilcraft/mixin/fabric/LightningBoltMixin.java similarity index 58% rename from common/src/main/java/dev/dubhe/anvilcraft/mixin/event/LightningBoltMixin.java rename to fabric/src/main/java/dev/dubhe/anvilcraft/mixin/fabric/LightningBoltMixin.java index 5f7b35f07..2f422a894 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/event/LightningBoltMixin.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/mixin/fabric/LightningBoltMixin.java @@ -1,12 +1,10 @@ -package dev.dubhe.anvilcraft.mixin.event; +package dev.dubhe.anvilcraft.mixin.fabric; -import dev.dubhe.anvilcraft.AnvilCraft; -import dev.dubhe.anvilcraft.api.event.entity.LightningStrikeEvent; +import dev.dubhe.anvilcraft.api.event.fabric.LightningBoltEvent; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.LightningBolt; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -16,12 +14,10 @@ abstract class LightningBoltMixin { @Shadow protected abstract BlockPos getStrikePosition(); - @Unique - private final LightningBolt ths = (LightningBolt) (Object) this; - + @SuppressWarnings("UnreachableCode") @Inject(method = "powerLightningRod", at = @At("HEAD")) private void powerLightningRod(CallbackInfo ci) { - LightningStrikeEvent event = new LightningStrikeEvent(ths, this.getStrikePosition(), ths.level()); - AnvilCraft.EVENT_BUS.post(event); + LightningBolt bolt = (LightningBolt) (Object) this; + LightningBoltEvent.LIGHTNING_STRIKE.invoker().strike(bolt.level(), this.getStrikePosition(), bolt); } } diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/utils/fabric/ServerHooks.java b/fabric/src/main/java/dev/dubhe/anvilcraft/utils/fabric/ServerHooks.java index de22c8bf0..687fb3e89 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/utils/fabric/ServerHooks.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/utils/fabric/ServerHooks.java @@ -1,14 +1,11 @@ package dev.dubhe.anvilcraft.utils.fabric; import lombok.Getter; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import lombok.Setter; import net.minecraft.server.MinecraftServer; public class ServerHooks { + @Setter @Getter private static MinecraftServer server = null; - - public static void init() { - ServerLifecycleEvents.SERVER_STARTED.register(server -> ServerHooks.server = server); - } } diff --git a/fabric/src/main/resources/anvilcraft.mixins.json b/fabric/src/main/resources/anvilcraft.mixins.json index 54e079551..60a2f7dea 100644 --- a/fabric/src/main/resources/anvilcraft.mixins.json +++ b/fabric/src/main/resources/anvilcraft.mixins.json @@ -4,6 +4,8 @@ "package": "dev.dubhe.anvilcraft.mixin.fabric", "compatibilityLevel": "JAVA_17", "mixins": [ + "FallingBlockEntityMixin", + "LightningBoltMixin", "ShapedRecipeMixin" ], "client": [ diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index 8f94f49d6..32be2b56a 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -40,18 +40,5 @@ "fabricloader": ">=0.14.7", "fabric": "*", "minecraft": ">=${minecraft_version}" - }, - "custom": { - "loom:injected_interfaces": { - "net/minecraft/class_1799": [ - "dev/dubhe/anvilcraft/util/IItemStackInjector" - ], - "net/minecraft/class_2680": [ - "dev/dubhe/anvilcraft/util/IBlockStateInjector" - ], - "net/minecraft/class_2275": [ - "dev/dubhe/anvilcraft/util/IBucketPickupInjector" - ] - } } } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/AnvilEvent.java b/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/AnvilEvent.java new file mode 100644 index 000000000..25049d68a --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/AnvilEvent.java @@ -0,0 +1,68 @@ +package dev.dubhe.anvilcraft.api.event.forge; + +import lombok.Getter; +import lombok.Setter; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.item.FallingBlockEntity; +import net.minecraft.world.level.Level; +import net.minecraftforge.event.entity.EntityEvent; + +@Getter +public class AnvilEvent extends EntityEvent { + private final FallingBlockEntity entity; + + public AnvilEvent(FallingBlockEntity entity) { + super(entity); + this.entity = entity; + } + + @Getter + public static class OnLand extends AnvilEvent { + private final Level level; + private final BlockPos pos; + private final float fallDistance; + @Setter + private boolean isAnvilDamage = false; + + /** + * 铁砧落地事件 + * + * @param level 世界 + * @param pos 位置 + * @param entity 铁砧 + * @param fallDistance 下落距离 + */ + public OnLand(Level level, BlockPos pos, FallingBlockEntity entity, float fallDistance) { + super(entity); + this.level = level; + this.pos = pos; + this.fallDistance = fallDistance; + } + } + + @Getter + public static class HurtEntity extends AnvilEvent { + private final BlockPos pos; + private final Level level; + private final Entity hurtedEntity; + private final float damage; + + /** + * 铁砧伤害实体 + * + * @param entity 铁砧 + * @param pos 位置 + * @param level 世界 + * @param hurtedEntity 被伤害的实体 + * @param damage 伤害 + */ + public HurtEntity(FallingBlockEntity entity, BlockPos pos, Level level, Entity hurtedEntity, float damage) { + super(entity); + this.pos = pos; + this.level = level; + this.hurtedEntity = hurtedEntity; + this.damage = damage; + } + } +} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartDataPackReloadEvent.java b/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/DataPackReloadedEvent.java similarity index 51% rename from common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartDataPackReloadEvent.java rename to forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/DataPackReloadedEvent.java index 16e680756..cabefc9ed 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/event/server/ServerStartDataPackReloadEvent.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/DataPackReloadedEvent.java @@ -1,14 +1,15 @@ -package dev.dubhe.anvilcraft.api.event.server; +package dev.dubhe.anvilcraft.api.event.forge; import lombok.Getter; import net.minecraft.server.MinecraftServer; import net.minecraft.server.packs.resources.CloseableResourceManager; +import net.minecraftforge.event.server.ServerLifecycleEvent; @Getter -public class ServerStartDataPackReloadEvent extends ServerEvent { +public class DataPackReloadedEvent extends ServerLifecycleEvent { private final CloseableResourceManager resourceManager; - public ServerStartDataPackReloadEvent(MinecraftServer server, CloseableResourceManager resourceManager) { + public DataPackReloadedEvent(MinecraftServer server, CloseableResourceManager resourceManager) { super(server); this.resourceManager = resourceManager; } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/LightningBoltStrikeEvent.java b/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/LightningBoltStrikeEvent.java new file mode 100644 index 000000000..c12509844 --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/api/event/forge/LightningBoltStrikeEvent.java @@ -0,0 +1,28 @@ +package dev.dubhe.anvilcraft.api.event.forge; + +import lombok.Getter; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.LightningBolt; +import net.minecraft.world.level.Level; +import net.minecraftforge.event.entity.EntityEvent; + +@Getter +public class LightningBoltStrikeEvent extends EntityEvent { + private final Level level; + private final BlockPos pos; + private final LightningBolt entity; + + /** + * 雷击事件 + * + * @param entity 闪电 + * @param level 世界 + * @param pos 位置 + */ + public LightningBoltStrikeEvent(LightningBolt entity, Level level, BlockPos pos) { + super(entity); + this.level = level; + this.pos = pos; + this.entity = entity; + } +} diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/AnvilEntityEvent.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/AnvilEntityEvent.java new file mode 100644 index 000000000..d656c7ee4 --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/AnvilEntityEvent.java @@ -0,0 +1,34 @@ +package dev.dubhe.anvilcraft.event.forge; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.entity.AnvilFallOnLandEvent; +import dev.dubhe.anvilcraft.api.event.entity.AnvilHurtEntityEvent; +import dev.dubhe.anvilcraft.api.event.forge.AnvilEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import org.jetbrains.annotations.NotNull; + +@Mod.EventBusSubscriber(modid = AnvilCraft.MOD_ID) +public class AnvilEntityEvent { + /** + * @param e 铁砧落地事件 + */ + @SubscribeEvent + public static void onLand(@NotNull AnvilEvent.OnLand e) { + AnvilFallOnLandEvent event = new AnvilFallOnLandEvent( + e.getLevel(), e.getPos(), e.getEntity(), e.getFallDistance() + ); + AnvilCraft.EVENT_BUS.post(event); + e.setAnvilDamage(event.isAnvilDamage()); + } + + /** + * @param e 铁砧伤害实体事件 + */ + @SubscribeEvent + public static void hurt(AnvilEvent.@NotNull HurtEntity e) { + AnvilCraft.EVENT_BUS.post( + new AnvilHurtEntityEvent(e.getEntity(), e.getPos(), e.getLevel(), e.getHurtedEntity(), e.getDamage()) + ); + } +} diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/listener/AnvilHammerListener.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/BlockEvent.java similarity index 92% rename from forge/src/main/java/dev/dubhe/anvilcraft/listener/AnvilHammerListener.java rename to forge/src/main/java/dev/dubhe/anvilcraft/event/forge/BlockEvent.java index 93d118194..b7972cc97 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/listener/AnvilHammerListener.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/BlockEvent.java @@ -1,4 +1,4 @@ -package dev.dubhe.anvilcraft.listener; +package dev.dubhe.anvilcraft.event.forge; import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.item.AnvilHammerItem; @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull; @Mod.EventBusSubscriber(modid = AnvilCraft.MOD_ID) -public class AnvilHammerListener { +public class BlockEvent { /** * 侦听左键方块事件 * diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LightningEvent.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LightningEvent.java new file mode 100644 index 000000000..e6cf76a10 --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LightningEvent.java @@ -0,0 +1,19 @@ +package dev.dubhe.anvilcraft.event.forge; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.entity.LightningStrikeEvent; +import dev.dubhe.anvilcraft.api.event.forge.LightningBoltStrikeEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import org.jetbrains.annotations.NotNull; + +@Mod.EventBusSubscriber(modid = AnvilCraft.MOD_ID) +public class LightningEvent { + /** + * @param event 雷击事件 + */ + @SubscribeEvent + public static void onLightningStrike(@NotNull LightningBoltStrikeEvent event) { + AnvilCraft.EVENT_BUS.post(new LightningStrikeEvent(event.getEntity(), event.getPos(), event.getLevel())); + } +} diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/listener/LootTableListener.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LootTableEvent.java similarity index 96% rename from forge/src/main/java/dev/dubhe/anvilcraft/listener/LootTableListener.java rename to forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LootTableEvent.java index 9d422f18d..ecdb0aec4 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/listener/LootTableListener.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/LootTableEvent.java @@ -1,4 +1,4 @@ -package dev.dubhe.anvilcraft.listener; +package dev.dubhe.anvilcraft.event.forge; import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.init.ModItems; @@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull; @Mod.EventBusSubscriber(modid = AnvilCraft.MOD_ID) -public class LootTableListener { +public class LootTableEvent { /** * 战利品表加载事件侦听器 * diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEvent.java b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEvent.java new file mode 100644 index 000000000..15463056d --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/event/forge/ServerLifecycleEvent.java @@ -0,0 +1,32 @@ +package dev.dubhe.anvilcraft.event.forge; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.forge.DataPackReloadedEvent; +import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.packs.resources.CloseableResourceManager; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import org.jetbrains.annotations.NotNull; + +@Mod.EventBusSubscriber(modid = AnvilCraft.MOD_ID) +public class ServerLifecycleEvent { + /** + * @param event 服务器启动事件 + */ + @SubscribeEvent + public static void serverStarted(@NotNull ServerStartedEvent event) { + AnvilCraft.EVENT_BUS.post(new dev.dubhe.anvilcraft.api.event.server.ServerStartedEvent(event.getServer())); + } + + /** + * @param event 数据包重载事件 + */ + @SubscribeEvent + public void onDataPackReloaded(@NotNull DataPackReloadedEvent event) { + MinecraftServer server = event.getServer(); + CloseableResourceManager resourceManager = event.getResourceManager(); + AnvilCraft.EVENT_BUS.post(new ServerEndDataPackReloadEvent(server, resourceManager)); + } +} diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java b/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java index b42a3bee8..127c19e91 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java @@ -4,7 +4,7 @@ import dev.dubhe.anvilcraft.config.AnvilCraftConfig; import dev.dubhe.anvilcraft.init.ModCommands; import dev.dubhe.anvilcraft.init.forge.ModRecipeTypesForge; -import dev.dubhe.anvilcraft.util.BlockHighlightUtil; +import dev.dubhe.anvilcraft.util.IBlockHighlightUtil; import me.shedaniel.autoconfig.AutoConfig; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; @@ -31,10 +31,10 @@ public AnvilCraftForge() { MinecraftForge.EVENT_BUS.addListener(AnvilCraftForge::registerCommand); MinecraftForge.EVENT_BUS.addListener((RenderLevelStageEvent event) -> { if (event.getStage() != RenderLevelStageEvent.Stage.AFTER_ENTITIES) return; - if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return; + if (IBlockHighlightUtil.SUBCHUNKS.isEmpty()) return; ClientLevel level = Minecraft.getInstance().level; if (level == null) return; - BlockHighlightUtil.render(level, Minecraft.getInstance().renderBuffers().bufferSource(), + IBlockHighlightUtil.render(level, Minecraft.getInstance().renderBuffers().bufferSource(), event.getPoseStack(), event.getCamera()); }); diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/FallingBlockEntityMixin.java b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/FallingBlockEntityMixin.java new file mode 100644 index 000000000..6ee75f30d --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/FallingBlockEntityMixin.java @@ -0,0 +1,102 @@ +package dev.dubhe.anvilcraft.mixin.forge; + +import dev.dubhe.anvilcraft.AnvilCraft; +import dev.dubhe.anvilcraft.api.event.entity.AnvilFallOnLandEvent; +import dev.dubhe.anvilcraft.api.event.entity.AnvilHurtEntityEvent; +import dev.dubhe.anvilcraft.api.event.forge.AnvilEvent; +import dev.dubhe.anvilcraft.init.ModBlocks; +import net.minecraft.core.BlockPos; +import net.minecraft.tags.BlockTags; +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.item.FallingBlockEntity; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.AnvilBlock; +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 org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Mixin(FallingBlockEntity.class) +abstract class FallingBlockEntityMixin extends Entity { + @Shadow + private BlockState blockState; + @Shadow + private boolean cancelDrop; + @Unique + private float anvilcraft$fallDistance; + + public FallingBlockEntityMixin(EntityType entityType, Level level) { + super(entityType, level); + } + + @Inject( + method = "tick", + at = @At( + value = "INVOKE", + ordinal = 0, + target = "Lnet/minecraft/world/entity/item/FallingBlockEntity;level()Lnet/minecraft/world/level/Level;" + ), + locals = LocalCapture.CAPTURE_FAILHARD) + private void anvilPerFallOnGround(CallbackInfo ci, Block block) { + if (this.level().isClientSide()) return; + if (this.onGround()) return; + this.anvilcraft$fallDistance = this.fallDistance; + } + + @SuppressWarnings("UnreachableCode") + @Inject( + method = "tick", + at = @At( + value = "INVOKE", + ordinal = 10, + target = "Lnet/minecraft/world/entity/item/FallingBlockEntity;level()Lnet/minecraft/world/level/Level;" + ), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void anvilFallOnGround(CallbackInfo ci, Block block, BlockPos blockPos) { + if (this.level().isClientSide()) return; + if (!this.blockState.is(BlockTags.ANVIL)) return; + FallingBlockEntity entity = (FallingBlockEntity) (Object) this; + AnvilEvent.OnLand event = new AnvilEvent.OnLand(this.level(), blockPos, entity, this.anvilcraft$fallDistance); + MinecraftForge.EVENT_BUS.post(event, (listener, event1) -> { + if (event1 instanceof AnvilEvent.OnLand onLand && onLand.isAnvilDamage()) { + BlockState state = this.blockState.is(ModBlocks.ROYAL_ANVIL.get()) + ? this.blockState + : AnvilBlock.damage(this.blockState); + if (state != null) this.level().setBlockAndUpdate(blockPos, state); + else { + this.level().setBlockAndUpdate(blockPos, Blocks.AIR.defaultBlockState()); + if (!this.isSilent()) this.level().levelEvent(1029, this.getOnPos(), 0); + this.cancelDrop = true; + } + } + }); + } + + @Redirect( + method = "method_32879", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z" + ) + ) + private static boolean anvilHurtEntity(@NotNull Entity instance, DamageSource source, float amount) { + boolean bl = instance.hurt(source, amount); + Entity directEntity = source.getDirectEntity(); + if (!bl || !(directEntity instanceof FallingBlockEntity entity)) return bl; + AnvilEvent event = new AnvilEvent.HurtEntity(entity, entity.getOnPos(), entity.level(), instance, amount); + MinecraftForge.EVENT_BUS.post(event); + return true; + } +} diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/LightningBoltMixin.java b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/LightningBoltMixin.java new file mode 100644 index 000000000..66389a65b --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/LightningBoltMixin.java @@ -0,0 +1,24 @@ +package dev.dubhe.anvilcraft.mixin.forge; + +import dev.dubhe.anvilcraft.api.event.forge.LightningBoltStrikeEvent; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.LightningBolt; +import net.minecraftforge.common.MinecraftForge; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(LightningBolt.class) +abstract class LightningBoltMixin { + @Shadow + protected abstract BlockPos getStrikePosition(); + + @SuppressWarnings("UnreachableCode") + @Inject(method = "powerLightningRod", at = @At("HEAD")) + private void powerLightningRod(CallbackInfo ci) { + LightningBolt bolt = (LightningBolt) (Object) this; + MinecraftForge.EVENT_BUS.post(new LightningBoltStrikeEvent(bolt, bolt.level(), this.getStrikePosition())); + } +} diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/MinecraftServerMixin.java b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/MinecraftServerMixin.java new file mode 100644 index 000000000..0d7ae6715 --- /dev/null +++ b/forge/src/main/java/dev/dubhe/anvilcraft/mixin/forge/MinecraftServerMixin.java @@ -0,0 +1,35 @@ +package dev.dubhe.anvilcraft.mixin.forge; + +import dev.dubhe.anvilcraft.api.event.forge.DataPackReloadedEvent; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.packs.resources.CloseableResourceManager; +import net.minecraftforge.common.MinecraftForge; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.Collection; +import java.util.concurrent.CompletableFuture; + +@Mixin(value = MinecraftServer.class) +public abstract class MinecraftServerMixin { + @Shadow + private MinecraftServer.ReloadableResources resources; + + @SuppressWarnings("UnreachableCode") + @Inject( + method = "reloadResources", at = @At("TAIL")) + private void endResourceReload( + Collection collection, @NotNull CallbackInfoReturnable> cir + ) { + MinecraftServer server = (MinecraftServer) (Object) this; + CloseableResourceManager resourceManager = this.resources.resourceManager(); + cir.getReturnValue().handleAsync((value, throwable) -> { + MinecraftForge.EVENT_BUS.post(new DataPackReloadedEvent(server, resourceManager)); + return value; + }, server); + } +} diff --git a/forge/src/main/resources/anvilcraft.mixins.json b/forge/src/main/resources/anvilcraft.mixins.json index 9684991ee..4269c9a1c 100644 --- a/forge/src/main/resources/anvilcraft.mixins.json +++ b/forge/src/main/resources/anvilcraft.mixins.json @@ -1,9 +1,12 @@ { "required": true, "minVersion": "0.8", - "package": "dev.dubhe.anvilcraft.forge.mixin", + "package": "dev.dubhe.anvilcraft.mixin.forge", "compatibilityLevel": "JAVA_17", "mixins": [ + "FallingBlockEntityMixin", + "LightningBoltMixin", + "MinecraftServerMixin" ], "client": [ ],