Skip to content

Commit

Permalink
Fix issues on client and block id desync
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 25, 2021
1 parent e303917 commit 529309b
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 43 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3

# Mod Properties
mod_version = 0.1.0-pre4-1.17
mod_version = 0.1.0-pre5-1.17
maven_group = eu.pb4
archives_base_name = polymer

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.34.8+1.17
fabric_version=0.36.0+1.17
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public class BlockEventS2CPacketMixin {
@Environment(EnvType.CLIENT)
@Inject(method = "getBlock", at = @At("TAIL"), cancellable = true)
private void replaceBlockClient(CallbackInfoReturnable<Block> cir) {
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
if (this.oldBlock instanceof VirtualBlock virtualBlock) {
cir.setReturnValue(virtualBlock.getVirtualBlock(this.pos, player.getServerWorld()));
if (MinecraftClient.getInstance().getServer() != null) {
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
if (this.oldBlock instanceof VirtualBlock virtualBlock) {
cir.setReturnValue(virtualBlock.getVirtualBlock(this.pos, player.getServerWorld()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,18 @@ private void removeInvalidEntries(int id, DataTracker tracker, boolean forceUpda
@Environment(EnvType.CLIENT)
@Inject(method = "getTrackedValues", at = @At("RETURN"), cancellable = true)
private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<List<DataTracker.Entry<?>>> cir) {
List<DataTracker.Entry<?>> list = new ArrayList<>();
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
if (MinecraftClient.getInstance().getServer() != null) {
List<DataTracker.Entry<?>> list = new ArrayList<>();
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());

for (DataTracker.Entry<?> entry : cir.getReturnValue()) {
if (entry.get() instanceof ItemStack stack) {
list.add(new DataTracker.Entry(entry.getData(), ItemHelper.getVirtualItemStack(stack, player)));
} else {
list.add(entry);
for (DataTracker.Entry<?> entry : cir.getReturnValue()) {
if (entry.get() instanceof ItemStack stack) {
list.add(new DataTracker.Entry(entry.getData(), ItemHelper.getVirtualItemStack(stack, player)));
} else {
list.add(entry);
}
}
}

cir.setReturnValue(list);
cir.setReturnValue(list);
}
}}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ public class EntityEquipmentUpdateS2CPacketMixin {
@Environment(EnvType.CLIENT)
@Inject(method = "getEquipmentList", at = @At("RETURN"), cancellable = true)
private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<List<Pair<EquipmentSlot, ItemStack>>> cir) {
List<Pair<EquipmentSlot, ItemStack>> list = new ArrayList<>();
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
if (MinecraftClient.getInstance().getServer() != null) {
List<Pair<EquipmentSlot, ItemStack>> list = new ArrayList<>();
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());

for (Pair<EquipmentSlot, ItemStack> pair : cir.getReturnValue()) {
list.add(new Pair<>(pair.getFirst(), ItemHelper.getVirtualItemStack(pair.getSecond(), player)));
}
for (Pair<EquipmentSlot, ItemStack> pair : cir.getReturnValue()) {
list.add(new Pair<>(pair.getFirst(), ItemHelper.getVirtualItemStack(pair.getSecond(), player)));
}

cir.setReturnValue(list);
cir.setReturnValue(list);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public class InventoryS2CPacketMixin {
@Environment(EnvType.CLIENT)
@Inject(method = "getContents", at = @At("RETURN"), cancellable = true)
private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<List<ItemStack>> cir) {
List<ItemStack> list = new ArrayList<>();
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
if (MinecraftClient.getInstance().getServer() != null) {
List<ItemStack> list = new ArrayList<>();
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());

for (ItemStack stack : cir.getReturnValue()) {
list.add(ItemHelper.getVirtualItemStack(stack, player));
}
for (ItemStack stack : cir.getReturnValue()) {
list.add(ItemHelper.getVirtualItemStack(stack, player));
}

cir.setReturnValue(list);
cir.setReturnValue(list);
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package eu.pb4.polymer.mixin.item;

import eu.pb4.polymer.item.ItemHelper;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -17,8 +20,17 @@ private ItemStack replaceWithVanillaItem(ItemStack itemStack) {
return ItemHelper.getVirtualItemStack(itemStack, PacketContext.get().getTarget());
}

@Environment(EnvType.SERVER)
@Inject(method = "readItemStack", at = @At("RETURN"), cancellable = true)
private void replaceWithRealItem(CallbackInfoReturnable<ItemStack> cir) {
cir.setReturnValue(ItemHelper.getRealItemStack(cir.getReturnValue()));
}

@Environment(EnvType.CLIENT)
@Inject(method = "readItemStack", at = @At("RETURN"), cancellable = true)
private void replaceWithRealItemClient(CallbackInfoReturnable<ItemStack> cir) {
if (MinecraftClient.getInstance().getServer() != null) {
cir.setReturnValue(ItemHelper.getRealItemStack(cir.getReturnValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class ScreenHandlerSlotUpdateS2CPacketMixin {
@Environment(EnvType.CLIENT)
@Inject(method = "getItemStack", at = @At("RETURN"), cancellable = true)
private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<ItemStack> cir) {
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
cir.setReturnValue(ItemHelper.getVirtualItemStack(cir.getReturnValue(), player));
if (MinecraftClient.getInstance().getServer() != null) {
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
cir.setReturnValue(ItemHelper.getVirtualItemStack(cir.getReturnValue(), player));
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/eu/pb4/polymer/mixin/other/SimpleRegistryMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package eu.pb4.polymer.mixin.other;

import com.mojang.serialization.Lifecycle;
import eu.pb4.polymer.interfaces.VirtualObject;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.util.registry.SimpleRegistry;
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(SimpleRegistry.class)
public abstract class SimpleRegistryMixin<T> {
@Shadow private int nextId;

@Shadow public abstract <V extends T> V set(int rawId, RegistryKey<T> key, V entry, Lifecycle lifecycle);

@Inject(method = "add", at = @At("HEAD"), cancellable = true)
private <V extends T> void moveVirtualObjectToTheEnd(RegistryKey<T> key, V entry, Lifecycle lifecycle, CallbackInfoReturnable<V> cir) {
if (entry instanceof VirtualObject) {
int current = this.nextId;
this.set(current + 1000000, key, entry, lifecycle);
this.nextId = current + 1;
cir.setReturnValue(entry);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/eu/pb4/polymer/other/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eu.pb4.polymer.other;

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

public class Event<T> {
private List<EventHandler<T>> handlers = new ArrayList<>();

public void register(EventHandler<T> event) {
this.handlers.add(event);
}

public void invoke(T obj) {
for (EventHandler<T> consumer : this.handlers) {
consumer.call(obj);
}
}


public interface EventHandler<T> {
void call(T obj);
}
}
19 changes: 4 additions & 15 deletions src/main/java/eu/pb4/polymer/resourcepack/ResourcePackUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.google.gson.JsonParser;
import eu.pb4.polymer.PolymerMod;
import eu.pb4.polymer.other.Event;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.item.Item;
import net.minecraft.util.Identifier;
Expand All @@ -20,7 +19,7 @@ public class ResourcePackUtils {
private static final Object2ObjectMap<Item, List<CMDInfo>> ITEMS = new Object2ObjectArrayMap<>();
private static final Set<String> MOD_IDS = new HashSet<>();

private static int CMD_OFFSET = PolymerMod.POLYMC_COMPAT ? 1000 : 1;
private static int CMD_OFFSET = PolymerMod.POLYMC_COMPAT ? 100000 : 1;

/**
* This method can be used to register custom model data for items
Expand Down Expand Up @@ -63,7 +62,7 @@ public static boolean build(Path path) {
boolean successful = true;
RPBuilder builder = new DefaultRPBuilder(path);

RESOURCE_PACK_CREATION_EVENT.invoker().call(builder);
RESOURCE_PACK_CREATION_EVENT.invoke(builder);

for (String modId : MOD_IDS) {
successful = builder.copyModAssets(modId) && successful;
Expand Down Expand Up @@ -91,15 +90,5 @@ public static boolean build(Path path) {
}
}


public static final Event<ResourcePackEvent> RESOURCE_PACK_CREATION_EVENT = EventFactory.createArrayBacked(ResourcePackEvent.class, (callbacks) -> (builder) -> {
for(ResourcePackEvent callback : callbacks) {
callback.call(builder);
}
});

@FunctionalInterface
public interface ResourcePackEvent {
void call(RPBuilder builder);
}
public static final Event<RPBuilder> RESOURCE_PACK_CREATION_EVENT = new Event<>();
}
1 change: 1 addition & 0 deletions src/main/resources/polymer.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"other.CommandManagerMixin",
"other.DimensionTypeAccessor",
"other.RegistrySyncManagerMixin",
"other.SimpleRegistryMixin",
"other.SynchronizeRecipesS2CPacketMixin",
"polymc.BlockPolyGeneratorMixin"
],
Expand Down

0 comments on commit 529309b

Please sign in to comment.