Skip to content

Commit

Permalink
Fix suspicious sand not showing polymer items, call syncState() inste…
Browse files Browse the repository at this point in the history
…ad of constructing packet incorrectly, breaking chest uis
  • Loading branch information
Patbox committed Aug 27, 2024
1 parent e69b6f8 commit c3584df
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 24 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fabric_version=0.102.1+1.21.1

maven_group = eu.pb4

mod_version = 0.9.11
mod_version = 0.9.12

minecraft_version_supported = ">=1.20.5-"
minecraft_version_supported = ">=1.21-"

packet_tweaker_version = 0.5.5+1.21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

public final class BooleanEvent<T> {
private List<T> handlers = new ArrayList<>();
private final List<T> handlers = new ArrayList<>();

public void register(T listener) {
this.handlers.add(listener);
Expand All @@ -25,10 +27,14 @@ public boolean invoke(Predicate<T> invoker) {
for (var handler : handlers) {
var bool = invoker.test(handler);

if (bool == true) {
if (bool) {
return true;
}
}
return false;
}

public Collection<T> invokers() {
return Collections.unmodifiableCollection(this.handlers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

public final class FunctionEvent<T, R> {
private List<T> handlers = new ArrayList<>();
private final List<T> handlers = new ArrayList<>();

public void register(T listener) {
this.handlers.add(listener);
Expand All @@ -25,4 +26,8 @@ public void unregister(T listener) {
public R invoke(Function<Collection<T>, R> invoker) {
return invoker.apply(handlers);
}

public Collection<T> invokers() {
return Collections.unmodifiableCollection(this.handlers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public final class SimpleEvent<T> {
private List<T> handlers = new ArrayList<>();
private final List<T> handlers = new ArrayList<>();

public void register(T listener) {
this.handlers.add(listener);
Expand All @@ -30,4 +32,8 @@ public void invoke(Consumer<T> invoker) {
public boolean isEmpty() {
return this.handlers.isEmpty();
}

public Collection<T> invokers() {
return Collections.unmodifiableCollection(this.handlers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ public static NbtCompound transformBlockEntityNbt(PacketContext context, BlockEn
}
}

if (original.contains("item", NbtElement.COMPOUND_TYPE)) {
var stack = ItemStack.fromNbtOrEmpty(lookup, original.getCompound("item"));
if (PolymerItemUtils.isPolymerServerItem(stack, context)) {
if (override == null) {
override = original.copy();
}
stack = PolymerItemUtils.getPolymerItemStack(stack, context);
override.put("item", stack.encodeAllowEmpty(lookup));
}
}

if (original.contains("components", NbtElement.COMPOUND_TYPE)) {
var ops = lookup.getOps(NbtOps.INSTANCE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;

/**
* Used to mark client-synchronized polymer objects like BlockEntities, Enchantments, Recipes, etc
Expand All @@ -28,7 +29,7 @@ default boolean canSynchronizeToPolymerClient(ServerPlayerEntity player) {
* Allows to mark it to still send it to supported clients (for client optional setups)
* Currently used for tags
*/
default boolean canSyncRawToClient(ServerPlayerEntity player) {
default boolean canSyncRawToClient(@Nullable ServerPlayerEntity player) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,28 @@ public static void schedulePacket(ServerPlayNetworkHandler handler, Packet<?> pa
public static void reloadWorld(ServerPlayerEntity player) {
player.server.execute(() -> {
PolymerImplUtils.IS_RELOADING_WORLD.set(Unit.INSTANCE);
player.networkHandler.sendPacket(new InventoryS2CPacket(0, 0, player.playerScreenHandler.getStacks(), player.playerScreenHandler.getCursorStack()));
try {
player.currentScreenHandler.syncState();

var world = player.getWorld();
var tacsAccess = ((ServerChunkLoadingManagerAccessor) ((ServerChunkManager) player.getWorld().getChunkManager()).chunkLoadingManager);
var world = player.getWorld();
var tacsAccess = ((ServerChunkLoadingManagerAccessor) ((ServerChunkManager) player.getWorld().getChunkManager()).chunkLoadingManager);

for (var e : ((ServerWorldAccessor) player.getWorld()).polymer_getEntityManager().getLookup().iterate()) {
var tracker = tacsAccess.polymer$getEntityTrackers().get(e.getId());
if (tracker != null) {
tracker.stopTracking(player);
for (var e : ((ServerWorldAccessor) player.getWorld()).polymer_getEntityManager().getLookup().iterate()) {
var tracker = tacsAccess.polymer$getEntityTrackers().get(e.getId());
if (tracker != null) {
tracker.stopTracking(player);
}
}
}


player.getChunkFilter().forEach((chunkPos) -> {
var chunk = world.getChunk(chunkPos.x, chunkPos.z);
player.networkHandler.chunkDataSender.unload(player, chunk.getPos());
player.networkHandler.chunkDataSender.add(chunk);
});
player.getChunkFilter().forEach((chunkPos) -> {
var chunk = world.getChunk(chunkPos.x, chunkPos.z);
player.networkHandler.chunkDataSender.unload(player, chunk.getPos());
player.networkHandler.chunkDataSender.add(chunk);
});
} catch (Throwable e) {
PolymerImpl.LOGGER.warn("Failed to reload player's world view!", e);
}

PolymerImplUtils.IS_RELOADING_WORLD.remove();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package eu.pb4.polymer.resourcepack.api;

import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import eu.pb4.polymer.common.api.events.SimpleEvent;
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.common.impl.CommonImplUtils;
Expand Down Expand Up @@ -41,7 +39,7 @@ public final class ResourcePackCreator {
public final SimpleEvent<Consumer<ResourcePackBuilder>> creationEvent = new SimpleEvent<>();
public final SimpleEvent<Runnable> finishedEvent = new SimpleEvent<>();
public final SimpleEvent<Consumer<ResourcePackBuilder>> afterInitialCreationEvent = new SimpleEvent<>();
private final Map<Item, List<PolymerModelData>> items = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH);
private final Map<Item, List<PolymerModelData>> items = new IdentityHashMap<>();
private final Object2IntMap<Item> itemIds = new Object2IntOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH);
private final Map<Item, Map<Identifier, PolymerModelData>> itemModels = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH);
private final Map<Item, List<ItemOverride>> itemOverrides = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH);
Expand All @@ -59,8 +57,31 @@ public final class ResourcePackCreator {
public static ResourcePackCreator create() {
return new ResourcePackCreator(0);
}
public static ResourcePackCreator createCopy(ResourcePackCreator source, boolean copyEvents) {
var creator = new ResourcePackCreator(source.cmdOffset);
if (copyEvents) {
source.creationEvent.invokers().forEach(creator.creationEvent::register);
source.afterInitialCreationEvent.invokers().forEach(creator.afterInitialCreationEvent::register);
source.finishedEvent.invokers().forEach(creator.finishedEvent::register);
}

source.items.forEach((a, b) -> creator.items.put(a, new ArrayList<>(b)));
creator.itemIds.putAll(source.itemIds);
source.itemModels.forEach((a, b) -> creator.itemModels.put(a, new HashMap<>(b)));
source.itemOverrides.forEach((a, b) -> creator.itemOverrides.put(a, new ArrayList<>(b)));
creator.modIds.addAll(source.modIds);
creator.modIdsNoCopy.addAll(source.modIdsNoCopy);
creator.takenArmorColors.addAll(source.takenArmorColors);
creator.armorModelMap.putAll(source.armorModelMap);
creator.armorColor = source.armorColor;
creator.packDescription = source.packDescription;
creator.packIcon = source.packIcon;
creator.sourcePaths.addAll(source.sourcePaths);

return creator;
}

protected ResourcePackCreator(int cmdOffset) {
ResourcePackCreator(int cmdOffset) {
this.cmdOffset = cmdOffset;
this.itemIds.defaultReturnValue(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.HoverEvent;
Expand Down Expand Up @@ -45,6 +47,19 @@ public void onInitialize() {
CommonImplUtils.registerCommands((x) -> x.then(literal("generate-pack")
.requires(CommonImplUtils.permission("command.generate", 3))
.executes(PolymerResourcePackMod::generateResources)));

for (var mod : FabricLoader.getInstance().getAllMods()) {
var include = mod.getMetadata().getCustomValue("polymer:resource_pack_include");
var require = mod.getMetadata().getCustomValue("polymer:resource_pack_require");

if (include != null && include.getType() == CustomValue.CvType.BOOLEAN && include.getAsBoolean()) {
PolymerResourcePackUtils.addModAssets(mod.getMetadata().getId());
}

if (require != null && require.getType() == CustomValue.CvType.BOOLEAN && require.getAsBoolean()) {
PolymerResourcePackUtils.markAsRequired();
}
}
}

@Override
Expand Down

0 comments on commit c3584df

Please sign in to comment.