添加幸运集装箱的功能
This commit is contained in:
parent
a4a8c57b62
commit
8029532678
4 changed files with 106 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<LuckyContainerBlockEntity> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ResourceLocation, List<String>> 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<ResourceLocation, JsonElement> pObject, ResourceManager manager, ProfilerFiller profiler) {
|
||||
containerData.clear();
|
||||
pObject.forEach((id, json) -> {
|
||||
try {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
List<String> 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<List<String>> getEntityTypes(ResourceLocation id) {
|
||||
return Optional.ofNullable(containerData.get(id));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"EntityTypes": [
|
||||
"superbwarfare:wheel_chair",
|
||||
"superbwarfare:lav_150"
|
||||
]
|
||||
}
|
Loading…
Add table
Reference in a new issue