ReFramed/src/main/java/io/github/cottonmc/templates/block/TemplateEntity.java

189 lines
6.6 KiB
Java
Raw Normal View History

package io.github.cottonmc.templates.block;
import io.github.cottonmc.templates.Templates;
import io.github.cottonmc.templates.api.ThemeableBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
2023-06-15 09:08:20 +02:00
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
2023-06-15 10:24:11 +02:00
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
2023-06-15 09:08:20 +02:00
import net.minecraft.registry.Registries;
import net.minecraft.server.world.ServerWorld;
2023-06-15 09:08:20 +02:00
import net.minecraft.util.math.BlockPos;
2023-06-15 10:24:11 +02:00
import org.jetbrains.annotations.Nullable;
import javax.annotation.Nonnull;
2023-06-15 10:24:11 +02:00
import java.util.Objects;
public class TemplateEntity extends BlockEntity implements ThemeableBlockEntity {
protected BlockState renderedState = Blocks.AIR.getDefaultState();
protected byte bitfield = DEFAULT_BITFIELD;
2023-07-03 09:35:07 +02:00
protected static final int SPENT_GLOWSTONE_DUST_MASK = 0b00000001;
protected static final int SPENT_REDSTONE_TORCH_MASK = 0b00000010;
protected static final int SPENT_POPPED_CHORUS_MASK = 0b00000100;
protected static final int EMITS_REDSTONE_MASK = 0b00001000;
protected static final int IS_SOLID_MASK = 0b00010000;
protected static final byte DEFAULT_BITFIELD = IS_SOLID_MASK; //brand-new templates shall be solid
2023-06-15 07:59:48 +02:00
//Using one-character names is a little brash, like, what if there's a mod that adds crap to the NBT of ever
//block entity, and uses short names for the same reason I am (there are lots and lots of block entities).
//Kinda doubt it?
protected static final String BLOCKSTATE_KEY = "s";
protected static final String BITFIELD_KEY = "b";
2023-07-08 08:29:22 +02:00
public TemplateEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
2023-06-15 09:08:20 +02:00
super(type, pos, state);
}
2023-06-15 07:59:48 +02:00
@Override
2023-06-15 09:08:20 +02:00
public void readNbt(NbtCompound tag) {
super.readNbt(tag);
BlockState lastRenderedState = renderedState;
if(tag.contains("BlockState")) { //2.0.4 and earlier
renderedState = NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), tag.getCompound("BlockState"));
if(tag.getBoolean("spentglow")) spentGlowstoneDust();
if(tag.getBoolean("spentredst")) spentRedstoneTorch();
if(tag.getBoolean("spentchor")) spentPoppedChorus();
setEmitsRedstone(tag.getBoolean("emitsredst"));
setSolidity(!tag.contains("solid") || tag.getBoolean("solid")); //default to "true" if it's nonexistent
} else {
renderedState = NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), tag.getCompound(BLOCKSTATE_KEY));
bitfield = tag.contains(BITFIELD_KEY) ? tag.getByte(BITFIELD_KEY) : DEFAULT_BITFIELD;
}
2023-07-03 09:35:07 +02:00
//Force a chunk remesh on the client if the displayed blockstate has changed
2023-07-21 09:49:49 +02:00
//TODO: doors? (need remeshing when the *other* party changes)
if(world != null && world.isClient && !Objects.equals(lastRenderedState, renderedState)) {
Templates.chunkRerenderProxy.accept(world, pos);
}
}
2023-06-15 07:59:48 +02:00
@Override
2023-06-15 09:08:20 +02:00
public void writeNbt(NbtCompound tag) {
super.writeNbt(tag);
2023-07-08 08:29:22 +02:00
if(renderedState != Blocks.AIR.getDefaultState()) tag.put(BLOCKSTATE_KEY, NbtHelper.fromBlockState(renderedState));
if(bitfield != DEFAULT_BITFIELD) tag.putByte(BITFIELD_KEY, bitfield);
}
2023-06-15 07:59:48 +02:00
public static @Nonnull BlockState readStateFromItem(ItemStack stack) {
NbtCompound blockEntityTag = BlockItem.getBlockEntityNbt(stack);
if(blockEntityTag == null) return Blocks.AIR.getDefaultState();
//slightly paranoid NBT handling cause you never know what mysteries are afoot with items
NbtElement subElement;
if(blockEntityTag.contains(BLOCKSTATE_KEY)) subElement = blockEntityTag.get(BLOCKSTATE_KEY); //2.0.5
else if(blockEntityTag.contains("BlockState")) subElement = blockEntityTag.get("BlockState"); //old 2.0.4 items
else return Blocks.AIR.getDefaultState();
if(!(subElement instanceof NbtCompound subCompound)) return Blocks.AIR.getDefaultState();
else return NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), subCompound);
}
2023-06-15 07:59:48 +02:00
//RenderAttachmentBlockEntity impl. Note that ThemeableBlockEntity depends on this returning a BlockState object.
@Override
public BlockState getRenderAttachmentData() {
return renderedState;
}
2023-06-15 07:59:48 +02:00
2023-06-15 10:24:11 +02:00
public void setRenderedState(BlockState newState) {
2023-07-03 09:35:07 +02:00
if(!Objects.equals(renderedState, newState)) {
renderedState = newState;
markDirtyAndDispatch();
2023-07-03 09:35:07 +02:00
}
2023-06-15 10:24:11 +02:00
}
2023-07-03 09:35:07 +02:00
public boolean hasSpentGlowstoneDust() {
return (bitfield & SPENT_GLOWSTONE_DUST_MASK) != 0;
}
2023-06-15 07:59:48 +02:00
2023-07-03 09:35:07 +02:00
public void spentGlowstoneDust() {
bitfield |= SPENT_GLOWSTONE_DUST_MASK;
markDirtyAndDispatch();
}
2023-06-15 07:59:48 +02:00
2023-07-03 09:35:07 +02:00
public boolean hasSpentRedstoneTorch() {
return (bitfield & SPENT_REDSTONE_TORCH_MASK) != 0;
}
2023-06-15 07:59:48 +02:00
2023-07-03 09:35:07 +02:00
public void spentRedstoneTorch() {
bitfield |= SPENT_REDSTONE_TORCH_MASK;
markDirtyAndDispatch();
}
2023-07-06 05:40:12 +02:00
public boolean hasSpentPoppedChorus() {
return (bitfield & SPENT_POPPED_CHORUS_MASK) != 0;
2023-07-06 05:40:12 +02:00
}
public void spentPoppedChorus() {
bitfield |= SPENT_POPPED_CHORUS_MASK;
markDirtyAndDispatch();
2023-07-06 05:40:12 +02:00
}
2023-07-08 08:29:22 +02:00
public boolean emitsRedstone() {
return (bitfield & EMITS_REDSTONE_MASK) != 0;
2023-07-08 08:29:22 +02:00
}
public void setEmitsRedstone(boolean nextEmitsRedstone) {
boolean currentlyEmitsRedstone = emitsRedstone();
if(currentlyEmitsRedstone != nextEmitsRedstone) {
if(currentlyEmitsRedstone) bitfield &= ~EMITS_REDSTONE_MASK;
else bitfield |= EMITS_REDSTONE_MASK;
markDirtyAndDispatch();
2023-07-08 08:29:22 +02:00
if(world != null) world.updateNeighbors(pos, getCachedState().getBlock());
}
}
public boolean isSolid() {
return (bitfield & IS_SOLID_MASK) != 0;
2023-07-08 08:29:22 +02:00
}
public void setSolidity(boolean nextSolid) {
boolean currentlySolid = isSolid();
if(currentlySolid != nextSolid) {
if(currentlySolid) bitfield &= ~IS_SOLID_MASK;
else bitfield |= IS_SOLID_MASK;
markDirtyAndDispatch();
if(world != null) world.setBlockState(pos, getCachedState()); //do i need to invalidate any shape caches or something
2023-07-08 08:29:22 +02:00
}
}
//<standard blockentity boilerplate>
@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
@Override
public NbtCompound toInitialChunkDataNbt() {
//TERRIBLE yarn name, this is "getUpdateTag", it's the nbt that will be sent to clients
//and it just calls "writeNbt"
return createNbt();
}
protected void dispatch() {
if(world instanceof ServerWorld sworld) sworld.getChunkManager().markForUpdate(pos);
}
protected void markDirtyAndDispatch() {
markDirty();
dispatch();
}
//</standard blockentity boilerplate>
}