Skip to content

Commit

Permalink
Convert items and blocks to be overlay based
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Feb 8, 2025
1 parent e9fc529 commit 9f49d2b
Show file tree
Hide file tree
Showing 26 changed files with 102 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import eu.pb4.polymer.blocks.api.PolymerTexturedBlock;
import eu.pb4.polymer.core.api.block.BlockMapper;
import net.minecraft.block.Block;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import net.minecraft.block.BlockState;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.registry.Registries;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

Expand All @@ -23,7 +22,7 @@ public BlockExtBlockMapper(BlockMapper baseMapper) {

@Override
public BlockState toClientSideState(BlockState state, PacketContext player) {
if (state.getBlock() instanceof PolymerTexturedBlock) {
if (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, state.getBlock()) instanceof PolymerTexturedBlock) {
return this.baseMapper.toClientSideState(state, player);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.polymer.blocks.impl;

import eu.pb4.polymer.blocks.api.PolymerTexturedBlock;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import io.github.theepicblock.polymc.api.PolyMcEntrypoint;
import io.github.theepicblock.polymc.api.PolyRegistry;
import io.github.theepicblock.polymc.api.resource.ModdedResources;
Expand All @@ -14,7 +15,7 @@ public class PolyMcResourceEntrypoint implements PolyMcEntrypoint {
@Override
public void registerPolys(PolyRegistry registry) {
for (var block : Registries.BLOCK) {
if (block instanceof PolymerTexturedBlock) {
if (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, block) instanceof PolymerTexturedBlock) {
registry.registerBlockPoly(block, new PolymerTextureBlockPoly());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eu.pb4.polymer.common.api.events.SimpleEvent;
import eu.pb4.polymer.common.impl.CommonImplUtils;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.impl.compat.polymc.PolyMcUtils;
import eu.pb4.polymer.core.impl.interfaces.BlockStateExtra;
import eu.pb4.polymer.core.impl.networking.PacketPatcher;
Expand Down Expand Up @@ -34,7 +35,7 @@

public final class PolymerBlockUtils {
public static final int NESTED_DEFAULT_DISTANCE = 32;
public static final Predicate<BlockState> IS_POLYMER_BLOCK_STATE_PREDICATE = state -> state.getBlock() instanceof PolymerBlock;
public static final Predicate<BlockState> IS_POLYMER_BLOCK_STATE_PREDICATE = state -> PolymerSyncedObject.getSyncedObject(Registries.BLOCK, state.getBlock()) instanceof PolymerBlock;
/**
* This event allows you to force server side mining for any block/item
*/
Expand Down Expand Up @@ -80,7 +81,7 @@ public static boolean isPolymerBlockEntityType(BlockEntityType<?> type) {
* @return
*/
public static boolean forceLightUpdates(BlockState blockState) {
if (blockState.getBlock() instanceof PolymerBlock virtualBlock) {
if (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, blockState.getBlock()) instanceof PolymerBlock virtualBlock) {
if (virtualBlock.forceLightUpdates(blockState)) {
return true;
}
Expand Down Expand Up @@ -119,7 +120,7 @@ public static BlockState getBlockStateSafely(PolymerBlock block, BlockState bloc
BlockState out = block.getPolymerBlockState(blockState, context);

int req = 0;
while (out.getBlock() instanceof PolymerBlock newBlock && newBlock != block && req < maxDistance) {
while (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, out.getBlock()) instanceof PolymerBlock newBlock && newBlock != block && req < maxDistance) {
out = newBlock.getPolymerBlockState(out, context);
req++;
}
Expand All @@ -130,7 +131,7 @@ public static BlockState getBlockBreakBlockStateSafely(PolymerBlock block, Block
BlockState out = block.getPolymerBreakEventBlockState(blockState, context);

int req = 0;
while (out.getBlock() instanceof PolymerBlock newBlock && newBlock != block && req < maxDistance) {
while (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, out.getBlock()) instanceof PolymerBlock newBlock && newBlock != block && req < maxDistance) {
out = newBlock.getPolymerBreakEventBlockState(blockState, context);
req++;
}
Expand All @@ -155,8 +156,8 @@ public static BlockEntityUpdateS2CPacket createBlockEntityPacket(BlockPos pos, B
}

public static boolean shouldMineServerSide(ServerPlayerEntity player, BlockPos pos, BlockState state) {
return (state.getBlock() instanceof PolymerBlock block && block.handleMiningOnServer(player.getMainHandStack(), state, pos, player))
|| (player.getMainHandStack().getItem() instanceof PolymerItem item && item.handleMiningOnServer(player.getMainHandStack(), state, pos, player))
return (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, state.getBlock()) instanceof PolymerBlock block && block.handleMiningOnServer(player.getMainHandStack(), state, pos, player))
|| (PolymerSyncedObject.getSyncedObject(Registries.ITEM, player.getMainHandStack().getItem()) instanceof PolymerItem item && item.handleMiningOnServer(player.getMainHandStack(), state, pos, player))
|| PolymerBlockUtils.SERVER_SIDE_MINING_CHECK.invoke((x) -> x.onBlockMine(state, pos, player));
}

Expand All @@ -170,9 +171,9 @@ public static NbtCompound transformBlockEntityNbt(PacketContext context, BlockEn

public static boolean isPolymerBlockInteraction(ServerPlayerEntity player, ItemStack stack, Hand hand, BlockHitResult blockHitResult, ServerWorld world, ActionResult actionResult) {
var blockState = world.getBlockState(blockHitResult.getBlockPos());
if (blockState.getBlock() instanceof PolymerBlock polymerBlock && polymerBlock.isPolymerBlockInteraction(blockState, player, hand, stack, world, blockHitResult, actionResult)) {
if (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, blockState.getBlock()) instanceof PolymerBlock polymerBlock && polymerBlock.isPolymerBlockInteraction(blockState, player, hand, stack, world, blockHitResult, actionResult)) {
return true;
} else if (stack.getItem() instanceof PolymerItem polymerItem && polymerItem.isPolymerBlockInteraction(blockState, player, hand, stack, world, blockHitResult, actionResult)) {
} else if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, stack.getItem()) instanceof PolymerItem polymerItem && polymerItem.isPolymerBlockInteraction(blockState, player, hand, stack, world, blockHitResult, actionResult)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eu.pb4.polymer.common.impl.CommonImplUtils;
import eu.pb4.polymer.common.impl.entity.InternalEntityHelpers;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.impl.interfaces.EntityAttachedPacket;
import eu.pb4.polymer.core.impl.networking.PolymerServerProtocol;
import eu.pb4.polymer.core.mixin.block.packet.ServerChunkLoadingManagerAccessor;
Expand Down Expand Up @@ -192,7 +193,7 @@ public static void refreshEntity(Entity entity) {
public static boolean isPolymerEntityInteraction(ServerPlayerEntity player, Hand hand, ItemStack stack, ServerWorld world, Entity entity, ActionResult actionResult) {
if (entity instanceof PolymerEntity polymerEntity && polymerEntity.isPolymerEntityInteraction(player, hand, stack, world, actionResult)) {
return true;
} else if (stack.getItem() instanceof PolymerItem polymerItem && polymerItem.isPolymerEntityInteraction(player, hand, stack, world, entity, actionResult)) {
} else if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, stack.getItem()) instanceof PolymerItem polymerItem && polymerItem.isPolymerEntityInteraction(player, hand, stack, world, entity, actionResult)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import eu.pb4.polymer.core.api.other.PolymerComponent;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.TransformingComponent;
Expand Down Expand Up @@ -169,7 +170,7 @@ public static ItemStack getPolymerItemStack(ItemStack itemStack, PacketContext c
public static ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipContext, PacketContext context) {
if (getPolymerIdentifier(itemStack) != null) {
return itemStack;
} else if (itemStack.getItem() instanceof PolymerItem item) {
} else if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, itemStack.getItem()) instanceof PolymerItem item) {
return item.getPolymerItemStack(itemStack, tooltipContext, context);
} else if (isPolymerServerItem(itemStack, context)) {
return createItemStack(itemStack, tooltipContext, context);
Expand Down Expand Up @@ -308,7 +309,7 @@ public static boolean isPolymerServerItem(ItemStack itemStack, PacketContext con
if (getPolymerIdentifier(itemStack) != null) {
return false;
}
if (itemStack.getItem() instanceof PolymerItem) {
if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, itemStack.getItem()) instanceof PolymerItem) {
return true;
}

Expand Down Expand Up @@ -363,7 +364,7 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
Item item = itemStack.getItem();
Identifier model = null;
boolean storeCount;
if (itemStack.getItem() instanceof PolymerItem virtualItem) {
if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, itemStack.getItem()) instanceof PolymerItem virtualItem) {
var data = PolymerItemUtils.getItemSafely(virtualItem, itemStack, context);
item = data.item();
storeCount = virtualItem.shouldStorePolymerItemStackCount();
Expand Down Expand Up @@ -397,7 +398,7 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
}
}

if (itemStack.getItem() instanceof PolymerItem polymerItem) {
if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, itemStack.getItem()) instanceof PolymerItem polymerItem) {
polymerItem.modifyBasePolymerItemStack(out, itemStack, context);
}

Expand Down Expand Up @@ -466,8 +467,8 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
if (!tooltip.isEmpty()) {
tooltip.removeFirst();

if (itemStack.getItem() instanceof PolymerItem) {
((PolymerItem) itemStack.getItem()).modifyClientTooltip(tooltip, itemStack, context);
if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, itemStack.getItem()) instanceof PolymerItem polymerItem) {
polymerItem.modifyClientTooltip(tooltip, itemStack, context);
}
if (!tooltip.isEmpty()) {
var lore = new ArrayList<Text>();
Expand Down Expand Up @@ -511,7 +512,7 @@ public static ItemWithMetadata getItemSafely(PolymerItem item, ItemStack stack,
PolymerItem lastVirtual = item;

int req = 0;
while (out instanceof PolymerItem newItem && newItem != item && req < maxDistance) {
while (PolymerSyncedObject.getSyncedObject(Registries.ITEM, out) instanceof PolymerItem newItem && newItem != item && req < maxDistance) {
out = newItem.getPolymerItem(stack, context);
lastVirtual = newItem;
req++;
Expand Down Expand Up @@ -540,7 +541,7 @@ public static ItemStack getClientItemStack(ItemStack stack, PacketContext contex
}

public static boolean isPolymerItemInteraction(ServerPlayerEntity player, ItemStack stack, Hand hand, ServerWorld world, ActionResult actionResult) {
if (stack.getItem() instanceof PolymerItem polymerItem && polymerItem.isPolymerItemInteraction(player, hand, stack, world, actionResult)) {
if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, stack.getItem()) instanceof PolymerItem polymerItem && polymerItem.isPolymerItemInteraction(player, hand, stack, world, actionResult)) {
return true;
}
return POLYMER_ITEM_INTERACTION_CHECK.invoke((x) -> x.isPolymerItemInteraction(player, hand, stack, world, actionResult));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;
Expand All @@ -18,33 +19,22 @@
public class PolymerSoundEvent implements PolymerSyncedObject<SoundEvent> {
@Nullable
protected final SoundEvent polymerSound;

@Nullable
protected final UUID source;
private final SoundEvent self;

public static PolymerSoundEvent of(Identifier identifier, @Nullable SoundEvent vanillaEvent) {
return new PolymerSoundEvent(null, identifier, 16.0F, false, vanillaEvent);
}

static PolymerSoundEvent of(Identifier identifier, float distanceToTravel, @Nullable SoundEvent vanillaEvent) {
return new PolymerSoundEvent(null, identifier, distanceToTravel, true, vanillaEvent);
}

public static PolymerSoundEvent of(UUID uuid, Identifier identifier, @Nullable SoundEvent vanillaEvent) {
return new PolymerSoundEvent(uuid, identifier, 16.0F, false, vanillaEvent);
}

static PolymerSoundEvent of(UUID uuid, Identifier identifier, float distanceToTravel, @Nullable SoundEvent vanillaEvent) {
return new PolymerSoundEvent(uuid, identifier, distanceToTravel, true, vanillaEvent);
public static PolymerSoundEvent of(Identifier identifier, SoundEvent self, @Nullable SoundEvent vanillaEvent) {
return new PolymerSoundEvent(null, self, vanillaEvent);
}

public PolymerSoundEvent(@Nullable UUID uuid, Identifier id, float distanceToTravel, boolean useStaticDistance, @Nullable SoundEvent vanillaEvent) {
public PolymerSoundEvent(@Nullable UUID uuid, SoundEvent self, @Nullable SoundEvent vanillaEvent) {
this.source = uuid;
this.self = self;
this.polymerSound = vanillaEvent;
}

@Override
public SoundEvent getPolymerReplacement(PacketContext context) {
return this.source == null || this.polymerSound == null || PolymerUtils.hasResourcePack(context.getPlayer(), this.source) ? this : (this.polymerSound instanceof PolymerSoundEvent pe ? pe.getPolymerReplacement(context) : this.polymerSound);
return this.source == null || this.polymerSound == null || PolymerUtils.hasResourcePack(context.getPlayer(), this.source) ? this.self : this.polymerSound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import eu.pb4.polymer.core.impl.interfaces.RegistryExtension;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

Expand Down Expand Up @@ -35,12 +34,18 @@ default boolean canSyncRawToClient(PacketContext context) {
}

static <T> boolean canSyncRawToClient(Registry<T> registry, T obj, PacketContext context) {
var pol = getSyncedObjectDefinition(registry, obj);
var pol = getSyncedObject(registry, obj);
return pol != null ? pol.canSyncRawToClient(context) : !PolymerUtils.isServerOnly(registry, obj);
}


static <T> void getSyncedObject(Registry<T> registry, T obj, PolymerSyncedObject<T> object) {
//noinspection unchecked
((RegistryExtension<T>) registry).polymer$setOverlay(obj, object);
}

@Nullable
static <T> PolymerSyncedObject<T> getSyncedObjectDefinition(@Nullable Registry<T> registry, T obj) {
static <T> PolymerSyncedObject<T> getSyncedObject(@Nullable Registry<T> registry, T obj) {
if (obj instanceof PolymerSyncedObject<?> instance) {
//noinspection unchecked
return (PolymerSyncedObject<T>) instance;
Expand All @@ -49,7 +54,7 @@ static <T> PolymerSyncedObject<T> getSyncedObjectDefinition(@Nullable Registry<T
}

static <T> boolean canSynchronizeToPolymerClient(Registry<T> registry, T entry, PacketContext.NotNullWithPlayer ctx) {
var obj = getSyncedObjectDefinition(registry, entry);
var obj = getSyncedObject(registry, entry);
return obj == null || obj.canSynchronizeToPolymerClient(ctx);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package eu.pb4.polymer.core.impl;

import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.core.api.block.PolymerBlock;
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.core.impl.client.InternalClientRegistry;
import eu.pb4.polymer.core.impl.compat.ServerTranslationUtils;
Expand All @@ -19,21 +19,14 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.UpdateSelectedSlotS2CPacket;
import net.minecraft.registry.*;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.Property;
import net.minecraft.util.Identifier;
import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;
Expand Down Expand Up @@ -133,7 +126,7 @@ public static String dumpRegistry() {
msg.accept("");

for (var state : Block.STATE_IDS) {
msg.accept(Block.STATE_IDS.getRawId(state) + " | " + getAsString(state) + " | Polymer? " + (state.getBlock() instanceof PolymerBlock));
msg.accept(Block.STATE_IDS.getRawId(state) + " | " + getAsString(state) + " | Polymer? " + (PolymerSyncedObject.getSyncedObject(Registries.BLOCK, state.getBlock())));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.pb4.polymer.core.impl.compat.polymc;

import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import io.github.theepicblock.polymc.api.PolyRegistry;
import net.minecraft.registry.Registries;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -17,7 +17,7 @@ public void registerPolys(PolyRegistry registry) {
}

for (var item : Registries.ITEM) {
if (item instanceof PolymerItem) {
if (PolymerSyncedObject.getSyncedObject(Registries.ITEM, item) != null) {
registry.registerItemPoly(item, PassthroughPoly.ITEM);
}
}
Expand Down
Loading

0 comments on commit 9f49d2b

Please sign in to comment.