-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/me/senseiwells/replay/ducks/ServerReplay$PackTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.senseiwells.replay.ducks; | ||
|
||
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket; | ||
|
||
import java.util.Collection; | ||
|
||
public interface ServerReplay$PackTracker { | ||
void replay$addPacks(Collection<ClientboundResourcePackPushPacket> packs); | ||
|
||
Collection<ClientboundResourcePackPushPacket> replay$getPacks(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/me/senseiwells/replay/mixin/rejoin/ServerCommonPacketListenerImplMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.senseiwells.replay.mixin.rejoin; | ||
|
||
import me.senseiwells.replay.ducks.ServerReplay$PackTracker; | ||
import net.minecraft.network.PacketSendListener; | ||
import net.minecraft.network.protocol.Packet; | ||
import net.minecraft.network.protocol.common.ClientboundResourcePackPopPacket; | ||
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket; | ||
import net.minecraft.server.network.ServerCommonPacketListenerImpl; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Mixin(ServerCommonPacketListenerImpl.class) | ||
public class ServerCommonPacketListenerImplMixin implements ServerReplay$PackTracker { | ||
// We need to keep track of what packs a player has... | ||
// We don't really care if the player accepts / declines them, we'll record them anyway. | ||
@Unique private final Map<UUID, ClientboundResourcePackPushPacket> replay$packs = new ConcurrentHashMap<>(); | ||
|
||
@Inject( | ||
method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V", | ||
at = @At("HEAD") | ||
) | ||
private void onSendPacket( | ||
Packet<?> packet, | ||
@Nullable PacketSendListener packetSendListener, | ||
CallbackInfo ci | ||
) { | ||
if (packet instanceof ClientboundResourcePackPushPacket resources) { | ||
this.replay$packs.put(resources.id(), resources); | ||
return; | ||
} | ||
if (packet instanceof ClientboundResourcePackPopPacket resources) { | ||
Optional<UUID> uuid = resources.id(); | ||
if (uuid.isPresent()) { | ||
this.replay$packs.remove(uuid.get()); | ||
} else { | ||
this.replay$packs.clear(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void replay$addPacks(Collection<ClientboundResourcePackPushPacket> packs) { | ||
for (ClientboundResourcePackPushPacket packet : packs) { | ||
this.replay$packs.put(packet.id(), packet); | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<ClientboundResourcePackPushPacket> replay$getPacks() { | ||
return this.replay$packs.values(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...n/java/me/senseiwells/replay/mixin/rejoin/ServerConfigurationPacketListenerImplMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package me.senseiwells.replay.mixin.rejoin; | ||
|
||
import com.llamalad7.mixinextras.sugar.Local; | ||
import me.senseiwells.replay.ducks.ServerReplay$PackTracker; | ||
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket; | ||
import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.Collection; | ||
|
||
@Mixin(ServerConfigurationPacketListenerImpl.class) | ||
public class ServerConfigurationPacketListenerImplMixin { | ||
@Inject( | ||
method = "handleConfigurationFinished", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/network/Connection;resumeInboundAfterProtocolChange()V" | ||
) | ||
) | ||
private void afterPlayerSpawned( | ||
ServerboundFinishConfigurationPacket serverboundFinishConfigurationPacket, | ||
CallbackInfo ci, | ||
@Local ServerPlayer serverPlayer | ||
) { | ||
// Merge the packs into the GamePacketListener | ||
Collection<ClientboundResourcePackPushPacket> packs = ((ServerReplay$PackTracker) this).replay$getPacks(); | ||
((ServerReplay$PackTracker) serverPlayer.connection).replay$addPacks(packs); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/kotlin/me/senseiwells/replay/commands/PackCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package me.senseiwells.replay.commands | ||
|
||
import com.mojang.brigadier.Command | ||
import com.mojang.brigadier.CommandDispatcher | ||
import com.mojang.brigadier.arguments.StringArgumentType | ||
import com.mojang.brigadier.context.CommandContext | ||
import com.mojang.brigadier.suggestion.Suggestions | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder | ||
import me.senseiwells.replay.ducks.`ServerReplay$PackTracker` | ||
import net.minecraft.commands.CommandSourceStack | ||
import net.minecraft.commands.Commands | ||
import net.minecraft.commands.SharedSuggestionProvider | ||
import net.minecraft.commands.arguments.UuidArgument | ||
import net.minecraft.network.protocol.common.ClientboundResourcePackPopPacket | ||
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket | ||
import java.util.Optional | ||
import java.util.UUID | ||
import java.util.concurrent.CompletableFuture | ||
|
||
object PackCommand { | ||
@JvmStatic | ||
fun register(dispatcher: CommandDispatcher<CommandSourceStack>) { | ||
dispatcher.register( | ||
Commands.literal("resource-pack").then( | ||
Commands.literal("push").then( | ||
Commands.argument("url", StringArgumentType.string()).then( | ||
Commands.argument("uuid", UuidArgument.uuid()).executes(this::pushPack) | ||
).executes { this.pushPack(it) } | ||
) | ||
).then( | ||
Commands.literal("pop").then( | ||
Commands.argument("uuid", UuidArgument.uuid()).suggests(this::suggestPacks).executes(this::popPack) | ||
) | ||
) | ||
) | ||
} | ||
|
||
private fun pushPack(context: CommandContext<CommandSourceStack>): Int { | ||
val url = StringArgumentType.getString(context, "url") | ||
val uuid = UUID.nameUUIDFromBytes(url.encodeToByteArray()) | ||
return this.pushPack(context, uuid) | ||
} | ||
|
||
private fun pushPack( | ||
context: CommandContext<CommandSourceStack>, | ||
uuid: UUID = UuidArgument.getUuid(context, "uuid") | ||
): Int { | ||
val url = StringArgumentType.getString(context, "url") | ||
val packet = ClientboundResourcePackPushPacket(uuid, url, "", false, null) | ||
for (player in context.source.server.playerList.players) { | ||
player.connection.send(packet) | ||
} | ||
return Command.SINGLE_SUCCESS | ||
} | ||
|
||
private fun popPack(context: CommandContext<CommandSourceStack>): Int { | ||
val uuid = UuidArgument.getUuid(context, "uuid") | ||
val packet = ClientboundResourcePackPopPacket(Optional.of(uuid)) | ||
for (player in context.source.server.playerList.players) { | ||
player.connection.send(packet) | ||
} | ||
return Command.SINGLE_SUCCESS | ||
} | ||
|
||
private fun suggestPacks( | ||
context: CommandContext<CommandSourceStack>, | ||
builder: SuggestionsBuilder | ||
): CompletableFuture<Suggestions> { | ||
val player = context.source.player ?: return Suggestions.empty() | ||
val packs = (player.connection as `ServerReplay$PackTracker`).`replay$getPacks`() | ||
return SharedSuggestionProvider.suggest(packs.map { it.id.toString() }, builder) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters