Skip to content

Commit

Permalink
Some finalizing stuff to the recode:
Browse files Browse the repository at this point in the history
- Moved classic related classes
- Fixed classic item selection screen
- Added PackVersionDiff back from v2
  • Loading branch information
FlorianMichael committed Nov 27, 2023
1 parent 0c42add commit b878bda
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import de.florianmichael.viafabricplus.event.DisconnectCallback;
import de.florianmichael.viafabricplus.event.LoadClassicProtocolExtensionCallback;
import de.florianmichael.viafabricplus.event.PostGameLoadCallback;
import de.florianmichael.viafabricplus.fixes.data.PackVersionDiff;
import de.florianmichael.viafabricplus.fixes.entity.EntityDimensionReplacements;
import de.florianmichael.viafabricplus.fixes.classic.CustomClassicProtocolExtensions;
import de.florianmichael.viafabricplus.fixes.classic.ClassicItemSelectionScreen;
import de.florianmichael.viafabricplus.fixes.classic.CPEAdditions;
import de.florianmichael.viafabricplus.fixes.classic.GridItemSelectionScreen;
import de.florianmichael.viafabricplus.injection.ViaFabricPlusMixinPlugin;
import de.florianmichael.viafabricplus.protocolhack.provider.vialegacy.ViaFabricPlusClassicMPPassProvider;
import net.minecraft.block.*;
Expand Down Expand Up @@ -62,7 +63,11 @@ public class ClientsideFixes {
private static int currentChatLength = 256;

public static void init() {
CustomClassicProtocolExtensions.create();
// Register additional CPE features
CPEAdditions.modifyMappings();

// Check if the pack format mappings are correct
PackVersionDiff.checkOutdated();

PostGameLoadCallback.EVENT.register(() -> {
// Handles and updates entity dimension changes in <= 1.17
Expand All @@ -78,6 +83,9 @@ public static void init() {

// Remove all previous unacked player actions
ClientPlayerInteractionManager1_18_2.clearUnackedActions();

// Rebuilt the item selection screen grid next time the screen is opened
GridItemSelectionScreen.INSTANCE.itemGrid = null;
});

// Reloads some clientside stuff when the protocol version changes
Expand Down Expand Up @@ -120,7 +128,7 @@ public static void init() {

// Rebuilds the item selection screen grid
if (newVersion.isOlderThanOrEqualTo(VersionEnum.c0_28toc0_30)) {
ClassicItemSelectionScreen.INSTANCE.rebuildGridOverlay();
GridItemSelectionScreen.INSTANCE.itemGrid = null;
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,25 @@
import java.util.Map;
import java.util.function.BiConsumer;

public class CustomClassicProtocolExtensions {
public static CustomClassicProtocolExtensions INSTANCE;
public class CPEAdditions {
public final static List<ClassicProtocolExtension> ALLOWED_EXTENSIONS = Arrays.asList(ClassicProtocolExtension.ENV_WEATHER_TYPE);
public final static Map<Integer, ClientboundPacketsc0_30cpe> CUSTOM_PACKETS = new HashMap<>();

public static ClientboundPacketsc0_30cpe EXT_WEATHER_TYPE;

public static void create() {
CustomClassicProtocolExtensions.INSTANCE = new CustomClassicProtocolExtensions();

public static void modifyMappings() {
EXT_WEATHER_TYPE = createNewPacket(ClassicProtocolExtension.ENV_WEATHER_TYPE, 31, (user, buf) -> buf.readByte());

LoadClassicProtocolExtensionCallback.EVENT.register(classicProtocolExtension -> {
if (classicProtocolExtension == ClassicProtocolExtension.CUSTOM_BLOCKS) {
ClassicItemSelectionScreen.INSTANCE.rebuildGridOverlay();
}
});
}

public final List<ClassicProtocolExtension> ALLOWED_EXTENSIONS = Arrays.asList(ClassicProtocolExtension.ENV_WEATHER_TYPE);
public final Map<Integer, ClientboundPacketsc0_30cpe> CUSTOM_PACKETS = new HashMap<>();

public static void allowExtension(final ClassicProtocolExtension classicProtocolExtension) {
INSTANCE.ALLOWED_EXTENSIONS.add(classicProtocolExtension);
ALLOWED_EXTENSIONS.add(classicProtocolExtension);
}

public static ClientboundPacketsc0_30cpe createNewPacket(final ClassicProtocolExtension classicProtocolExtension, final int packetId, final BiConsumer<UserConnection, ByteBuf> packetSplitter) {
final ClientboundPacketsc0_30cpe packet = Enums.newInstance(ClientboundPacketsc0_30cpe.class, classicProtocolExtension.getName(), ClassicProtocolExtension.values().length, new Class[] { int.class, BiConsumer.class }, new Object[] { packetId, packetSplitter });
Enums.addEnumInstance(ClientboundPacketsc0_30cpe.class, packet);
INSTANCE.CUSTOM_PACKETS.put(packetId, packet);
CUSTOM_PACKETS.put(packetId, packet);

return packet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.util.List;

@SuppressWarnings("DataFlowIssue")
public class ClassicItemSelectionScreen extends VFPScreen {
public static final ClassicItemSelectionScreen INSTANCE = new ClassicItemSelectionScreen();
public class GridItemSelectionScreen extends VFPScreen {
public static final GridItemSelectionScreen INSTANCE = new GridItemSelectionScreen();

private static final int MAX_ROW_DIVIDER = 9;
private static final int ITEM_XY_BOX_DIMENSION_CLASSIC = 25;
Expand All @@ -42,13 +42,16 @@ public class ClassicItemSelectionScreen extends VFPScreen {
public Item[][] itemGrid = null;
public ItemStack selectedItem = null;

public ClassicItemSelectionScreen() {
public GridItemSelectionScreen() {
super("Classic item selection", false);
}

public void rebuildGridOverlay() {
@Override
protected void init() {
if (itemGrid != null) {
return;
}
final List<Item> allowedItems = new ArrayList<>();

// Calculate all visible items
for (Item item : Registries.ITEM) {
if (ItemRegistryDiff.keepItem(item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import java.util.BitSet;
import java.util.List;

public class ClassicWorldHeightInjections {
public class WorldHeightSupport {

public static PacketHandler handleJoinGame(final PacketHandler parentHandler) {
return wrapper -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.connection.UserConnection;
import de.florianmichael.viafabricplus.injection.access.IExtensionProtocolMetadataStorage;
import de.florianmichael.viafabricplus.protocolhack.command.VFPViaSubCommand;
import net.minecraft.util.Formatting;
import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.storage.ExtensionProtocolMetadataStorage;
import net.raphimc.vialoader.util.VersionEnum;

public class ListExtensionsCommand extends ClassicViaSubCommand {
public class ListExtensionsCommand extends VFPViaSubCommand {
@Override
public String name() {
return "listextensions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

import com.viaversion.viaversion.api.command.ViaCommandSender;
import com.viaversion.viaversion.api.connection.UserConnection;
import de.florianmichael.viafabricplus.protocolhack.command.VFPViaSubCommand;
import net.minecraft.util.Formatting;
import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.vialegacy.protocols.alpha.protocola1_0_17_1_0_17_4toa1_0_16_2.storage.TimeLockStorage;

public class SetTimeCommand extends ClassicViaSubCommand {
public class SetTimeCommand extends VFPViaSubCommand {
@Override
public String name() {
return "settime";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,15 +1358,20 @@ public class ItemRegistryDiff {
public static boolean keepItem(final Item item) {
if (ProtocolHack.getTargetVersion().equals(c0_30cpe)) {
final ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler();
if (handler == null) return true;
if (handler == null) {
// Don't drop any items if the connection is not established yet
return true;
}
final ExtensionProtocolMetadataStorage extensionProtocol = ((IClientConnection)handler.getConnection()).viaFabricPlus$getUserConnection().get(ExtensionProtocolMetadataStorage.class);
if (extensionProtocol == null) return false;
if (extensionProtocol == null) { // Should never happen
return false;
}
if (extensionProtocol.hasServerExtension(ClassicProtocolExtension.CUSTOM_BLOCKS, 1) && EXTENDED_CLASSIC_ITEMS.contains(item)) {
return true;
}
}

return !ITEM_DIFF.containsKey(item) || ITEM_DIFF.get(item).contains(ProtocolHack.getTargetVersion() /*ProtocolHack.getTargetVersion().isNewerThanOrEqualTo(ITEM_DIFF.get(item)*/);
return !ITEM_DIFF.containsKey(item) || ITEM_DIFF.get(item).contains(ProtocolHack.getTargetVersion());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
* Copyright (C) 2021-2023 FlorianMichael/EnZaXD
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.fixes.data;

import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import net.minecraft.GameVersion;
import net.minecraft.SaveVersion;
import net.minecraft.SharedConstants;
import net.minecraft.resource.ResourceType;
import net.raphimc.vialoader.util.VersionEnum;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PackVersionDiff {
private final static Map<VersionEnum, GameVersion> GAME_VERSION_DIFF = new HashMap<>();

static {
registerVersion(VersionEnum.r1_20_3, 22, "1.20.3-pre2");
registerVersion(VersionEnum.r1_20_2, 18, "1.20.2");
registerVersion(VersionEnum.r1_20tor1_20_1, 15, "1.20.1"); // 1.20 and 1.20.1 are equal
registerVersion(VersionEnum.r1_19_4, 13, "1.19.4");
registerVersion(VersionEnum.r1_19_3, 12, "1.19.3");
registerVersion(VersionEnum.r1_19_1tor1_19_2, 9, "1.19.2");
registerVersion(VersionEnum.r1_19, 9, "1.19");
registerVersion(VersionEnum.r1_18_2, 8, "1.18.2");
registerVersion(VersionEnum.r1_18tor1_18_1, 8, "1.18");
registerVersion(VersionEnum.r1_17_1, 7, "1.17.1");
registerVersion(VersionEnum.r1_17, 7, "1.17");
registerVersion(VersionEnum.r1_16_4tor1_16_5, 6, "1.16.5"); // 1.16.4 and 1.16.5 are equal
registerVersion(VersionEnum.r1_16_3, 6, "1.16.3");
registerVersion(VersionEnum.r1_16_2, 6, "1.16.2");
registerVersion(VersionEnum.r1_16_1, 5, "1.16.1");
registerVersion(VersionEnum.r1_16, 5, "1.16");
registerVersion(VersionEnum.r1_15_2, 5, "1.15.2");
registerVersion(VersionEnum.r1_15_1, 5, "1.15.1");
registerVersion(VersionEnum.r1_15, 5, "1.15");
registerVersion(VersionEnum.r1_14_4, 4, "1.14.4");
registerVersion(VersionEnum.r1_14_3, 4, "1.14.3");
registerVersion(VersionEnum.r1_14_2, 4, "1.14.2", "1.14.2 / f647ba8dc371474797bee24b2b312ff4");
registerVersion(VersionEnum.r1_14_1, 4, "1.14.1", "1.14.1 / a8f78b0d43c74598a199d6d80cda413f");
registerVersion(VersionEnum.r1_14, 4, "1.14", "1.14 / 5dac5567e13e46bdb0c1d90aa8d8b3f7");
registerVersion(VersionEnum.r1_13_2, 4, "1.13.2"); // ids weren't sent over the http headers back then
registerVersion(VersionEnum.r1_13_1, 4, "1.13.1");
registerVersion(VersionEnum.r1_13, 4, "1.13");
registerVersion(VersionEnum.r1_12_2, 3, "1.12.2");
registerVersion(VersionEnum.r1_12_1, 3, "1.12.1");
registerVersion(VersionEnum.r1_12, 3, "1.12");
registerVersion(VersionEnum.r1_11_1to1_11_2, 3, "1.11.2");
registerVersion(VersionEnum.r1_11, 3, "1.11");
registerVersion(VersionEnum.r1_10, 2, "1.10.2");
registerVersion(VersionEnum.r1_9_3tor1_9_4, 2, "1.9.4");
registerVersion(VersionEnum.r1_9_2, 2, "1.9.2");
registerVersion(VersionEnum.r1_9_1, 2, "1.9.1");
registerVersion(VersionEnum.r1_9, 2, "1.9");
registerVersion(VersionEnum.r1_8, 1, "1.8.9");
registerVersion(VersionEnum.r1_7_6tor1_7_10, 1, "1.7.10");
registerVersion(VersionEnum.r1_7_2tor1_7_5, 1, "1.7.5");
}

public static void checkOutdated() {
if (!GAME_VERSION_DIFF.containsKey(ProtocolHack.NATIVE_VERSION)) {
throw new RuntimeException("The current version has no pack format registered");
}

final GameVersion gameVersion = GAME_VERSION_DIFF.get(ProtocolHack.NATIVE_VERSION);
if (!gameVersion.getName().equals(SharedConstants.getGameVersion().getName()) || !gameVersion.getId().equals(SharedConstants.getGameVersion().getId()) ||
gameVersion.getResourceVersion(ResourceType.CLIENT_RESOURCES) != SharedConstants.getGameVersion().getResourceVersion(ResourceType.CLIENT_RESOURCES)) {
throw new RuntimeException("The current version is outdated, please update to " + gameVersion.getName() + " (" + gameVersion.getId() + ")");
}
}

public static GameVersion get(final VersionEnum version) {
if (!GAME_VERSION_DIFF.containsKey(version)) {
return SharedConstants.getGameVersion();
}
return GAME_VERSION_DIFF.get(version);
}

private static void registerVersion(final VersionEnum version, final int packFormat, final String name) {
registerVersion(version, packFormat, name, name);
}

private static void registerVersion(final VersionEnum version, final int packFormat, final String name, final String id) {
GAME_VERSION_DIFF.put(version, new GameVersion() {

@Override
public SaveVersion getSaveVersion() {
return null;
}

@Override
public String getId() {
return id;
}

@Override
public String getName() {
return name;
}

@Override
public int getProtocolVersion() {
return version.getProtocol().getOriginalVersion();
}

@Override
public int getResourceVersion(ResourceType type) {
if (type == ResourceType.CLIENT_RESOURCES) {
return packFormat;
}
throw new UnsupportedOperationException();
}

@Override
public Date getBuildTime() {
return null;
}

@Override
public boolean isStable() {
return true;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
import com.llamalad7.mixinextras.sugar.Local;
import de.florianmichael.viafabricplus.fixes.data.PackVersionDiff;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import net.minecraft.GameVersion;
import net.minecraft.SharedConstants;
import net.minecraft.client.resource.ServerResourcePackProvider;
import net.raphimc.vialoader.util.VersionEnum;
import org.apache.commons.codec.digest.DigestUtils;
Expand All @@ -47,8 +47,7 @@ public abstract class MixinServerResourcePackProvider {

@Redirect(method = "getDownloadHeaders", at = @At(value = "INVOKE", target = "Lnet/minecraft/SharedConstants;getGameVersion()Lnet/minecraft/GameVersion;"))
private static GameVersion editHeaders() {
// return PackFormatsMappings.current();
return SharedConstants.getGameVersion(); // TODO | Fix
return PackVersionDiff.get(ProtocolHack.getTargetVersion());
}

@Inject(method = "getDownloadHeaders", at = @At("TAIL"), cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.screen;

import de.florianmichael.viafabricplus.fixes.classic.ClassicItemSelectionScreen;
import de.florianmichael.viafabricplus.fixes.classic.GridItemSelectionScreen;
import de.florianmichael.viafabricplus.settings.impl.VisualSettings;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
Expand All @@ -34,7 +34,7 @@ public abstract class MixinCreativeInventoryScreen {
@Inject(method = "init", at = @At("RETURN"))
private void replaceCreativeMenu(CallbackInfo ci) {
if (VisualSettings.global().replaceCreativeInventory.isEnabled()) {
MinecraftClient.getInstance().setScreen(ClassicItemSelectionScreen.INSTANCE);
MinecraftClient.getInstance().setScreen(GridItemSelectionScreen.INSTANCE);
}
}

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

package de.florianmichael.viafabricplus.injection.mixin.fixes.vialegacy;

import de.florianmichael.viafabricplus.fixes.classic.CustomClassicProtocolExtensions;
import de.florianmichael.viafabricplus.fixes.classic.CPEAdditions;
import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.data.ClassicProtocolExtension;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -31,21 +31,21 @@ public abstract class MixinClassicProtocolExtension {

@Inject(method = "supportsVersion", at = @At("HEAD"), cancellable = true)
private void allowExtensions_supportsVersion(int version, CallbackInfoReturnable<Boolean> cir) {
if (CustomClassicProtocolExtensions.INSTANCE.ALLOWED_EXTENSIONS.contains((ClassicProtocolExtension) (Object) this)) {
if (CPEAdditions.ALLOWED_EXTENSIONS.contains((ClassicProtocolExtension) (Object) this)) {
cir.setReturnValue(true);
}
}

@Inject(method = "isSupported", at = @At("HEAD"), cancellable = true)
private void allowExtensions_isSupported(CallbackInfoReturnable<Boolean> cir) {
if (CustomClassicProtocolExtensions.INSTANCE.ALLOWED_EXTENSIONS.contains((ClassicProtocolExtension) (Object) this)) {
if (CPEAdditions.ALLOWED_EXTENSIONS.contains((ClassicProtocolExtension) (Object) this)) {
cir.setReturnValue(true);
}
}

@Inject(method = "getHighestSupportedVersion", at = @At("HEAD"), cancellable = true)
private void allowExtensions_getHighestSupportedVersion(CallbackInfoReturnable<Integer> cir) {
if (CustomClassicProtocolExtensions.INSTANCE.ALLOWED_EXTENSIONS.contains((ClassicProtocolExtension) (Object) this)) {
if (CPEAdditions.ALLOWED_EXTENSIONS.contains((ClassicProtocolExtension) (Object) this)) {
cir.setReturnValue(1);
}
}
Expand Down
Loading

0 comments on commit b878bda

Please sign in to comment.