Fixed syncing task base, improved TripleChestHandler code

This commit is contained in:
FlorianMichael 2023-09-23 02:03:18 +02:00
parent 64adbb1330
commit 96fc1b78e0
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
8 changed files with 49 additions and 70 deletions

View File

@ -33,6 +33,9 @@ import de.florianmichael.viafabricplus.mappings.ItemReleaseVersionMappings;
import de.florianmichael.viafabricplus.mappings.PackFormatsMappings;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import de.florianmichael.viafabricplus.screen.ClassicItemSelectionScreen;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier;
import net.raphimc.vialoader.util.VersionEnum;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@ -24,6 +24,7 @@ import de.florianmichael.viafabricplus.base.event.FinishMinecraftLoadCallback;
import de.florianmichael.viafabricplus.base.event.LoadClassicProtocolExtensionCallback;
import de.florianmichael.viafabricplus.injection.MixinPlugin;
import de.florianmichael.viafabricplus.injection.access.IFontStorage;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
@ -33,6 +34,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.data.ClassicProtocolExtension;
import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.vialoader.util.VersionRange;
@ -100,6 +102,20 @@ public class ClientsideFixes {
Blocks.RED_STAINED_GLASS_PANE, Blocks.BLACK_STAINED_GLASS_PANE, Blocks.PISTON, Blocks.PISTON_HEAD,
Blocks.SNOW, Blocks.COBBLESTONE_WALL, Blocks.MOSSY_COBBLESTONE_WALL
);
// Allows to execute tasks on the main thread
ClientPlayNetworking.registerGlobalReceiver(new Identifier(ClientsideFixes.PACKET_SYNC_IDENTIFIER), (client, handler, buf, responseSender) -> {
final var uuid = buf.readString();
if (PENDING_EXECUTION_TASKS.containsKey(uuid)) {
MinecraftClient.getInstance().execute(() -> {
final var task = PENDING_EXECUTION_TASKS.get(uuid);
PENDING_EXECUTION_TASKS.remove(uuid);
task.accept(buf);
});
}
});
});
// Reloads some clientside stuff when the protocol version changes
@ -136,28 +152,6 @@ public class ClientsideFixes {
});
}
/**
* Returns the sync task for the specified uuid and removes it from the list
*
* @param uuid The uuid of the sync task
* @return The sync task
*/
public static Consumer<PacketByteBuf> getSyncTask(final String uuid) {
final var task = PENDING_EXECUTION_TASKS.get(uuid);
PENDING_EXECUTION_TASKS.remove(uuid);
return task;
}
/**
* Checks if there is a sync task for the specified uuid
*
* @param uuid The uuid to check
* @return True if there is a sync task, otherwise false
*/
public static boolean hasSyncTask(final String uuid) {
return PENDING_EXECUTION_TASKS.containsKey(uuid);
}
/**
* Executes a sync task and returns the uuid of the task
*

View File

@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.florianmichael.viafabricplus.definition.screen;
package de.florianmichael.viafabricplus.definition;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.gson.JsonElement;
@ -24,44 +24,41 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.resource.featuretoggle.FeatureFlags;
import net.minecraft.resource.featuretoggle.FeatureSet;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
public class CustomScreenHandler {
public class TripleChestHandler1_13_2 {
public final static Consumer<PacketByteBuf> TRIPLE_CHEST_HANDLER = data -> {
final var byteBuf = data.asByteBuf();
try {
CustomScreenHandler.handleTripleChestHandler(Type.SHORT.readPrimitive(byteBuf), Type.COMPONENT.read(byteBuf), Type.SHORT.readPrimitive(byteBuf));
TripleChestHandler1_13_2.handleTripleChestHandler(Type.SHORT.readPrimitive(byteBuf), Type.COMPONENT.read(byteBuf), Type.SHORT.readPrimitive(byteBuf));
} catch (Exception e) {
ViaFabricPlus.LOGGER.error("Failed to open custom ScreenHandler with dimension 9xN", e);
}
};
private final static Map<Integer, ScreenHandlerType<ScreenHandler>> TRIPLE_CHEST_HANDLERS = new LinkedHashMap<>();
static {
for (int i = 0; i < 1000; i++) {
int finalI = i;
TRIPLE_CHEST_HANDLERS.put(i, new ScreenHandlerType<>((syncId, playerInventory) -> new GenericContainerScreenHandler(TRIPLE_CHEST_HANDLERS.get(finalI), syncId, playerInventory, finalI), FeatureFlags.VANILLA_FEATURES));
}
}
public static void handleTripleChestHandler(final short windowID, final JsonElement title, final short slots) {
int n = slots / 9;
final int modulo = slots % 9;
if (modulo > 0) n++;
HandledScreens.open(CustomScreenHandler.TRIPLE_CHEST_HANDLERS.get(n), MinecraftClient.getInstance(), windowID, Text.Serializer.fromJson(title.toString()));
final var screenHandler = new AtomicReference<ScreenHandlerType<?>>();
int finalN = n;
screenHandler.set(new TripleChestScreenHandlerType((syncId, playerInventory) -> new GenericContainerScreenHandler(screenHandler.get(), syncId, playerInventory, finalN), FeatureFlags.VANILLA_FEATURES));
HandledScreens.open(screenHandler.get(), MinecraftClient.getInstance(), windowID, Text.Serializer.fromJson(title.toString()));
}
public static boolean isTripleChestHandler(final ScreenHandlerType<?> screenHandlerType) {
return TRIPLE_CHEST_HANDLERS.containsValue(screenHandlerType);
public static class TripleChestScreenHandlerType extends ScreenHandlerType<GenericContainerScreenHandler> {
public TripleChestScreenHandlerType(Factory<GenericContainerScreenHandler> factory, FeatureSet requiredFeatures) {
super(factory, requiredFeatures);
}
}
}

View File

@ -17,7 +17,7 @@
*/
package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft;
import de.florianmichael.viafabricplus.definition.screen.CustomScreenHandler;
import de.florianmichael.viafabricplus.definition.TripleChestHandler1_13_2;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
@ -36,7 +36,7 @@ public class MixinHandledScreens {
@Inject(method = "getProvider", at = @At("HEAD"), cancellable = true)
private static <T extends ScreenHandler> void returnFakeProvider(ScreenHandlerType<T> type, CallbackInfoReturnable<HandledScreens.@Nullable Provider> cir) {
if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_13_2) && CustomScreenHandler.isTripleChestHandler(type)) {
if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_13_2) && type instanceof TripleChestHandler1_13_2.TripleChestScreenHandlerType) {
cir.setReturnValue((handler, playerInventory, title) -> new GenericContainerScreen((GenericContainerScreenHandler) handler, playerInventory, title));
}
}

