Skip to content

Commit

Permalink
Some bug fixes, make jade/wthit use server provided icon for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed May 29, 2024
1 parent 3d7d9bf commit 216a938
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.97.6+1.20.6

maven_group = eu.pb4

mod_version = 0.8.1
mod_version = 0.8.2

minecraft_version_supported = ">=1.20.5-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
Expand All @@ -17,11 +18,15 @@

@Environment(EnvType.CLIENT)
public record ClientPolymerBlock(Identifier identifier, int numId, Text name, BlockState defaultBlockState,
@Nullable Block registryEntry) implements ClientPolymerEntry<Block> {
@Nullable Block registryEntry, ItemStack displayStack) implements ClientPolymerEntry<Block> {
public static final ClientPolymerBlock NONE = new ClientPolymerBlock(PolymerImplUtils.id("none"), 0, Text.empty(), Blocks.AIR.getDefaultState());
public static final State NONE_STATE = new State(Collections.emptyMap(), NONE);
public static final PolymerRegistry<ClientPolymerBlock> REGISTRY = InternalClientRegistry.BLOCKS;

public ClientPolymerBlock(Identifier identifier, int numId, Text name, BlockState defaultBlockState, @Nullable Block registryEntry) {
this(identifier, numId, name, defaultBlockState, registryEntry, defaultBlockState.getBlock().asItem().getDefaultStack());
}

public ClientPolymerBlock(Identifier identifier, int numId, Text name, BlockState defaultBlockState) {
this(identifier, numId, name, defaultBlockState, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ public IElement getIcon(BlockAccessor accessor, snownee.jade.api.config.IPluginC
if (block != ClientPolymerBlock.NONE_STATE) {
BlockState state = accessor.getLevel().getBlockState(accessor.getPosition());

var itemStack = state.getBlock().getPickStack(accessor.getLevel(), accessor.getPosition(), state);

if (!itemStack.isEmpty() && state.hasBlockEntity()) {
var blockEntity = accessor.getLevel().getBlockEntity(accessor.getPosition());

if (blockEntity != null) {
itemStack.applyComponentsFrom(blockEntity.createComponentMap());
var itemStack = block.block().displayStack();
if (itemStack.isEmpty()) {
itemStack = state.getBlock().getPickStack(accessor.getLevel(), accessor.getPosition(), state);
if (!itemStack.isEmpty() && state.hasBlockEntity()) {
var blockEntity = accessor.getLevel().getBlockEntity(accessor.getPosition());

if (blockEntity != null) {
itemStack.applyComponentsFrom(blockEntity.getComponents());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ private static class BlockOverride implements IBlockComponentProvider {
if (block != ClientPolymerBlock.NONE_STATE) {
BlockState state = accessor.getWorld().getBlockState(accessor.getPosition());

var itemStack = state.getBlock().getPickStack(accessor.getWorld(), accessor.getPosition(), state);

if (!itemStack.isEmpty() && state.hasBlockEntity()) {
var blockEntity = accessor.getWorld().getBlockEntity(accessor.getPosition());

if (blockEntity != null) {
itemStack.applyComponentsFrom(blockEntity.getComponents());
var itemStack = block.block().displayStack();
if (itemStack.isEmpty()) {
itemStack = state.getBlock().getPickStack(accessor.getWorld(), accessor.getPosition(), state);
if (!itemStack.isEmpty() && state.hasBlockEntity()) {
var blockEntity = accessor.getWorld().getBlockEntity(accessor.getPosition());

if (blockEntity != null) {
itemStack.applyComponentsFrom(blockEntity.getComponents());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static boolean prevent(ServerCommonNetworkHandler handler, Packet<?> pack
return true;
} else if ((packet instanceof EntityAttributesS2CPacket original
&& EntityAttachedPacket.get(packet, original.getEntityId()) instanceof PolymerEntity entity
&& InternalEntityHelpers.isLivingEntity(entity.getPolymerEntityType(player)))) {
&& !InternalEntityHelpers.isLivingEntity(entity.getPolymerEntityType(player)))) {
return true;
} else if (packet instanceof BlockEntityUpdateS2CPacket be && PolymerBlockUtils.isPolymerBlockEntityType(be.getBlockEntityType())) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static <T> CustomPayload.Id<PolymerGenericListPayload<T>> registerList(Id
register(SYNC_FINISHED, PolymerSyncFinishedS2CPayload::new, 6);
register(SYNC_CLEAR, PolymerSyncClearS2CPayload::new, 6);

SYNC_BLOCK_ID = registerList(SYNC_BLOCK, PolymerBlockEntry.CODEC,6);
SYNC_BLOCK_ID = registerList(SYNC_BLOCK, PolymerBlockEntry.CODEC,6, 7);
SYNC_BLOCKSTATE_ID = registerList(SYNC_BLOCKSTATE, PolymerBlockStateEntry.CODEC, 6);
SYNC_ITEM_ID = registerList(SYNC_ITEM, PolymerItemEntry.CODEC, 6);
SYNC_ENTITY_ID = registerList(SYNC_ENTITY, PolymerEntityEntry.CODEC,6);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
Expand All @@ -17,22 +18,31 @@


@ApiStatus.Internal
public record PolymerBlockEntry(Identifier identifier, int numId, Text text, BlockState visual) {
public record PolymerBlockEntry(Identifier identifier, int numId, Text text, BlockState visual, ItemStack visualStack) {
private static final PacketCodec<ByteBuf, BlockState> STATE = PacketCodecs.entryOf(Block.STATE_IDS);
public static final PacketCodec<ContextByteBuf, PolymerBlockEntry> CODEC = PacketCodec.of(PolymerBlockEntry::write, PolymerBlockEntry::read);
public void write(PacketByteBuf buf) {
public void write(ContextByteBuf buf) {
buf.writeIdentifier(identifier);
buf.writeVarInt(numId);
TextCodecs.PACKET_CODEC.encode(buf, text);
STATE.encode(buf, visual);
if (buf.version() >= 7) {
ItemStack.OPTIONAL_PACKET_CODEC.encode(buf, this.visualStack);
}
}

public static PolymerBlockEntry of(Block block) {
return new PolymerBlockEntry(Registries.BLOCK.getId(block), Registries.BLOCK.getRawId(block),
block.getName(), block.getDefaultState());
block.getName(), block.getDefaultState(), block.asItem() != null ? block.asItem().getDefaultStack() : ItemStack.EMPTY);
}

public static PolymerBlockEntry read(PacketByteBuf buf) {
return new PolymerBlockEntry(buf.readIdentifier(), buf.readVarInt(), TextCodecs.PACKET_CODEC.decode(buf), Block.getStateFromRawId(buf.readVarInt()));
public static PolymerBlockEntry read(ContextByteBuf buf) {
var id = buf.readIdentifier();
var numId = buf.readVarInt();
var name = TextCodecs.PACKET_CODEC.decode(buf);
var visual = STATE.decode(buf);
var visualStack = buf.version() >= 7 ? ItemStack.OPTIONAL_PACKET_CODEC.decode(buf)
: (visual.getBlock().asItem() != null ? visual.getBlock().asItem().getDefaultStack() : ItemStack.EMPTY);
return new PolymerBlockEntry(id, numId, name, visual, visualStack);
}
}

0 comments on commit 216a938

Please sign in to comment.