Skip to content

Commit

Permalink
Some fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 5, 2024
1 parent 229ccbb commit 4071734
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fabric_version=0.100.0+1.21

maven_group = eu.pb4

mod_version = 0.9.2
mod_version = 0.9.3

minecraft_version_supported = ">=1.20.5-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class DefaultModelData {
{
var plant = new ArrayList<BlockState>();

for (var block : new Block[]{Blocks.OAK_SAPLING, Blocks.BIRCH_SAPLING, Blocks.SPRUCE_SAPLING, Blocks.JUNGLE_SAPLING, Blocks.ACACIA_SAPLING, Blocks.DARK_OAK_SAPLING}) {
for (var block : new Block[]{Blocks.OAK_SAPLING, Blocks.BIRCH_SAPLING, Blocks.SPRUCE_SAPLING, Blocks.JUNGLE_SAPLING, Blocks.ACACIA_SAPLING, Blocks.DARK_OAK_SAPLING, Blocks.CHERRY_SAPLING}) {
var id = Registries.BLOCK.getId(block);

var model = new PolymerBlockModel[]{PolymerBlockModel.of(Identifier.of(id.getNamespace() + ":block/" + id.getPath()))};
Expand Down
2 changes: 1 addition & 1 deletion polymer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {

//modRuntime "supercoder79:databreaker:0.2.7"

modCompileOnly('maven.modrinth:lithium:mc1.20.2-0.12.0')
modCompileOnly('maven.modrinth:lithium:mc1.21-0.12.7')

//modCompileOnly ('nl.theepicblock:PolyMc:5.1.0+1.19')
modCompileOnly('com.github.TheEpicBlock:PolyMc:5.6.1') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.pb4.polymer.core.api.item;

import com.google.common.collect.ImmutableRangeSet;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Dynamic;
Expand All @@ -16,13 +17,17 @@
import eu.pb4.polymer.core.impl.TransformingComponent;
import eu.pb4.polymer.core.impl.compat.polymc.PolyMcUtils;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.rsm.api.RegistrySyncUtils;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.component.ComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.EnchantmentEffectComponentTypes;
import net.minecraft.component.type.*;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.DefaultAttributeRegistry;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.BlockPredicatesChecker;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -34,6 +39,7 @@
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
Expand All @@ -43,9 +49,7 @@
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Predicate;

public final class PolymerItemUtils {
Expand Down Expand Up @@ -119,6 +123,7 @@ public final class PolymerItemUtils {
HideableTooltip.of(DataComponentTypes.CAN_PLACE_ON, BlockPredicatesChecker::withShowInTooltip),
HideableTooltip.of(DataComponentTypes.JUKEBOX_PLAYABLE, JukeboxPlayableComponent::withShowInTooltip)
);
private static final Set<ArmorMaterial> ARMOR_MATERIALS = new ReferenceOpenHashSet<>();

private PolymerItemUtils() {
}
Expand Down Expand Up @@ -244,7 +249,6 @@ public static Map<Identifier, NbtElement> getServerComponents(ItemStack stack) {
@Nullable
public static Map<Identifier, NbtElement> getPolymerComponents(ItemStack stack) {
return getPolymerComponents(stack.get(DataComponentTypes.CUSTOM_DATA));

}

@Nullable
Expand Down Expand Up @@ -436,7 +440,7 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
} else {
out.set(DataComponentTypes.CUSTOM_DATA, comp);
}
});
}, PolymerCommonUtils::executeWithoutNetworkingLogic);
} catch (Throwable e) {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(RegistryOps.of(NbtOps.INSTANCE, lookup), POLYMER_STACK_ID_CODEC, Registries.ITEM.getId(itemStack.getItem())).getOrThrow());
}
Expand Down Expand Up @@ -534,6 +538,29 @@ public static ItemWithMetadata getItemSafely(PolymerItem item, ItemStack stack,
return getItemSafely(item, stack, player, PolymerBlockUtils.NESTED_DEFAULT_DISTANCE);
}

/**
* Marks BlockEntity type as server-side only
*
* @param types BlockEntityTypes
*/
@SafeVarargs
public static void registerArmorMaterial(RegistryEntry<ArmorMaterial>... types) {
for (var type : types) {
RegistrySyncUtils.setServerEntry(Registries.ARMOR_MATERIAL, type.value());
ARMOR_MATERIALS.add(type.value());

}
}

/**
* Checks if BlockEntity is server-side only
*
* @param type BlockEntities type
*/
public static boolean isPolymerArmorMaterial(RegistryEntry<ArmorMaterial> type) {
return ARMOR_MATERIALS.contains(type.value());
}