View File

@ -17,8 +17,10 @@
*/
package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.network;
import com.viaversion.viaversion.api.type.Type;
import de.florianmichael.viafabricplus.definition.ClientsideFixes;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import io.netty.buffer.ByteBufUtil;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientCommonNetworkHandler;
@ -39,6 +41,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.time.Duration;
import java.util.function.BooleanSupplier;
@SuppressWarnings("UnstableApiUsage")
@Mixin(ClientCommonNetworkHandler.class)
public abstract class MixinClientCommonNetworkHandler {
@ -74,19 +77,4 @@ public abstract class MixinClientCommonNetworkHandler {
}
send(packet, sendCondition, expiry);
}
@Inject(method = "onCustomPayload(Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetworkThreadUtils;forceMainThread(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/util/thread/ThreadExecutor;)V", shift = At.Shift.AFTER), cancellable = true)
public void handlePseudoPackets(CustomPayloadS2CPacket packet, CallbackInfo ci) {
final var channel = packet.payload().id().toString();
if (channel.equals(ClientsideFixes.PACKET_SYNC_IDENTIFIER) && packet.payload() instanceof PacketByteBufPayload payload) {
final var data = payload.data();
final var uuid = data.readString();
if (ClientsideFixes.hasSyncTask(uuid)) {
ClientsideFixes.getSyncTask(uuid).accept(data);
ci.cancel();
}
}
}
}

View File

@ -97,14 +97,13 @@ public abstract class MixinClientPlayNetworkHandler {
}
}
// @SuppressWarnings("InvalidInjectorMethodSignature")
// @ModifyConstant(method = "onEntityPassengersSet", constant = @Constant(classValue = BoatEntity.class))
// public Class<?> dontChangePlayerYaw(Object entity, Class<?> constant) {
// if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_18_2)) {
// return Integer.class;
// }
// return constant;
// }
@ModifyConstant(method = "onEntityPassengersSet", constant = @Constant(classValue = BoatEntity.class))
public Class<?> dontChangePlayerYaw(Object entity, Class<?> constant) {
if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_18_2)) {
return Integer.class;
}
return constant;
}
@WrapWithCondition(method = "onPlayerList", at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;warn(Ljava/lang/String;Ljava/lang/Object;)V", remap = false))
public boolean removeWarning(Logger instance, String s, Object o) {

View File

@ -28,7 +28,7 @@ import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.InventoryPackets;
import de.florianmichael.viafabricplus.definition.ClientsideFixes;
import de.florianmichael.viafabricplus.definition.screen.CustomScreenHandler;
import de.florianmichael.viafabricplus.definition.TripleChestHandler1_13_2;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import io.netty.buffer.Unpooled;
import net.minecraft.SharedConstants;
@ -65,7 +65,7 @@ public class MixinInventoryPackets {
fakeOpenWindow.read(Type.VAR_INT);
fakeOpenWindow.read(Type.VAR_INT);
final String uuid = ClientsideFixes.executeSyncTask(CustomScreenHandler.TRIPLE_CHEST_HANDLER);
final String uuid = ClientsideFixes.executeSyncTask(TripleChestHandler1_13_2.TRIPLE_CHEST_HANDLER);
wrapper.write(Type.STRING, uuid);
wrapper.write(Type.SHORT, windowId);

View File

@ -25,12 +25,10 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.connection.UserConnectionImpl;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
import de.florianmichael.viafabricplus.ViaFabricPlus;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import io.netty.buffer.Unpooled;
import net.minecraft.SharedConstants;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.math.BlockPos;
@ -41,7 +39,7 @@ public class BlockStateTranslator {
private final static UserConnection DUMMY_USER_CONNECTION = ProtocolHack.createFakerUserConnection(null);
public static int translateBlockState1_18(int oldId) {
final List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(SharedConstants.getProtocolVersion(), ProtocolVersion.v1_18_2.getVersion());
final List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(ViaFabricPlus.NATIVE_VERSION.getVersion(), ProtocolVersion.v1_18_2.getVersion());
if (protocolPath == null) return oldId;
final PacketByteBuf inputData = new PacketByteBuf(Unpooled.buffer());