Skip to content

Commit

Permalink
Color sync and jade fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Apr 7, 2024
1 parent 4940c2b commit cadbeaa
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package dev.compactmods.machines.neoforge.compat.jade;

import dev.compactmods.machines.neoforge.machine.Machines;
import dev.compactmods.machines.neoforge.machine.block.BoundCompactMachineBlock;
import dev.compactmods.machines.neoforge.machine.block.BoundCompactMachineBlockEntity;
import net.minecraft.resources.ResourceLocation;
import snownee.jade.api.BlockAccessor;
import snownee.jade.api.IBlockComponentProvider;
import snownee.jade.api.ITooltip;
import snownee.jade.api.IWailaClientRegistration;
import snownee.jade.api.IWailaCommonRegistration;
import snownee.jade.api.IWailaPlugin;
import snownee.jade.api.TooltipPosition;
import snownee.jade.api.WailaPlugin;
import snownee.jade.api.config.IPluginConfig;

@WailaPlugin
public class CMJadePlugin implements IWailaPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;

public class RoomAttachmentData extends AttachmentHolder {

Expand Down Expand Up @@ -62,6 +63,10 @@ public void save() {
}

try {
if(!file.exists()) {
Files.createDirectories(file.toPath());
}

NbtIo.writeCompressed(fullTag, file.toPath());
} catch (IOException e) {
LoggingUtil.modLog().error("Failed to write room data: " + roomCode, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ public BoundCompactMachineBlock(Properties props) {

@Override
public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelReader level, BlockPos pos, Player player) {
if (level.getBlockEntity(pos) instanceof BoundCompactMachineBlockEntity be) {
return MachineCreator.boundToRoom(be.connectedRoom(), be.getColor());
try {
if (level.getBlockEntity(pos) instanceof BoundCompactMachineBlockEntity be) {
return MachineCreator.boundToRoom(be.connectedRoom(), be.getColor());
}

return MachineCreator.unbound();
}

LoggingUtil.modLog().warn("Warning: tried to pick block on a machine that does not have an associated block entity.");
return MachineCreator.unbound();
catch(Exception ex) {
LoggingUtil.modLog().warn("Warning: tried to pick block on a bound machine that does not have a room bound.", ex);
return MachineCreator.unbound();
}
}

@Override
Expand Down Expand Up @@ -90,29 +96,6 @@ public float getDestroyProgress(BlockState state, Player player, BlockGetter lev
}
}

@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
if (level.getBlockEntity(pos) instanceof BoundCompactMachineBlockEntity tile) {
// force client redraw
if (stack.getItem() instanceof IBoundCompactMachineItem bound) {
if (!level.isClientSide) {
bound.getRoom(stack).ifPresent(roomCode -> {
if(placer instanceof ServerPlayer sp && RoomHelper.entityInsideRoom(sp, roomCode)) {
// TODO: Ouroboros advancement
}

tile.setConnectedRoom(roomCode);
});
} else {
final int color = bound.getMachineColor(stack);
tile.setColor(color);
}
// PacketDistributor.TRACKING_CHUNK.with(level.getChunkAt(pos))
// .send(ClientboundBlockEntityDataPacket.create(tile));
}
}
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.compactmods.machines.neoforge.machine.block;

import dev.compactmods.machines.api.machine.block.IBoundCompactMachineBlockEntity;
import dev.compactmods.machines.api.room.RoomApi;
import dev.compactmods.machines.api.machine.IColoredMachine;
import dev.compactmods.machines.neoforge.machine.Machines;
Expand All @@ -18,7 +19,7 @@
import java.util.Optional;
import java.util.UUID;

public class BoundCompactMachineBlockEntity extends BlockEntity implements IColoredMachine {
public class BoundCompactMachineBlockEntity extends BlockEntity implements IBoundCompactMachineBlockEntity, IColoredMachine {

protected UUID owner;
private String roomCode;
Expand All @@ -30,11 +31,6 @@ public class BoundCompactMachineBlockEntity extends BlockEntity implements IColo
@Nullable
private Component customName;

public static final String NBT_OWNER = "owner";
public static final String NBT_COLOR = "machine_color";
public static final String NBT_ROOM_CODE = "room_code";
public static final String NBT_ROOM_COLOR = "room_color";

public BoundCompactMachineBlockEntity(BlockPos pos, BlockState state) {
super(Machines.MACHINE_ENTITY.get(), pos, state);
}
Expand Down Expand Up @@ -94,11 +90,8 @@ public CompoundTag getUpdateTag() {
data.putString(NBT_ROOM_CODE, roomCode);
}

if (level instanceof ServerLevel) {
// TODO - Internal player list
if (this.owner != null)
data.putUUID("owner", this.owner);
}
if (this.owner != null)
data.putUUID("owner", this.owner);

if (hasMachineColorOverride)
data.putInt(NBT_COLOR, machineColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import dev.compactmods.machines.api.shrinking.PSDTags;
import dev.compactmods.machines.neoforge.machine.Machines;
import dev.compactmods.machines.neoforge.machine.item.UnboundCompactMachineItem;
import dev.compactmods.machines.neoforge.network.machine.UnboundMachineTemplateSyncPacket;
import dev.compactmods.machines.neoforge.room.RoomHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
Expand All @@ -27,6 +29,7 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -38,8 +41,10 @@ public UnboundCompactMachineBlock(Properties props) {
@Override
public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelReader level, BlockPos pos, Player player) {
if (level.getBlockEntity(pos) instanceof UnboundCompactMachineEntity be) {
final var id = be.templateId();
final var temp = be.template().orElseThrow();
return UnboundCompactMachineItem.forTemplate(be.templateId(), temp);
if(id != null && temp != RoomTemplate.INVALID_TEMPLATE)
return UnboundCompactMachineItem.forTemplate(id, temp);
}

return MachineCreator.unbound();
Expand All @@ -50,16 +55,6 @@ public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelRead
return new UnboundCompactMachineEntity(pos, state);
}

@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
level.getBlockEntity(pos, Machines.UNBOUND_MACHINE_ENTITY.get()).ifPresent(tile -> {
if(stack.getItem() instanceof IUnboundCompactMachineItem unbound) {
final var template = unbound.getTemplateId(stack);
tile.setTemplate(template);
}
});
}

@Override
public @NotNull InteractionResult use(@NotNull BlockState state, Level level, @NotNull BlockPos pos, Player player, @NotNull InteractionHand hand, @NotNull BlockHitResult hitResult) {
MinecraftServer server = level.getServer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package dev.compactmods.machines.neoforge.machine.block;

import dev.compactmods.machines.api.machine.block.ICompactMachineBlockEntity;
import dev.compactmods.machines.api.machine.block.IUnboundCompactMachineBlockEntity;
import dev.compactmods.machines.api.room.RoomApi;
import dev.compactmods.machines.api.room.RoomTemplate;
import dev.compactmods.machines.api.machine.IColoredMachine;
import dev.compactmods.machines.neoforge.machine.Machines;
import dev.compactmods.machines.neoforge.network.machine.UnboundMachineTemplateSyncPacket;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class UnboundCompactMachineEntity extends BlockEntity implements IColoredMachine {

public static final String NBT_TEMPLATE_ID = "template_id";
public class UnboundCompactMachineEntity extends BlockEntity implements IUnboundCompactMachineBlockEntity, IColoredMachine {

private RoomTemplate template;
private @Nullable ResourceLocation templateId;
Expand All @@ -31,22 +36,19 @@ public UnboundCompactMachineEntity(BlockPos pos, BlockState state) {
public void load(@NotNull CompoundTag nbt) {
super.load(nbt);

if (nbt.contains(NBT_TEMPLATE_ID)) {
templateId = new ResourceLocation(nbt.getString(NBT_TEMPLATE_ID));
if(nbt.contains(NBT_TEMPLATE_ID)) {
this.templateId = new ResourceLocation(nbt.getString(NBT_TEMPLATE_ID));
}
}

@Override
public void onLoad() {
super.onLoad();

loadTemplateFromID();
}

private void loadTemplateFromID() {
if(templateId != null && level != null && !level.isClientSide) {
final var server = level.getServer();
this.template = RoomApi.getTemplates(server).get(templateId);
if(this.level != null && this.templateId != null) {
this.template = level.registryAccess()
.registryOrThrow(RoomTemplate.REGISTRY_KEY)
.get(this.templateId);
}
}

Expand All @@ -60,8 +62,14 @@ protected void saveAdditional(@NotNull CompoundTag nbt) {
public CompoundTag getUpdateTag() {
CompoundTag data = super.getUpdateTag();

if (!this.template.equals(RoomTemplate.INVALID_TEMPLATE))
data.putString(NBT_TEMPLATE_ID, this.templateId.toString());
if(templateId != null) {
var encodedTemplate = RoomTemplate.CODEC.encodeStart(NbtOps.INSTANCE, this.template)
.getOrThrow(false, err -> {
});

data.putString(NBT_TEMPLATE_ID, templateId.toString());
data.put("template", encodedTemplate);
}

return data;
}
Expand All @@ -70,13 +78,26 @@ public CompoundTag getUpdateTag() {
public void handleUpdateTag(CompoundTag tag) {
super.handleUpdateTag(tag);

if (tag.contains(NBT_TEMPLATE_ID))
if (tag.contains(NBT_TEMPLATE_ID)) {
templateId = new ResourceLocation(tag.getString(NBT_TEMPLATE_ID));

this.template = RoomTemplate.CODEC
.parse(NbtOps.INSTANCE, tag.get("template"))
.getOrThrow(false, err -> {});
}
}

public void setTemplate(ResourceLocation template) {
this.templateId = template;
this.loadTemplateFromID();
this.template = level.registryAccess()
.registryOrThrow(RoomTemplate.REGISTRY_KEY)
.get(template);

if(!level.isClientSide) {
PacketDistributor.TRACKING_CHUNK.with(level.getChunkAt(worldPosition))
.send(new UnboundMachineTemplateSyncPacket(GlobalPos.of(level.dimension(), worldPosition), this.templateId));
}

this.setChanged();
}

Expand All @@ -86,7 +107,7 @@ public Optional<RoomTemplate> template() {

@Override
public int getColor() {
return this.template().map(RoomTemplate::color).orElse(0xFFFFFFFF);
return this.template().map(RoomTemplate::color).orElse(0xFFFF0000);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ public String getDescriptionId(ItemStack stack) {
return FALLBACK_ID;
}

// public static Vec3i getRoomSize(ItemStack stack) {
// if (!stack.hasTag()) return Vec3i.ZERO;
// final var tag = stack.getTag();
// if (tag == null || tag.isEmpty() || !tag.contains(NBT_ROOM_DIMENSIONS)) return Vec3i.ZERO;
// final var dimNbt = tag.getIntArray(NBT_ROOM_DIMENSIONS);
// return new Vec3i(dimNbt[0], dimNbt[1], dimNbt[2]);
// }
//
// public static ItemStack setRoomSize(ItemStack stack, Vec3i innerBounds) {
// var tag = stack.getOrCreateTag();
// tag.putIntArray(NBT_ROOM_DIMENSIONS, new int[]{
// innerBounds.getX(),
// innerBounds.getY(),
// innerBounds.getZ()
// });
//
// return stack;
// }

@Override
public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
super.appendHoverText(stack, worldIn, tooltip, flagIn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static Optional<RoomTemplate> getTemplate(RegistryAccess registries, Item
return Optional.empty();
}

public static ItemStack forTemplate(ResourceLocation templateId, RoomTemplate template) {
public static ItemStack forTemplate(@NotNull ResourceLocation templateId, @NotNull RoomTemplate template) {
final var stack = new ItemStack(Machines.UNBOUND_MACHINE_BLOCK_ITEM.get(), 1);
if (stack.getItem() instanceof IUnboundCompactMachineItem unbound) {
unbound.setTemplate(stack, templateId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.compactmods.machines.neoforge.network;

import dev.compactmods.machines.api.Constants;
import dev.compactmods.machines.neoforge.network.machine.UnboundMachineTemplateSyncPacket;
import net.minecraft.network.FriendlyByteBuf;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
Expand Down Expand Up @@ -29,5 +30,8 @@ public static void onPacketRegistration(final RegisterPayloadHandlerEvent payloa

main.play(PlayerRequestedUpgradeMenuPacket.ID, (FriendlyByteBuf b) -> b.readJsonWithCodec(PlayerRequestedUpgradeMenuPacket.CODEC),
builder -> builder.server(PlayerRequestedUpgradeMenuPacket.HANDLER));

main.play(UnboundMachineTemplateSyncPacket.ID, buf -> buf.readJsonWithCodec(UnboundMachineTemplateSyncPacket.CODEC),
b -> b.client(UnboundMachineTemplateSyncPacket.HANDLER));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.compactmods.machines.neoforge.network.machine;

import dev.compactmods.machines.api.room.RoomTemplate;
import dev.compactmods.machines.neoforge.machine.Machines;
import net.minecraft.client.Minecraft;
import net.minecraft.core.GlobalPos;
import net.minecraft.resources.ResourceLocation;

public class ClientMachinePacketHandler {
public static void setMachineTemplate(GlobalPos position, ResourceLocation template) {
var mc = Minecraft.getInstance();
assert mc.level != null;
if(mc.level.dimension() == position.dimension()) {
mc.level.getBlockEntity(position.pos(), Machines.UNBOUND_MACHINE_ENTITY.get())
.ifPresent(ent -> ent.setTemplate(template));
}
}
}
Loading

0 comments on commit cadbeaa

Please sign in to comment.