Skip to content

Commit

Permalink
Some bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 14, 2024
1 parent eb7b7ac commit 35dca91
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 27 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.4
mod_version = 0.9.5

minecraft_version_supported = ">=1.20.5-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class AutoHost implements ModInitializer {
public static void init(MinecraftServer server) {
var config = CommonImpl.loadConfig("auto-host", AutoHostConfig.class);
AutoHost.config = config;
AutoHost.config.enabled |= Files.exists(FabricLoader.getInstance().getGameDir().resolve("polymer/.force_autohost"));

if (!config.enabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public void serverStarted(MinecraftServer minecraftServer) {

@Nullable
public void serverStopped(MinecraftServer minecraftServer) {
server.stop(0);
if (server != null) {
server.stop(0);
}
}

protected boolean updateHash() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.authlib.GameProfile;
import eu.pb4.polymer.autohost.impl.AutoHost;
import eu.pb4.polymer.autohost.impl.AutoHostTask;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
Expand Down Expand Up @@ -34,9 +35,11 @@ public ServerConfigurationNetworkHandlerMixin(MinecraftServer server, ClientConn

@Shadow protected abstract void onTaskFinished(ServerPlayerConfigurationTask.Key key);

@Shadow protected abstract GameProfile getProfile();

@Inject(method = "queueSendResourcePackTask", at = @At("TAIL"))
private void polymerAutoHost$addTask(CallbackInfo ci) {
if (AutoHost.config.enabled) {
if (AutoHost.config.enabled && !this.server.isHost(this.getProfile())) {
var x = new ArrayList<MinecraftServer.ServerResourcePackProperties>();
var ready = AutoHost.provider.isReady();
if (ready) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static boolean isPolymerServerItem(ItemStack itemStack, @Nullable ServerP
return true;
} else if (x.getValue() != null && x.getValue().isPresent()
&& x.getValue().get() instanceof TransformingComponent t
&& t.polymer$requireModification(player)) {
&& t.polymer$requireModification(ctx)) {
return true;
}
}
Expand Down Expand Up @@ -417,13 +417,18 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
}
}

var ctx = PacketContext.get();
if (player != null && ctx.getPlayer() != player) {
ctx = PacketContext.of(player);
}

for (var i = 0; i < COMPONENTS_TO_COPY.length; i++) {
var key = COMPONENTS_TO_COPY[i];
var x = itemStack.get(key);

if (x instanceof TransformingComponent t) {
//noinspection unchecked,rawtypes
out.set((ComponentType) key, t.polymer$getTransformed(player));
out.set((ComponentType) key, t.polymer$getTransformed(ctx));
} else {
//noinspection unchecked,rawtypes
out.set((ComponentType) key, (Object) itemStack.get(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.pb4.polymer.rsm.api.RegistrySyncUtils;
import net.minecraft.component.ComponentType;
import net.minecraft.registry.Registries;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

public interface PolymerComponent extends PolymerObject {
Expand All @@ -26,7 +27,7 @@ static boolean isPolymerComponent(ComponentType<?> type) {
return PolymerComponentImpl.UNSYNCED_COMPONENTS.contains(type);
}

static boolean canSync(ComponentType<?> key, Object entry, PacketContext context) {
static boolean canSync(ComponentType<?> key, @Nullable Object entry, PacketContext context) {
if (entry instanceof PolymerComponent component && component.canSyncRawToClient(context)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.minecraft.state.property.Property;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedWriter;
Expand Down Expand Up @@ -123,7 +124,14 @@ public static String dumpRegistry() {
msg.accept("");
msg.accept("= Polymer Starts: " + PolymerImplUtils.getBlockStateOffset());
msg.accept("");
msg.accept("= Vanilla ChunkDeltaUpdateS2CPacket broken: " + (PolymerImplUtils.getBlockStateOffset() >= 524288));
msg.accept("= All States: " + Block.STATE_IDS.size());

//noinspection unchecked
var pl = (PolymerIdList<BlockState>) Block.STATE_IDS;
msg.accept("= Polymer States: " + pl.polymer$getPolymerEntries().size());
msg.accept("= Server Bits: " + MathHelper.ceilLog2(Block.STATE_IDS.size()));
msg.accept("= Vanilla Bits: " + pl.polymer$getVanillaBitCount());
msg.accept("= NonPolymer Bits: " + pl.polymer$getNonPolymerBitCount());
msg.accept("");

for (var state : Block.STATE_IDS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package eu.pb4.polymer.core.impl;

import net.minecraft.server.network.ServerPlayerEntity;
import xyz.nucleoid.packettweaker.PacketContext;

public interface TransformingComponent {
Object polymer$getTransformed(ServerPlayerEntity player);
boolean polymer$requireModification(ServerPlayerEntity player);
Object polymer$getTransformed(PacketContext context);
boolean polymer$requireModification(PacketContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,8 +19,8 @@ public class AttributeModifiersComponentMixin implements TransformingComponent {
@Shadow @Final private boolean showInTooltip;

@Override
public Object polymer$getTransformed(ServerPlayerEntity player) {
if (!polymer$requireModification(player)) {
public Object polymer$getTransformed(PacketContext context) {
if (!polymer$requireModification(context)) {
return this;
}
var list = new ArrayList<AttributeModifiersComponent.Entry>();
Expand All @@ -33,7 +34,7 @@ public class AttributeModifiersComponentMixin implements TransformingComponent {
}

@Override
public boolean polymer$requireModification(ServerPlayerEntity player) {
public boolean polymer$requireModification(PacketContext context) {
for (var x : this.modifiers) {
if (PolymerEntityUtils.isPolymerEntityAttribute(x.attribute())) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package eu.pb4.polymer.core.mixin.item.component;

import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.impl.TransformingComponent;
import net.minecraft.block.Block;
import net.minecraft.component.type.DebugStickStateComponent;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.Property;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.Map;

@Mixin(DebugStickStateComponent.class)
public class DebugStickStateComponentMixin implements TransformingComponent {
@Shadow @Final private Map<RegistryEntry<Block>, Property<?>> properties;

@Override
public Object polymer$getTransformed(PacketContext context) {
if (polymer$requireModification(context)) {
return DebugStickStateComponent.DEFAULT;
}
return this;
}

@Override
public boolean polymer$requireModification(PacketContext context) {
for (var key : this.properties.keySet()) {
if (!PolymerSyncedObject.canSyncRawToClient(Registries.BLOCK, key.value(), context.getPlayer())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.List;
import java.util.Optional;
Expand All @@ -27,16 +28,16 @@ public abstract class FoodComponentMixin implements TransformingComponent {
@Shadow public abstract Optional<ItemStack> usingConvertsTo();

@Override
public Object polymer$getTransformed(ServerPlayerEntity player) {
if (!polymer$requireModification(player)) {
public Object polymer$getTransformed(PacketContext context) {
if (!polymer$requireModification(context)) {
return this;
}

return new FoodComponent(this.nutrition, this.saturation, this.canAlwaysEat, this.eatSeconds, this.usingConvertsTo(), List.of());
}

@Override
public boolean polymer$requireModification(ServerPlayerEntity player) {
public boolean polymer$requireModification(PacketContext context) {
for (var effect : this.effects) {
if (effect.effect().getEffectType().value() instanceof PolymerObject) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.List;
import java.util.Optional;
Expand All @@ -23,16 +24,16 @@ public abstract class PotionContentsComponentMixin implements TransformingCompon
@Shadow public abstract int getColor();

@Override
public Object polymer$getTransformed(ServerPlayerEntity player) {
if (!polymer$requireModification(player)) {
public Object polymer$getTransformed(PacketContext context) {
if (!polymer$requireModification(context)) {
return this;
}

return new PotionContentsComponent(Optional.empty(), Optional.of(this.getColor()), List.of());
}

@Override
public boolean polymer$requireModification(ServerPlayerEntity player) {
public boolean polymer$requireModification(PacketContext context) {
if (this.potion.isPresent() && this.potion.get().value() instanceof PolymerObject) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.List;

Expand All @@ -16,16 +17,16 @@ public abstract class SuspiciousStewComponentMixin implements TransformingCompon
@Shadow @Final private List<SuspiciousStewEffectsComponent.StewEffect> effects;

@Override
public Object polymer$getTransformed(ServerPlayerEntity player) {
if (!polymer$requireModification(player)) {
public Object polymer$getTransformed(PacketContext context) {
if (!polymer$requireModification(context)) {
return this;
}

return new SuspiciousStewEffectsComponent(List.of());
}

@Override
public boolean polymer$requireModification(ServerPlayerEntity player) {
public boolean polymer$requireModification(PacketContext context) {
for (var effect : this.effects) {
if (effect.effect().value() instanceof PolymerObject) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package eu.pb4.polymer.core.mixin.other;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.serialization.Codec;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.core.api.other.PolymerComponent;
import eu.pb4.polymer.core.impl.TransformingComponent;
import eu.pb4.polymer.core.impl.networking.TransformingPacketCodec;
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentType;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.function.Function;

@Mixin(ComponentChanges.class)
public class ComponentChangesMixin {
@Mutable
@Shadow @Final public static PacketCodec<RegistryByteBuf, ComponentChanges> PACKET_CODEC;

@ModifyExpressionValue(method = "<clinit>", at = @At(value = "INVOKE", target = "Lcom/mojang/serialization/Codec;xmap(Ljava/util/function/Function;Ljava/util/function/Function;)Lcom/mojang/serialization/Codec;"))
private static Codec<ComponentChanges> patchCodec(Codec<ComponentChanges> codec) {
return codec.xmap(Function.identity(), content -> { // Encode
if (PolymerCommonUtils.isServerNetworkingThread()) {
return transformContent(content);
}
return content;
});
}

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void patchNetCodec(CallbackInfo ci) {
PACKET_CODEC = TransformingPacketCodec.encodeOnly(PACKET_CODEC, ((byteBuf, content) -> transformContent(content)));
}

@Unique
private static ComponentChanges transformContent(ComponentChanges content) {
var player = PacketContext.get();
var builder = ComponentChanges.builder();
for (var entry : content.entrySet()) {
if (!PolymerComponent.canSync(entry.getKey(), entry.getValue().orElse(null), player)) {
continue;
} else if (entry.getValue().isPresent() && entry.getValue().get() instanceof TransformingComponent t) {
//noinspection unchecked
builder.add((ComponentType<Object>) entry.getKey(), t.polymer$getTransformed(player));
}

if (entry.getValue().isPresent()) {
//noinspection unchecked
builder.add((ComponentType<Object>) entry.getKey(), entry.getValue().get());
} else {
builder.remove(entry.getKey());
}
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import eu.pb4.polymer.core.api.other.PolymerComponent;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.TransformingComponent;
import net.minecraft.component.ComponentMap;
import net.minecraft.component.ComponentType;
import net.minecraft.text.HoverEvent;
Expand All @@ -34,7 +35,9 @@ private static Codec<Map<ComponentType<?>, Object>> patchCodec(Codec<Map<Compone
var map = new IdentityHashMap<ComponentType<?>, Object>();
for (var key : content.keySet()) {
var entry = content.get(key);
if (PolymerComponent.canSync(key, entry, player)) {
if (entry instanceof TransformingComponent t) {
map.put(key, t.polymer$getTransformed(player));
} else if (PolymerComponent.canSync(key, entry, player)) {
map.put(key, entry);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private static Codec<HoverEvent> patchCodec(Codec<HoverEvent> codec) {
} else if (content.getAction() == HoverEvent.Action.SHOW_ENTITY) {
var val = Objects.requireNonNull(content.getValue(HoverEvent.Action.SHOW_ENTITY));
if (PolymerEntityUtils.isPolymerEntityType(val.entityType)) {
val = new HoverEvent.EntityContent(val.entityType, val.uuid, val.name);
return new HoverEvent(HoverEvent.Action.SHOW_TEXT, Texts.join(val.asTooltip(), Text.literal("\n")));
}
}
Expand Down
2 changes: 2 additions & 0 deletions polymer-core/src/main/resources/polymer-core.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"entity.PlayerListS2CPacketAccessor",
"entity.RemoveEntityStatusEffectS2CPacketMixin",
"entity.ServerWorldAccessor",
"item.component.DebugStickStateComponentMixin",
"item.ItemGroupMixin",
"item.ItemGroupsMixin",
"item.ItemStackMixin",
Expand All @@ -70,6 +71,7 @@
"item.packet.SynchronizeRecipesS2CPacketMixin",
"item.packet.TradedItemMixin",
"other.CodecCacheMixin",
"other.ComponentChangesMixin",
"other.ComponentMapMixin",
"other.FluidMixin",
"other.HoverEventMixin",
Expand Down
Loading

0 comments on commit 35dca91

Please sign in to comment.