328 lines
14 KiB
Java
328 lines
14 KiB
Java
package com.dairymoose.modernlife.tileentities;
|
|
|
|
import com.dairymoose.modernlife.core.CustomBlocks;
|
|
import com.dairymoose.modernlife.core.ModernLifeClient;
|
|
import com.dairymoose.modernlife.core.ModernLifeCommon;
|
|
import com.dairymoose.modernlife.network.play.client.ClientboundMultipartCanvasPacket;
|
|
import com.dairymoose.modernlife.network.play.client.ICanvasReader;
|
|
import com.dairymoose.modernlife.util.CanvasData;
|
|
import com.mojang.datafixers.types.Type;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.multiplayer.ClientLevel;
|
|
import net.minecraft.client.renderer.RenderType;
|
|
import net.minecraft.client.renderer.texture.DynamicTexture;
|
|
import net.minecraft.client.renderer.texture.TextureManager;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.NonNullList;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.network.Connection;
|
|
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.Container;
|
|
import net.minecraft.world.ContainerHelper;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.phys.AABB;
|
|
import net.minecraft.world.phys.Vec3;
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
import net.minecraftforge.fml.DistExecutor;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/CanvasBlockEntity.class */
|
|
public class CanvasBlockEntity extends BlockEntity implements Container, ICanvasReader {
|
|
public static final BlockEntityType<CanvasBlockEntity> CANVAS = BlockEntityType.Builder.of(CanvasBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_CANVAS.get()}).build((Type) null);
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
public static final int CANVAS_CONTAINER_SIZE = 1;
|
|
public float blockWidth;
|
|
public float blockHeight;
|
|
public float xOffset;
|
|
public float yOffset;
|
|
private Object nbtLock;
|
|
private NonNullList<ItemStack> canvasStack;
|
|
CanvasData canvasData;
|
|
private DynamicTexture texture;
|
|
private RenderType dynamicRenderType;
|
|
|
|
@OnlyIn(Dist.CLIENT)
|
|
private TextureManager textureManager;
|
|
|
|
public CanvasBlockEntity(BlockPos pos, BlockState state) {
|
|
super(CANVAS, pos, state);
|
|
this.blockWidth = 0.0f;
|
|
this.blockHeight = 0.0f;
|
|
this.xOffset = 0.0f;
|
|
this.yOffset = 0.0f;
|
|
this.nbtLock = new Object();
|
|
this.canvasStack = NonNullList.withSize(1, ItemStack.EMPTY);
|
|
this.canvasData = new CanvasData();
|
|
this.texture = null;
|
|
ClientboundMultipartCanvasPacket.subscribe(this);
|
|
}
|
|
|
|
@Override // com.dairymoose.modernlife.network.play.client.ICanvasReader
|
|
public void onUpdate(long uniqueId) {
|
|
if (isRemoved()) {
|
|
ClientboundMultipartCanvasPacket.unsubscribe(this);
|
|
return;
|
|
}
|
|
if (!isEmpty() && getItem(0).getItem() == CustomBlocks.ITEM_CANVAS.get()) {
|
|
CompoundTag nbt = getItem(0).getTag();
|
|
if (nbt.contains("UniqueId")) {
|
|
long thisUniqueId = nbt.getLong("UniqueId");
|
|
if (thisUniqueId == uniqueId) {
|
|
updateToNewTexture();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public AABB getRenderBoundingBox() {
|
|
BlockPos blockPos = getBlockPos();
|
|
Vec3 loc = new Vec3(blockPos.getX() + 0.5d, blockPos.getY() + 0.5d, blockPos.getZ() + 0.5d);
|
|
return AABB.ofSize(loc, 10.0d, 10.0d, 10.0d);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* renamed from: com.dairymoose.modernlife.tileentities.CanvasBlockEntity$1 */
|
|
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/CanvasBlockEntity$1.class */
|
|
public class RunnableC01561 implements Runnable {
|
|
final /* synthetic */ CanvasBlockEntity val$thisEntity;
|
|
|
|
RunnableC01561(CanvasBlockEntity canvasBlockEntity) {
|
|
this = canvasBlockEntity;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
CompoundTag nbt;
|
|
if (!this.isEmpty() && this.getItem(0).getItem() == CustomBlocks.ITEM_CANVAS.get() && (nbt = this.getItem(0).getTag()) != null && nbt.contains("UniqueId")) {
|
|
long uniqueId = nbt.getLong("UniqueId");
|
|
Logger logger = ModernLifeCommon.LOGGER;
|
|
this.getBlockPos();
|
|
logger.debug("updating texture for ID=" + uniqueId + " at pos = " + logger);
|
|
if (CanvasBlockEntity.this.texture != null) {
|
|
CanvasBlockEntity.this.texture.close();
|
|
}
|
|
CanvasBlockEntity.this.texture = null;
|
|
byte[] compressed = ModernLifeClient.getCanvasData(uniqueId);
|
|
if (compressed != null) {
|
|
int texWidth = ModernLifeClient.getCanvasWidth(uniqueId);
|
|
int texHeight = ModernLifeClient.getCanvasHeight(uniqueId);
|
|
if (texWidth > 0 && texHeight > 0) {
|
|
CanvasBlockEntity.this.canvasData.disable();
|
|
CanvasBlockEntity.this.canvasData.setTextureSize(texWidth, texHeight);
|
|
CanvasBlockEntity.this.canvasData.fromCompressedNbt(compressed);
|
|
CanvasBlockEntity.this.canvasData.reenable();
|
|
synchronized (CanvasBlockEntity.this.nbtLock) {
|
|
if (nbt.contains("BlockWidth")) {
|
|
this.blockWidth = nbt.getFloat("BlockWidth");
|
|
}
|
|
if (nbt.contains("BlockHeight")) {
|
|
this.blockHeight = nbt.getFloat("BlockHeight");
|
|
}
|
|
if (nbt.contains("xOffset")) {
|
|
this.xOffset = nbt.getFloat("xOffset");
|
|
}
|
|
if (nbt.contains("yOffset")) {
|
|
this.yOffset = nbt.getFloat("yOffset");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void updateToNewTexture() {
|
|
DistExecutor.runWhenOn(Dist.CLIENT, () -> {
|
|
return new Runnable() { // from class: com.dairymoose.modernlife.tileentities.CanvasBlockEntity.1
|
|
final /* synthetic */ CanvasBlockEntity val$thisEntity;
|
|
|
|
RunnableC01561(CanvasBlockEntity this) {
|
|
this = this;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
CompoundTag nbt;
|
|
if (!this.isEmpty() && this.getItem(0).getItem() == CustomBlocks.ITEM_CANVAS.get() && (nbt = this.getItem(0).getTag()) != null && nbt.contains("UniqueId")) {
|
|
long uniqueId = nbt.getLong("UniqueId");
|
|
Logger logger = ModernLifeCommon.LOGGER;
|
|
this.getBlockPos();
|
|
logger.debug("updating texture for ID=" + uniqueId + " at pos = " + logger);
|
|
if (CanvasBlockEntity.this.texture != null) {
|
|
CanvasBlockEntity.this.texture.close();
|
|
}
|
|
CanvasBlockEntity.this.texture = null;
|
|
byte[] compressed = ModernLifeClient.getCanvasData(uniqueId);
|
|
if (compressed != null) {
|
|
int texWidth = ModernLifeClient.getCanvasWidth(uniqueId);
|
|
int texHeight = ModernLifeClient.getCanvasHeight(uniqueId);
|
|
if (texWidth > 0 && texHeight > 0) {
|
|
CanvasBlockEntity.this.canvasData.disable();
|
|
CanvasBlockEntity.this.canvasData.setTextureSize(texWidth, texHeight);
|
|
CanvasBlockEntity.this.canvasData.fromCompressedNbt(compressed);
|
|
CanvasBlockEntity.this.canvasData.reenable();
|
|
synchronized (CanvasBlockEntity.this.nbtLock) {
|
|
if (nbt.contains("BlockWidth")) {
|
|
this.blockWidth = nbt.getFloat("BlockWidth");
|
|
}
|
|
if (nbt.contains("BlockHeight")) {
|
|
this.blockHeight = nbt.getFloat("BlockHeight");
|
|
}
|
|
if (nbt.contains("xOffset")) {
|
|
this.xOffset = nbt.getFloat("xOffset");
|
|
}
|
|
if (nbt.contains("yOffset")) {
|
|
this.yOffset = nbt.getFloat("yOffset");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
});
|
|
}
|
|
|
|
public CompoundTag getUpdateTag() {
|
|
CompoundTag nbt = super.getUpdateTag();
|
|
if (isEmpty() || getItem(0).getItem() == CustomBlocks.ITEM_CANVAS.get()) {
|
|
}
|
|
ContainerHelper.saveAllItems(nbt, this.canvasStack);
|
|
return nbt;
|
|
}
|
|
|
|
public void handleUpdateTag(CompoundTag tag) {
|
|
ContainerHelper.loadAllItems(tag, this.canvasStack);
|
|
ItemStack item = getItem(0);
|
|
CompoundTag nbt = null;
|
|
if (item != null) {
|
|
nbt = item.getTag();
|
|
}
|
|
if (!isEmpty() && item != null && item.getItem() == CustomBlocks.ITEM_CANVAS.get() && nbt != null && nbt.contains("UniqueId")) {
|
|
getItem(0).getTag().getLong("UniqueId");
|
|
}
|
|
updateToNewTexture();
|
|
super.handleUpdateTag(tag);
|
|
}
|
|
|
|
public void load(CompoundTag p_230337_2_) {
|
|
super.load(p_230337_2_);
|
|
ContainerHelper.loadAllItems(p_230337_2_, this.canvasStack);
|
|
ItemStack item = getItem(0);
|
|
if (!item.isEmpty() && item.hasTag()) {
|
|
updateToNewTexture();
|
|
}
|
|
}
|
|
|
|
/* renamed from: getUpdatePacket */
|
|
public ClientboundBlockEntityDataPacket m219getUpdatePacket() {
|
|
return ClientboundBlockEntityDataPacket.create(this);
|
|
}
|
|
|
|
public void onDataPacket(Connection conn, ClientboundBlockEntityDataPacket pkt) {
|
|
CompoundTag tag = pkt.getTag();
|
|
if (tag != null) {
|
|
ContainerHelper.loadAllItems(tag, this.canvasStack);
|
|
ItemStack item = getItem(0);
|
|
if (!item.isEmpty() && item.hasTag()) {
|
|
updateToNewTexture();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected void saveAdditional(CompoundTag nbt) {
|
|
ContainerHelper.saveAllItems(nbt, this.canvasStack);
|
|
}
|
|
|
|
public void m_6211_() {
|
|
this.canvasStack.set(0, ItemStack.EMPTY);
|
|
}
|
|
|
|
public int getContainerSize() {
|
|
return 1;
|
|
}
|
|
|
|
public boolean isEmpty() {
|
|
return ((ItemStack) this.canvasStack.get(0)).isEmpty();
|
|
}
|
|
|
|
public ItemStack getItem(int var1) {
|
|
return (ItemStack) this.canvasStack.get(var1);
|
|
}
|
|
|
|
public ItemStack removeItem(int var1, int var2) {
|
|
return ContainerHelper.removeItem(this.canvasStack, var1, var2);
|
|
}
|
|
|
|
public ItemStack removeItemNoUpdate(int var1) {
|
|
return ContainerHelper.takeItem(this.canvasStack, var1);
|
|
}
|
|
|
|
public void setItem(int var1, ItemStack item) {
|
|
this.canvasStack.set(var1, item);
|
|
if (item.getItem() != CustomBlocks.ITEM_CANVAS.get() || item.hasTag()) {
|
|
}
|
|
}
|
|
|
|
public boolean stillValid(Player p_70300_1_) {
|
|
return this.level.getBlockEntity(this.worldPosition) == this && p_70300_1_.distanceToSqr(((double) this.worldPosition.getX()) + 0.5d, ((double) this.worldPosition.getY()) + 0.5d, ((double) this.worldPosition.getZ()) + 0.5d) <= 64.0d;
|
|
}
|
|
|
|
public RenderType getDynamicRenderType() {
|
|
return this.dynamicRenderType;
|
|
}
|
|
|
|
@OnlyIn(Dist.CLIENT)
|
|
public void initImage() {
|
|
if (this.textureManager == null) {
|
|
this.textureManager = Minecraft.getInstance().getTextureManager();
|
|
}
|
|
this.canvasData.initImage();
|
|
if ((getLevel() instanceof ClientLevel) && this.texture == null && this.canvasData.isValid()) {
|
|
this.texture = new DynamicTexture(this.canvasData.textureWidth(), this.canvasData.textureHeight(), true);
|
|
ResourceLocation resourcelocation = this.textureManager.register("artpad/dyn", this.texture);
|
|
this.dynamicRenderType = RenderType.text(resourcelocation);
|
|
updateTexture();
|
|
}
|
|
}
|
|
|
|
private void updateTexture() {
|
|
if (!this.canvasData.isValid()) {
|
|
return;
|
|
}
|
|
if (getLevel() instanceof ClientLevel) {
|
|
for (int i = 0; i < this.canvasData.textureHeight(); i++) {
|
|
for (int j = 0; j < this.canvasData.textureWidth(); j++) {
|
|
int textureWidth = j + (i * this.canvasData.textureWidth());
|
|
if (this.texture != null && this.texture.getPixels() != null && this.canvasData.isValid()) {
|
|
try {
|
|
this.texture.getPixels().setPixelRGBA(j, i, this.canvasData.getAbgrOutputPixel(j, i));
|
|
} catch (IllegalStateException e) {
|
|
ModernLifeCommon.LOGGER.warn("Unallocated image error");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
try {
|
|
if (this.texture != null && this.canvasData.isValid()) {
|
|
this.texture.upload();
|
|
}
|
|
} catch (IllegalStateException e2) {
|
|
ModernLifeCommon.LOGGER.warn("Unallocated image error");
|
|
}
|
|
}
|
|
if (getLevel() instanceof ServerLevel) {
|
|
getLevel().markAndNotifyBlock(getBlockPos(), getLevel().getChunkAt(getBlockPos()), getBlockState(), getBlockState(), 2, 0);
|
|
}
|
|
}
|
|
}
|