Skip to content

Commit

Permalink
Merge pull request #13 from Just-Chaldea/general-fixes
Browse files Browse the repository at this point in the history
General fixes
  • Loading branch information
danorris709 authored Nov 23, 2022
2 parents 9f1b1e8 + 1cadf85 commit c6c115a
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ subprojects {
apply plugin: 'maven-publish'

group = 'gg.chaldea.client.reset.packet'
version = '0.0.5'
version = '0.0.6'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
36 changes: 36 additions & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ buildscript {
repositories {
maven { url = 'https://maven.minecraftforge.net' }
mavenCentral()
maven {
name = 'sponge'
url = 'https://repo.spongepowered.org/maven'
}
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true
classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
}
}

apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'org.spongepowered.mixin'

repositories {
maven {
name = 'spongepowered-repo'
url = 'https://repo.spongepowered.org/maven'
}
}

minecraft {
mappings channel: 'official', version: '1.16.5'
Expand All @@ -19,6 +32,29 @@ sourceSets.main.resources { srcDir 'src/generated/resources' }

dependencies {
minecraft 'net.minecraftforge:forge:1.16.5-36.2.34'

compileOnly 'org.spongepowered:mixin:0.8.3-SNAPSHOT'
compileOnly 'org.spongepowered:mixin:0.8.3-SNAPSHOT:processor'

annotationProcessor 'org.spongepowered:mixin:0.8.3-SNAPSHOT:processor'
}

sourceSets {
main {
ext.refMap = "mixins.clientresetpacket.refmap.json"
}
}

jar {
manifest.attributes(
'TweakClass': 'org.spongepowered.asm.launch.MixinTweaker',
'TweakOrder': 0,
'MixinConfigs' : "mixins.clientresetpacket.json"
)
}

mixin {
add sourceSets.main, "mixins.clientresetpacket.refmap.json"
}

build.finalizedBy('versionedRelease')
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.network.*;
import net.minecraftforge.registries.GameData;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.commons.lang3.tuple.Pair;

import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.Pair;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.network.FMLNetworkConstants;

@Mod("clientresetpacket")
public class ClientReset {
Expand Down Expand Up @@ -97,7 +95,14 @@ public static boolean handleClear(NetworkEvent.Context context) {

// Clear
Minecraft.getInstance().clearLevel(new DirtMessageScreen(new TranslationTextComponent("connect.negotiating")));

try {
context.getNetworkManager().channel().pipeline().remove("forge:forge_fixes");
} catch (NoSuchElementException ignored) {
}
try {
context.getNetworkManager().channel().pipeline().remove("forge:vanilla_filter");
} catch (NoSuchElementException ignored) {
}
// Restore
Minecraft.getInstance().setCurrentServer(serverData);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gg.chaldea.client.reset.packet.mixin;

import com.google.common.collect.Maps;
import net.minecraft.network.login.server.SDisconnectLoginPacket;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.fml.network.*;
import net.minecraftforge.registries.ForgeRegistry;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import static net.minecraftforge.registries.ForgeRegistry.REGISTRIES;

@Mixin(FMLHandshakeHandler.class)
public class MixinFMLHandshakeHandler {


@Shadow(remap = false) @Final static Marker FMLHSMARKER;
@Shadow(remap = false) @Final private static Logger LOGGER;

@Shadow(remap = false) private Set<ResourceLocation> registriesToReceive;
@Shadow(remap = false) private Map<ResourceLocation, ForgeRegistry.Snapshot> registrySnapshots;

/**
* @author
* @reason
*/
@Overwrite(remap = false)
void handleServerModListOnClient(FMLHandshakeMessages.S2CModList serverModList, Supplier<NetworkEvent.Context> c) {
LOGGER.debug(FMLHSMARKER, "Logging into server with mod list [{}]", String.join(", ", serverModList.getModList()));
c.get().setPacketHandled(true);
FMLNetworkConstants.handshakeChannel.reply(new FMLHandshakeMessages.C2SModListReply(), c.get());

LOGGER.debug(FMLHSMARKER, "Accepted server connection");
// Set the modded marker on the channel so we know we got packets
c.get().getNetworkManager().channel().attr(FMLNetworkConstants.FML_NETVERSION).set(FMLNetworkConstants.NETVERSION);
c.get().getNetworkManager().channel().attr(FMLNetworkConstants.FML_CONNECTION_DATA)
.set(new FMLConnectionData(serverModList.getModList(), serverModList.getChannels()));

this.registriesToReceive = new HashSet<>(serverModList.getRegistries());
this.registrySnapshots = Maps.newHashMap();
LOGGER.debug(REGISTRIES, "Expecting {} registries: {}", ()->this.registriesToReceive.size(), ()->this.registriesToReceive);
}

/**
* @author
* @reason
*/
@Overwrite(remap = false)
void handleClientModListOnServer(FMLHandshakeMessages.C2SModListReply clientModList, Supplier<NetworkEvent.Context> c) {
LOGGER.debug(FMLHSMARKER, "Received client connection with modlist [{}]", String.join(", ", clientModList.getModList()));
boolean accepted = NetworkRegistry.validateServerChannels(clientModList.getChannels());
((NetworkEvent.Context)c.get()).getNetworkManager().channel().attr(FMLNetworkConstants.FML_CONNECTION_DATA).set(new FMLConnectionData(clientModList.getModList(), clientModList.getChannels()));
((NetworkEvent.Context)c.get()).setPacketHandled(true);
if (!accepted) {
LOGGER.error(FMLHSMARKER, "Terminating connection with client, mismatched mod list");
c.get().getNetworkManager().send(new SDisconnectLoginPacket(new StringTextComponent("Connection closed - mismatched mod channel list")));
((NetworkEvent.Context)c.get()).getNetworkManager().disconnect(new StringTextComponent("Connection closed - mismatched mod channel list"));
} else {
LOGGER.debug(FMLHSMARKER, "Accepted client connection mod list");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gg.chaldea.client.reset.packet.mixin;

import net.minecraft.network.NetworkManager;
import net.minecraftforge.network.NetworkFilters;
import net.minecraftforge.network.VanillaPacketFilter;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Map;
import java.util.function.Function;

@Mixin(NetworkFilters.class)
public class MixinNetworkFilters {

@Shadow(remap = false) @Final private static Map<String, Function<NetworkManager, VanillaPacketFilter>> instances;
@Shadow(remap = false) @Final private static Logger LOGGER;

/**
* @author danorris709
* @reason Prevent error
*/
@Overwrite(remap = false)
public static void injectIfNecessary(NetworkManager manager)
{
instances.forEach((key, filterFactory) -> {
if (manager.channel().pipeline().get(key) != null) {
return;
}

try {
VanillaPacketFilter filter = filterFactory.apply(manager);
if (((VanillaPacketFilterAccessor)filter).invokeIsNecessary(manager)) {
manager.channel().pipeline().addBefore("packet_handler", key, filter);
LOGGER.debug("Injected {} into {}", filter, manager);
}
} catch (Exception e) {

}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gg.chaldea.client.reset.packet.mixin;

import io.netty.channel.Channel;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.DirtMessageScreen;
import net.minecraft.client.gui.screen.DisconnectedScreen;
import net.minecraft.client.gui.screen.MainMenuScreen;
import net.minecraft.client.gui.screen.MultiplayerScreen;
import net.minecraft.network.NetworkManager;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(NetworkManager.class)
public class MixinNetworkManager {

@Shadow private Channel channel;
@Shadow private ITextComponent disconnectedReason;

/**
* @author
* @reason
*/
@Overwrite
@OnlyIn(Dist.CLIENT)
public void disconnect(ITextComponent p_150718_1_) {
if (this.channel.isOpen()) {
this.channel.close().awaitUninterruptibly();
this.disconnectedReason = p_150718_1_;

if (Minecraft.getInstance().screen instanceof DirtMessageScreen) {
ITextComponent title = ((DirtMessageScreen)Minecraft.getInstance().screen).getTitle();

if (title instanceof TranslationTextComponent &&
((TranslationTextComponent) title).getKey().equals("connect.negotiating")) {
Minecraft.getInstance().setScreen(new DisconnectedScreen(new MultiplayerScreen(new MainMenuScreen()), new StringTextComponent(""), this.disconnectedReason));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gg.chaldea.client.reset.packet.mixin;

import net.minecraft.network.NetworkManager;
import net.minecraftforge.network.VanillaPacketFilter;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(VanillaPacketFilter.class)
public interface VanillaPacketFilterAccessor {

@Invoker("isNecessary")
boolean invokeIsNecessary(NetworkManager manager);
}
4 changes: 3 additions & 1 deletion forge/src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public net.minecraftforge.fml.network.FMLHandshakeMessages$LoginIndexedMessage <
public net.minecraftforge.fml.network.FMLHandshakeMessages$LoginIndexedMessage setLoginIndex(I)V # setLoginIndex
public net.minecraftforge.fml.network.FMLHandshakeHandler LOGGER # LOGGER
public net.minecraftforge.fml.network.FMLHandshakeHandler FMLHSMARKER # FMLHSMARKER
public net.minecraftforge.fml.network.NetworkEvent$Context <init>(Lnet/minecraft/network/NetworkManager;Lnet/minecraftforge/fml/network/NetworkDirection;I)V
public net.minecraftforge.fml.network.NetworkEvent$Context <init>(Lnet/minecraft/network/NetworkManager;Lnet/minecraftforge/fml/network/NetworkDirection;I)V
public net.minecraftforge.fml.network.FMLConnectionData <init>(Ljava/util/List;Ljava/util/Map;)V
public net.minecraftforge.fml.network.NetworkRegistry validateServerChannels(Ljava/util/Map;)Z
2 changes: 1 addition & 1 deletion forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ issueTrackerURL="https://github.com/Just-Chaldea/Forge-Client-Reset-Packet/issue

[[mods]]
modId="clientresetpacket"
version="0.0.5"
version="0.0.6"
displayName="Client Reset Packet"
displayURL="https://github.com/Just-Chaldea/Forge-Client-Reset-Packet"
credits="https://github.com/Just-Chaldea/Forge-Client-Reset-Packet"
Expand Down
15 changes: 15 additions & 0 deletions forge/src/main/resources/mixins.clientresetpacket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"required": true,
"minVersion": "0.7.10",
"package": "gg.chaldea.client.reset.packet.mixin",
"refmap": "mixins.clientresetpacket.refmap.json",
"target": "@env(DEFAULT)",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinFMLHandshakeHandler",
"MixinNetworkFilters",
"MixinNetworkManager",
"VanillaPacketFilterAccessor"
],
"client": []
}
Binary file removed release/ForgeClientResetPacket-0.0.3.jar
Binary file not shown.
Binary file modified release/ForgeClientResetPacket-0.0.5.jar
Binary file not shown.
Binary file removed release/forge-0.0.3-0.0.3.jar
Binary file not shown.

0 comments on commit c6c115a

Please sign in to comment.