/**
* @deprecated Use {@link PolymerComponent#registerDataComponent(ComponentType[])} instead
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.pb4.polymer.core.api.utils;

import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;

/**
Expand Down Expand Up @@ -31,7 +32,12 @@ default boolean canSyncRawToClient(ServerPlayerEntity player) {
return false;
}

@Deprecated
static boolean canSyncRawToClient(Object obj, ServerPlayerEntity player) {
return obj instanceof PolymerSyncedObject pol ? pol.canSyncRawToClient(player) : !(obj instanceof PolymerObject);
return obj instanceof PolymerSyncedObject<?> pol ? pol.canSyncRawToClient(player) : !PolymerUtils.isServerOnly(obj);
}

static <T> boolean canSyncRawToClient(Registry<T> registry, T obj, ServerPlayerEntity player) {
return obj instanceof PolymerSyncedObject<?> pol ? pol.canSyncRawToClient(player) : !PolymerUtils.isServerOnly(registry, obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import eu.pb4.polymer.core.api.other.PolymerComponent;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.interfaces.PolymerPlayNetworkHandlerExtension;
import eu.pb4.polymer.core.impl.networking.PacketPatcher;
import eu.pb4.polymer.core.mixin.block.packet.ServerChunkLoadingManagerAccessor;
import eu.pb4.polymer.core.mixin.entity.ServerWorldAccessor;
import eu.pb4.polymer.rsm.api.RegistrySyncUtils;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.component.ComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.InventoryS2CPacket;
import net.minecraft.potion.Potion;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.resource.featuretoggle.FeatureFlag;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerPlayNetworkHandler;
Expand Down Expand Up @@ -202,8 +208,20 @@ public static Path getClientJar() {
return PolymerCommonUtils.getClientJar();
}

@SuppressWarnings("unchecked")
public static boolean isServerOnly(Object obj) {
return obj instanceof PolymerObject || (obj instanceof ItemStack stack && PolymerItemUtils.isPolymerServerItem(stack)) || (obj instanceof EntityType<?> type && PolymerEntityUtils.isPolymerEntityType(type)) || (obj instanceof BlockEntityType<?> typeBE && PolymerBlockUtils.isPolymerBlockEntityType(typeBE)) || (obj instanceof VillagerProfession villagerProfession && PolymerEntityUtils.getPolymerProfession(villagerProfession) != null);
return obj instanceof PolymerObject
|| (obj instanceof ItemStack stack && PolymerItemUtils.isPolymerServerItem(stack))
|| (obj instanceof EntityType<?> type && PolymerEntityUtils.isPolymerEntityType(type))
|| (obj instanceof BlockEntityType<?> typeBE && PolymerBlockUtils.isPolymerBlockEntityType(typeBE))
|| (obj instanceof RegistryEntry<?> entry && (
(entry.value() instanceof ArmorMaterial && PolymerItemUtils.isPolymerArmorMaterial((RegistryEntry<ArmorMaterial>) entry))
|| (entry.value() instanceof EntityAttribute && PolymerEntityUtils.isPolymerEntityAttribute((RegistryEntry<EntityAttribute>) entry))))
|| (obj instanceof ComponentType<?> componentType && PolymerComponent.isPolymerComponent(componentType))
|| (obj instanceof VillagerProfession villagerProfession && PolymerEntityUtils.getPolymerProfession(villagerProfession) != null);
}
public static <T> boolean isServerOnly(Registry<T> registry, T obj) {
return RegistrySyncUtils.isServerEntry(registry, obj) || isServerOnly(obj);
}

public static boolean hasResourcePack(@Nullable ServerPlayerEntity player, UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package eu.pb4.polymer.core.mixin.block;

import eu.pb4.polymer.core.api.block.PolymerBlock;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.utils.PolymerObject;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.interfaces.PolymerIdList;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.s2c.play.WorldEventS2CPacket;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.collection.IdList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Block.class)
public class BlockMixin {
@Shadow
@Final
public static IdList<BlockState> STATE_IDS;

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void polymer$enableMapping(CallbackInfo ci) {
((PolymerIdList<BlockState>) STATE_IDS).polymer$setChecker(
x -> x.getBlock() instanceof PolymerObject,
x -> PolymerImplUtils.isServerSideSyncableEntry((Registry<Object>) (Object) Registries.BLOCK, x.getBlock()),
x -> "(Block) " + Registries.BLOCK.getId(x.getBlock())
);
}

@Inject(method = "spawnBreakParticles", at = @At("HEAD"))
private void addPolymerParticles(World world, PlayerEntity player, BlockPos pos, BlockState state, CallbackInfo ci) {
if (player instanceof ServerPlayerEntity serverPlayer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,46 @@

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.serialization.Codec;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.other.PolymerTooltipType;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.function.Supplier;


@Mixin(ItemStack.class)
public class ItemStackMixin {
@ModifyExpressionValue(method = "getTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/tooltip/TooltipType;isAdvanced()Z"))
private boolean removeAdvanced(boolean original, @Local(ordinal = 0) TooltipType type) {
private boolean removeAdvanced(boolean original, @Local(ordinal = 0, argsOnly = true) TooltipType type) {
return original && !(type instanceof PolymerTooltipType);
}

@ModifyArg(method = "<clinit>", at = @At(value = "INVOKE", target = "Lcom/mojang/serialization/Codec;lazyInitialized(Ljava/util/function/Supplier;)Lcom/mojang/serialization/Codec;"))
private static Supplier<Codec<ItemStack>> patchCodec(Supplier<Codec<ItemStack>> codec) {
return () -> codec.get().xmap(content -> { // Decode
if (PolymerCommonUtils.isServerNetworkingThread()) {
var context = PacketContext.get().getRegistryWrapperLookup();
var player = PolymerCommonUtils.getPlayerContext();
var lookup = context != null ? context : (player != null ? player.getRegistryManager() : PolymerImplUtils.FALLBACK_LOOKUP);
return PolymerItemUtils.getRealItemStack(content, lookup);
}
return content;
}, content -> { // Encode
if (PolymerCommonUtils.isServerNetworkingThread()) {
var context = PacketContext.get().getRegistryWrapperLookup();
var player = PolymerCommonUtils.getPlayerContext();
var lookup = context != null ? context : (player != null ? player.getRegistryManager() : PolymerImplUtils.FALLBACK_LOOKUP);
return PolymerItemUtils.getPolymerItemStack(content, lookup, player);
}
return content;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.pb4.polymer.core.mixin.other;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(targets = "net/minecraft/util/dynamic/CodecCache$2")
public class CodecCacheMixin {
@Shadow
@Final
Codec<Object> field_51505;
@Inject(method = "encode", at = @At("HEAD"), cancellable = true)
private <T> void dontCacheOnNetworking(Object value, DynamicOps<T> ops, T prefix, CallbackInfoReturnable<DataResult<T>> cir) {
if (PolymerCommonUtils.isServerNetworkingThread()) {
cir.setReturnValue(this.field_51505.encode(value, ops, prefix));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
import eu.pb4.polymer.core.api.utils.PolymerObject;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.interfaces.PolymerIdList;
import net.minecraft.Bootstrap;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.collection.IdList;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Bootstrap.class)
public class BootstrapMixin {
@Inject(method = "setOutputStreams", at = @At("HEAD"))
@Mixin(Fluid.class)
public class FluidMixin {
@Shadow @Final public static IdList<FluidState> STATE_IDS;

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void polymer$enableMapping(CallbackInfo ci) {
((PolymerIdList<BlockState>) Block.STATE_IDS).polymer$setChecker(
x -> x.getBlock() instanceof PolymerObject,
x -> PolymerImplUtils.isServerSideSyncableEntry((Registry<Object>) (Object) Registries.BLOCK, x.getBlock()),
x -> "(Block) " + Registries.BLOCK.getId(x.getBlock())
);
((PolymerIdList<FluidState>) Fluid.STATE_IDS).polymer$setChecker(
((PolymerIdList<FluidState>) STATE_IDS).polymer$setChecker(
x -> x.getFluid() instanceof PolymerObject,
x -> PolymerImplUtils.isServerSideSyncableEntry((Registry<Object>) (Object) Registries.FLUID, x.getFluid()),
x -> "(Fluid) " + Registries.FLUID.getId(x.getFluid()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public abstract class IdListMixin<T> implements PolymerIdList<T> {
}
}
this.polymer$nonPolymerBitCount = MathHelper.ceilLog2(this.list.size() - this.polymer$states.size());
this.polymer$vanillaEntryCount = MathHelper.ceilLog2(this.polymer$vanillaEntryCount);
this.polymer$vanillaBitCount = MathHelper.ceilLog2(this.polymer$vanillaEntryCount);
}
}

Expand Down Expand Up @@ -156,6 +156,7 @@ public abstract class IdListMixin<T> implements PolymerIdList<T> {
this.polymer$initLazy();
}

@Unique
private void polymer$initLazy() {
if (this.polymer$locked && this.polymer$initializeLazy) {
if (StackWalker.getInstance().walk(PolymerImplUtils::shouldSkipStateInitialization)) {
Expand All @@ -167,6 +168,7 @@ public abstract class IdListMixin<T> implements PolymerIdList<T> {
this.polymer$lazyList.forEach(this::add);
this.polymer$lazyList.clear();
this.polymer$nonPolymerBitCount = MathHelper.ceilLog2(this.list.size() - this.polymer$states.size());
this.polymer$vanillaBitCount = MathHelper.ceilLog2(this.polymer$vanillaEntryCount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class SynchronizeTagsS2CPacketMixin {
var list = new IntArrayList(entry.getValue().size());

for (int i : entry.getValue()) {
if (PolymerSyncedObject.canSyncRawToClient(reg.get(i), player)) {
//noinspection unchecked
if (PolymerSyncedObject.canSyncRawToClient(reg, reg.get(i), player)) {
list.add(i);
}
}
Expand Down
Loading

0 comments on commit 4071734

Please sign in to comment.