Skip to content

Commit

Permalink
Fix moving element holders not working when pushed with slime blocks,…
Browse files Browse the repository at this point in the history
… add method to modify attribute data sent bt entity, /polymer generate-pack reload command
  • Loading branch information
Patbox committed Sep 6, 2024
1 parent c3584df commit 667c73a
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 88 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.102.1+1.21.1

maven_group = eu.pb4

mod_version = 0.9.12
mod_version = 0.9.13

minecraft_version_supported = ">=1.21-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
import com.mojang.brigadier.arguments.BoolArgumentType;
import eu.pb4.polymer.autohost.api.ResourcePackDataProvider;
import eu.pb4.polymer.autohost.impl.providers.AbstractProvider;
import eu.pb4.polymer.autohost.impl.providers.NettyProvider;
import eu.pb4.polymer.autohost.impl.providers.EmptyProvider;
import eu.pb4.polymer.autohost.impl.providers.NettyProvider;
import eu.pb4.polymer.autohost.impl.providers.StandaloneWebServerProvider;
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.common.impl.CommonImplUtils;
import eu.pb4.polymer.common.impl.CommonNetworkHandlerExt;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.resourcepack.impl.PolymerResourcePackMod;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
Expand All @@ -32,15 +28,12 @@

public class AutoHost implements ModInitializer {
public static final Map<Identifier, Supplier<ResourcePackDataProvider>> TYPES = new HashMap<>();
public static final List<MinecraftServer.ServerResourcePackProperties> GLOBAL_RESOURCE_PACKS = new ArrayList<>();
public static AutoHostConfig config = new AutoHostConfig();
public static Text message = Text.empty();
public static Text disconnectMessage = Text.empty();

public static ResourcePackDataProvider provider = EmptyProvider.INSTANCE;

public static final List<MinecraftServer.ServerResourcePackProperties> GLOBAL_RESOURCE_PACKS = new ArrayList<>();


public static void init(MinecraftServer server) {
var config = CommonImpl.loadConfig("auto-host", AutoHostConfig.class);
AutoHost.config = config;
Expand Down Expand Up @@ -94,20 +87,9 @@ public static void end(MinecraftServer server) {
provider.serverStopped(server);
}

@Deprecated(forRemoval = true)
public static void generateAndCall(MinecraftServer server, Consumer<Text> messageConsumer, Runnable runnable) {
Util.getIoWorkerExecutor().execute(() -> {
messageConsumer.accept(Text.literal("[Polymer] Starting resource pack generation..."));
boolean success = PolymerResourcePackUtils.buildMain();

server.execute(() -> {
if (success) {
messageConsumer.accept(Text.literal("[Polymer] Resource pack created successfully!"));
runnable.run();
} else {
messageConsumer.accept(Text.literal("[Polymer] Found issues while creating resource pack! See logs above for more detail!"));
}
});
});
PolymerResourcePackMod.generateAndCall(server, true, messageConsumer, runnable);
}

public static File getFile(String path) {
Expand All @@ -133,25 +115,33 @@ public void onInitialize() {
ResourcePackDataProvider.register(Identifier.of("polymer", "standalone"), StandaloneWebServerProvider::new);
ResourcePackDataProvider.register(Identifier.of("polymer", "empty"), EmptyProvider::new);

CommonImplUtils.registerCommands((c) -> c.then(literal("generate-pack")
.requires(CommonImplUtils.permission("command.generate", 3))
.then(literal("reload").executes((ctx -> {
return PolymerResourcePackMod.generateResources(ctx, () -> {
if (!provider.isReady()) {
ctx.getSource().sendMessage(Text.literal("[Polymer] AutoHost module isn't configured/loaded correctly so resource pack won't be synced!"));
return;
}
for (var player : ctx.getSource().getServer().getPlayerManager().getPlayerList()) {
for (var x : provider.getProperties(((CommonNetworkHandlerExt) player.networkHandler).polymerCommon$getConnection())) {
player.networkHandler.sendPacket(new ResourcePackSendS2CPacket(x.id(), x.url(), x.hash(), AutoHost.config.require || PolymerResourcePackUtils.isRequired(), Optional.ofNullable(AutoHost.message)));
}
}
});
})))
)
);

CommonImplUtils.registerDevCommands((c) -> {
c.then(literal("reload_resourcepack").executes(context -> {
if (provider.isReady()) {
for (var x : provider.getProperties(((CommonNetworkHandlerExt) context.getSource().getPlayerOrThrow().networkHandler).polymerCommon$getConnection()
)) {
for (var x : provider.getProperties(((CommonNetworkHandlerExt) context.getSource().getPlayerOrThrow().networkHandler).polymerCommon$getConnection())) {
context.getSource().getPlayerOrThrow().networkHandler.sendPacket(new ResourcePackSendS2CPacket(x.id(), x.url(), x.hash(), AutoHost.config.require || PolymerResourcePackUtils.isRequired(), Optional.ofNullable(AutoHost.message)));
}
}
return 0;
}));
c.then(literal("rebuild_reload_rp").executes(context -> {
generateAndCall(context.getSource().getServer(), context.getSource()::sendMessage, () -> {
for (var x : provider.getProperties(((CommonNetworkHandlerExt) context.getSource().getPlayer().networkHandler).polymerCommon$getConnection()
)) {
context.getSource().getPlayer().networkHandler.sendPacket(new ResourcePackSendS2CPacket(x.id(), x.url(), x.hash(), AutoHost.config.require || PolymerResourcePackUtils.isRequired(), Optional.ofNullable(AutoHost.message)));
}
});
return 0;
}));

c.then(literal("force_ready").then(argument("value", BoolArgumentType.bool()).executes((ctx) -> {
if (provider instanceof AbstractProvider abs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.resourcepack.api.ResourcePackBuilder;
import eu.pb4.polymer.resourcepack.impl.PolymerResourcePackMod;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.MinecraftServer;
import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void serverStarted(MinecraftServer minecraftServer) {
isPackReady = true;
});

AutoHost.generateAndCall(minecraftServer, minecraftServer::sendMessage, () -> {});
PolymerResourcePackMod.generateAndCall(minecraftServer, true, minecraftServer::sendMessage, () -> {});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import net.minecraft.entity.data.DataTracker;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.EntityAttributesS2CPacket;
import net.minecraft.server.network.PlayerAssociatedNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Vec3d;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -50,6 +53,11 @@ default void modifyRawTrackedData(List<DataTracker.SerializedEntry<?>> data, Ser

}

default void modifyRawEntityAttributeData(List<EntityAttributesS2CPacket.Entry> data, PacketContext context, boolean initial) {

}


default void onEntityPacketSent(Consumer<Packet<?>> consumer, Packet<?> packet) {
consumer.accept(packet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ public static void reloadWorld(ServerPlayerEntity player) {
PolymerImplUtils.IS_RELOADING_WORLD.remove();
});
}

/**
* @deprecated Use {@link PolymerComponent#registerDataComponent(ComponentType[])} instead
*/
@Deprecated
public static void markAsPolymer(ComponentType<?>... types) {
PolymerItemUtils.markAsPolymer(types);
}
Expand All @@ -157,7 +160,7 @@ public static void markAsPolymer(ComponentType<?>... types) {
* Resends inventory to player
*/
public static void reloadInventory(ServerPlayerEntity player) {
player.networkHandler.sendPacket(new InventoryS2CPacket(0, 0, player.playerScreenHandler.getStacks(), player.playerScreenHandler.getCursorStack()));
player.currentScreenHandler.syncState();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions polymer-core/src/main/java/eu/pb4/polymer/core/impl/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public static void register(LiteralArgumentBuilder<ServerCommandSource> command,
.requires(CommonImplUtils.permission("command.target-block", 3))
.executes(Commands::targetBlock)
)
.then(literal("target-item")
.requires(CommonImplUtils.permission("command.target-item", 3))
.executes(Commands::targetItem)
)
.then(literal("pick")
.requires(CommonImplUtils.permission("command.pick", 0))
.executes((ctx) -> Commands.pickTarget(ctx, false))
Expand Down Expand Up @@ -248,6 +252,12 @@ private static int targetBlock(CommandContext<ServerCommandSource> context) {
return 0;
}

private static int targetItem(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var itemStack = context.getSource().getPlayerOrThrow().getMainHandStack();
context.getSource().sendFeedback(() -> Text.of(Registries.ITEM.getId(itemStack.getItem())), false);
return 0;
}

private static int dumpRegistries(CommandContext<ServerCommandSource> context) {
var path = PolymerImplUtils.dumpRegistry();
if (path != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eu.pb4.polymer.core.api.client.ClientPolymerItem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.component.ComponentMap;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -34,6 +35,11 @@ public RegistryEntry.Reference<Item> getRegistryEntry() {
return this.polymerItem.visualStack().getItem().getRegistryEntry();
}

@Override
public ItemStack getDefaultStack() {
return this.polymerItem.visualStack().copy();
}

@Override
public Text getName() {
return this.polymerItem.visualStack().getName();
Expand All @@ -48,6 +54,21 @@ public ClientPolymerItem getPolymerEntry() {
return this.polymerItem;
}

@Override
public ComponentMap getComponents() {
return this.polymerItem.visualStack().getComponents();
}

@Override
protected String getOrCreateTranslationKey() {
return "";
}

@Override
public int getMaxCount() {
return this.polymerItem.visualStack().getMaxCount();
}

@Override
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
if (this.polymerItem.visualStack().contains(DataComponentTypes.LORE)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.pb4.polymer.core.impl.interfaces;

public interface EntityTrackerUpdateS2CPacketExt {
public interface PossiblyInitialPacket {
boolean polymer$getInitial();
void polymer$setInitial();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.common.impl.entity.InternalEntityHelpers;
import eu.pb4.polymer.core.impl.interfaces.EntityAttachedPacket;
import eu.pb4.polymer.core.impl.interfaces.PossiblyInitialPacket;
import eu.pb4.polymer.core.impl.networking.TransformingPacketCodec;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -21,27 +22,45 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Mixin(EntityAttributesS2CPacket.class)
public abstract class EntityAttributesS2CPacketMixin {
public abstract class EntityAttributesS2CPacketMixin implements PossiblyInitialPacket {
@Unique
private boolean isInitial = false;

@Override
public boolean polymer$getInitial() {
return this.isInitial;
}

@Override
public void polymer$setInitial() {
this.isInitial = true;
}

@SuppressWarnings("UnreachableCode")
@ModifyExpressionValue(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Ljava/util/function/BiFunction;)Lnet/minecraft/network/codec/PacketCodec;"))
private static PacketCodec<RegistryByteBuf, EntityAttributesS2CPacket> patchCodec(PacketCodec<RegistryByteBuf, EntityAttributesS2CPacket> original) {
return TransformingPacketCodec.encodeOnly(original, (buf, packet) -> {
if (EntityAttachedPacket.get(packet, packet.getEntityId()) instanceof PolymerEntity entity) {
var type = entity.getPolymerEntityType(PolymerUtils.getPlayerContext());
var context = PacketContext.get();
var type = entity.getPolymerEntityType(context.getPlayer());
var p = new EntityAttributesS2CPacket(packet.getEntityId(), List.of());
var list = ((EntityAttributesS2CPacketAccessor) p).getEntries();
var vanillaContainer = DefaultAttributeRegistry.get((EntityType<? extends LivingEntity>) type);
for (var entry : packet.getEntries()) {
var data = new ArrayList<>(packet.getEntries());
entity.modifyRawEntityAttributeData(data, context, ((PossiblyInitialPacket) packet).polymer$getInitial());
for (var entry : data) {
if (vanillaContainer.has(entry.attribute()) && !PolymerEntityUtils.isPolymerEntityAttribute(entry.attribute())) {
list.add(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.datafixers.util.Pair;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
import eu.pb4.polymer.core.api.other.PlayerBoundConsumer;
import eu.pb4.polymer.core.impl.interfaces.EntityTrackerUpdateS2CPacketExt;
import eu.pb4.polymer.core.impl.interfaces.PossiblyInitialPacket;
import eu.pb4.polymer.core.impl.networking.PolymerServerProtocol;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
Expand All @@ -16,7 +16,6 @@
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket;
import net.minecraft.server.network.EntityTrackerEntry;
import net.minecraft.server.network.PlayerAssociatedNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
Expand Down Expand Up @@ -48,7 +47,13 @@ public abstract class EntityTrackerEntryMixin {

@ModifyArg(method = "sendPackets", at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V", ordinal = 1))
private Object polymer$markAsInitial(Object obj) {
((EntityTrackerUpdateS2CPacketExt) obj).polymer$setInitial();
((PossiblyInitialPacket) obj).polymer$setInitial();
return obj;
}

@ModifyArg(method = "sendPackets", at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V", ordinal = 2))
private Object polymer$markAsInitial2(Object obj) {
((PossiblyInitialPacket) obj).polymer$setInitial();
return obj;
}

Expand Down Expand Up @@ -89,7 +94,7 @@ public abstract class EntityTrackerEntryMixin {
if (this.entity instanceof PolymerEntity polymerEntity) {
if (polymerEntity.sendEmptyTrackerUpdates(player) && this.changedEntries == null) {
var x = new EntityTrackerUpdateS2CPacket(this.entity.getId(), List.of());
((EntityTrackerUpdateS2CPacketExt) (Object) x).polymer$setInitial();
((PossiblyInitialPacket) (Object) x).polymer$setInitial();
sender.accept(x);
}

Expand Down
Loading

0 comments on commit 667c73a

Please sign in to comment.