From 8029532678b12df87cc8a1194a5ff473cc0c5b50 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sun, 13 Jul 2025 22:53:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B9=B8=E8=BF=90=E9=9B=86?= =?UTF-8?q?=E8=A3=85=E7=AE=B1=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/LuckyContainerBlock.java | 5 ++ .../entity/LuckyContainerBlockEntity.java | 39 +++++++++++++ .../data/container/ContainerDataManager.java | 56 +++++++++++++++++++ .../containers/all_vehicles.json | 6 ++ 4 files changed, 106 insertions(+) create mode 100644 src/main/java/com/atsuishio/superbwarfare/data/container/ContainerDataManager.java create mode 100644 src/main/resources/data/superbwarfare/containers/all_vehicles.json diff --git a/src/main/java/com/atsuishio/superbwarfare/block/LuckyContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/LuckyContainerBlock.java index 79707b463..ce141f21f 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/LuckyContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/LuckyContainerBlock.java @@ -2,11 +2,13 @@ package com.atsuishio.superbwarfare.block; import com.atsuishio.superbwarfare.block.entity.LuckyContainerBlockEntity; import com.atsuishio.superbwarfare.init.ModBlockEntities; +import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModTags; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; +import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.ItemInteractionResult; import net.minecraft.world.entity.player.Player; @@ -60,6 +62,9 @@ public class LuckyContainerBlock extends BaseEntityBlock { return ItemInteractionResult.FAIL; } + level.setBlockAndUpdate(pos, state.setValue(OPENED, true)); + level.playSound(null, BlockPos.containing(pos.getX(), pos.getY(), pos.getZ()), ModSounds.OPEN.get(), SoundSource.BLOCKS, 1, 1); + return ItemInteractionResult.FAIL; } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java index 60c533aa9..96d3cc076 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java @@ -1,6 +1,7 @@ package com.atsuishio.superbwarfare.block.entity; import com.atsuishio.superbwarfare.block.LuckyContainerBlock; +import com.atsuishio.superbwarfare.data.container.ContainerDataManager; import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.tools.ParticleTool; import net.minecraft.core.BlockPos; @@ -8,9 +9,11 @@ import net.minecraft.core.HolderLookup; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; @@ -18,15 +21,19 @@ import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; +import org.joml.Math; import software.bernie.geckolib.animatable.GeoBlockEntity; import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache; import software.bernie.geckolib.animation.*; import software.bernie.geckolib.util.GeckoLibUtil; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEntity { + @Nullable + public ResourceLocation location; public int tick = 0; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -50,11 +57,34 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn } } else { var direction = pState.getValue(LuckyContainerBlock.FACING); + var type = blockEntity.unpackEntities(); + + if (type != null) { + var entity = type.create(pLevel); + if (entity != null) { + entity.setPos(pPos.getX() + 0.5 + (2 * Math.random() - 1) * 0.1f, pPos.getY() + 0.5 + (2 * Math.random() - 1) * 0.1f, pPos.getZ() + 0.5 + (2 * Math.random() - 1) * 0.1f); + entity.setYRot(direction.toYRot()); + pLevel.addFreshEntity(entity); + } + } pLevel.setBlockAndUpdate(pPos, Blocks.AIR.defaultBlockState()); } } + @Nullable + public EntityType unpackEntities() { + if (this.location != null && this.level != null && this.level.getServer() != null) { + ContainerDataManager dataManager = ContainerDataManager.INSTANCE; + var list = dataManager.getEntityTypes(this.location); + if (list.isPresent()) { + int rand = this.level.random.nextInt(list.get().size()); + return EntityType.byString(list.get().get(rand)).orElse(null); + } + } + return null; + } + private PlayState predicate(AnimationState event) { if (this.getBlockState().getValue(LuckyContainerBlock.OPENED)) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.container.open")); @@ -76,6 +106,9 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn @ParametersAreNonnullByDefault protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) { super.loadAdditional(tag, registries); + if (tag.contains("Location", 8)) { + this.location = ResourceLocation.withDefaultNamespace(tag.getString("Location")); + } this.tick = tag.getInt("Tick"); } @@ -83,6 +116,9 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn @ParametersAreNonnullByDefault protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) { super.saveAdditional(tag, registries); + if (this.location != null) { + tag.putString("Location", this.location.toString()); + } tag.putInt("Tick", this.tick); } @@ -100,6 +136,9 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn @ParametersAreNonnullByDefault public void saveToItem(ItemStack stack, HolderLookup.Provider registries) { CompoundTag tag = new CompoundTag(); + if (this.location != null) { + tag.putString("Location", this.location.toString()); + } BlockItem.setBlockEntityData(stack, this.getType(), tag); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/data/container/ContainerDataManager.java b/src/main/java/com/atsuishio/superbwarfare/data/container/ContainerDataManager.java new file mode 100644 index 000000000..b48bb76b0 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/data/container/ContainerDataManager.java @@ -0,0 +1,56 @@ +package com.atsuishio.superbwarfare.data.container; + +import com.atsuishio.superbwarfare.Mod; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.ResourceManager; +import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; +import net.minecraft.util.profiling.ProfilerFiller; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.event.AddReloadListenerEvent; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.*; + +@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME) +public class ContainerDataManager extends SimpleJsonResourceReloadListener { + + public static ContainerDataManager INSTANCE = new ContainerDataManager(); + + private static final Gson GSON = new Gson(); + private static final String DIRECTORY = "containers"; + private final Map> containerData = new HashMap<>(); + + public ContainerDataManager() { + super(GSON, DIRECTORY); + } + + @SubscribeEvent + public static void onAddReloadListeners(AddReloadListenerEvent event) { + INSTANCE = new ContainerDataManager(); + event.addListener(INSTANCE); + } + + @Override + @ParametersAreNonnullByDefault + protected void apply(Map pObject, ResourceManager manager, ProfilerFiller profiler) { + containerData.clear(); + pObject.forEach((id, json) -> { + try { + JsonObject obj = json.getAsJsonObject(); + List list = new ArrayList<>(); + obj.getAsJsonArray("EntityTypes").forEach(e -> list.add(e.getAsString())); + containerData.put(id, list); + } catch (Exception e) { + Mod.LOGGER.error("Failed to load container data for {}", id); + } + }); + } + + public Optional> getEntityTypes(ResourceLocation id) { + return Optional.ofNullable(containerData.get(id)); + } +} diff --git a/src/main/resources/data/superbwarfare/containers/all_vehicles.json b/src/main/resources/data/superbwarfare/containers/all_vehicles.json new file mode 100644 index 000000000..6eb618fa5 --- /dev/null +++ b/src/main/resources/data/superbwarfare/containers/all_vehicles.json @@ -0,0 +1,6 @@ +{ + "EntityTypes": [ + "superbwarfare:wheel_chair", + "superbwarfare:lav_150" + ] +} \ No newline at end of file