Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Apr 3, 2024
1 parent 1fed15d commit 5c724f1
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MixinServerPing {
locals = LocalCapture.CAPTURE_FAILSOFT,
at = @At("HEAD")
)
private void captureEntry(ServerInfo entry, Runnable saver, CallbackInfo ci) {
private void captureEntry(ServerInfo entry, Runnable saver, Runnable pingCallback, CallbackInfo ci) {
this.entry = entry;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.ServerMain;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.CrossbowItem;
import net.minecraft.item.ItemStack;
Expand All @@ -12,14 +13,13 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import static com.github.zly2006.enclosure.utils.Permission.SHOOT;

@Mixin(CrossbowItem.class)
public class MixinCrossBowItem {
@Inject(method = "use", locals = LocalCapture.CAPTURE_FAILSOFT, at = @At(value = "INVOKE",target = "Lnet/minecraft/item/CrossbowItem;shootAll(Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/util/Hand;Lnet/minecraft/item/ItemStack;FF)V"), cancellable = true)
private void onShoot(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir, ItemStack itemStack) {
@Inject(method = "use", at = @At(value = "INVOKE",target = "Lnet/minecraft/item/CrossbowItem;shootAll(Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/util/Hand;Lnet/minecraft/item/ItemStack;FFLnet/minecraft/entity/LivingEntity;)V"), cancellable = true)
private void onShoot(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir, @Local ItemStack itemStack) {
if (user instanceof ServerPlayerEntity player) {
if (!ServerMain.INSTANCE.checkPermission(player, SHOOT, player.getBlockPos())) {
player.sendMessage(SHOOT.getNoPermissionMsg(player));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.boss.WitherEntity;
Expand All @@ -14,16 +14,15 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(WitherEntity.class)
public abstract class MixinWitherEntity extends Entity {
public MixinWitherEntity(EntityType<?> type, World world) {
super(type, world);
}

@Inject(method = "mobTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;breakBlock(Lnet/minecraft/util/math/BlockPos;ZLnet/minecraft/entity/Entity;)Z"), locals = LocalCapture.CAPTURE_FAILSOFT, cancellable = true)
private void onBreakBlock(CallbackInfo ci, int i, int j, int k, boolean bl, int l, int m, int n, int o, int p, int q, BlockPos blockPos, BlockState blockState) {
@Inject(method = "mobTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;breakBlock(Lnet/minecraft/util/math/BlockPos;ZLnet/minecraft/entity/Entity;)Z"), cancellable = true)
private void onBreakBlock(CallbackInfo ci, @Local BlockPos blockPos) {
if (getWorld().isClient) {
return;
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/github/zly2006/enclosure/mixin/PacketDebug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.zly2006.enclosure.mixin;/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Final;
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;

import java.util.Map;

@Mixin(targets = "net/minecraft/network/packet/CustomPayload$1" ,priority = 1001)
public class PacketDebug {
@Final
@Shadow
Map<Identifier, PacketCodec<?, ? extends CustomPayload>> field_48658;
@Inject(
method = "getCodec",
at = @At(
"RETURN"
)
)
private void wrapConfigCodec(Identifier id, CallbackInfoReturnable<PacketCodec<?, ?>> cir, @Local PacketCodec<?, ?> packetCodec) {
System.out.println("id " + id + " -> " + packetCodec);
System.out.println("maps " + field_48658.keySet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.zly2006.enclosure.mixin.workaround.fuckMajong;

import com.github.zly2006.enclosure.network.EnclosureInstalledC2SPacket;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
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.ArrayList;

@Mixin(CustomPayloadC2SPacket.class)
public class MixinC2S {
@Inject(
method = "method_58271",
at = @At("HEAD")
)
private static void inject(ArrayList<CustomPayload.Type<?, ?>> types, CallbackInfo ci) {
types.add(new CustomPayload.Type<>(EnclosureInstalledC2SPacket.Companion.getID(), EnclosureInstalledC2SPacket.Companion.getCODEC()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ConfirmRequestS2CPacket(

@JvmStatic
fun register() {
PayloadTypeRegistry.configurationS2C().register(ID, CODEC)
PayloadTypeRegistry.playS2C().register(ID, CODEC)
ClientPlayNetworking.registerGlobalReceiver(ID) { payload: ConfirmRequestS2CPacket?, context: ClientPlayNetworking.Context? ->
val client = MinecraftClient.getInstance()
if (client.currentScreen is EnclosureGui) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ package com.github.zly2006.enclosure.network
import com.github.zly2006.enclosure.MOD_VERSION
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
import net.fabricmc.loader.api.Version
import net.minecraft.network.PacketByteBuf
import net.minecraft.network.codec.PacketCodec
import net.minecraft.network.packet.CustomPayload
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerPlayNetworkHandler
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.Identifier
import java.util.*

class EnclosureInstalledC2SPacket(
val version: Version
) : CustomPayload {

constructor(buf: PacketByteBuf?) : this(Version.parse(buf!!.readString()))
constructor(buf: PacketByteBuf) : this(Version.parse(buf.readString()))

private fun write(buf: PacketByteBuf) {
buf.writeString(version.friendlyString)
Expand All @@ -28,7 +26,7 @@ class EnclosureInstalledC2SPacket(
override fun getId() = ID

companion object {
val installedClientMod: MutableMap<ServerPlayerEntity, Version> = HashMap()
val installedClientMod: MutableMap<UUID, Version> = HashMap()
val ID: CustomPayload.Id<EnclosureInstalledC2SPacket?> = CustomPayload.Id(Identifier("enclosure:installed"))
val CODEC: PacketCodec<PacketByteBuf, EnclosureInstalledC2SPacket?> = CustomPayload.codecOf(
{ obj, buf -> obj!!.write(buf) },
Expand All @@ -37,21 +35,21 @@ class EnclosureInstalledC2SPacket(
@JvmStatic
fun isInstalled(player: ServerPlayerEntity?): Boolean {
if (player == null) return false
return installedClientMod.containsKey(player)
return installedClientMod.containsKey(player.uuid)
}

fun clientVersion(connection: ServerPlayerEntity): Version? {
return installedClientMod[connection]
return installedClientMod[connection.uuid]
}

fun register() {
PayloadTypeRegistry.configurationC2S().register(ID, CODEC)
ServerPlayNetworking.registerGlobalReceiver<EnclosureInstalledC2SPacket?>(ID) { payload: EnclosureInstalledC2SPacket?, context: ServerPlayNetworking.Context ->
val player = context.player() as ServerPlayerEntity
installedClientMod[player] = payload!!.version
ServerConfigurationNetworking.registerGlobalReceiver<EnclosureInstalledC2SPacket>(ID) { payload, context ->
if (payload == null) return@registerGlobalReceiver
installedClientMod[context.networkHandler().debugProfile.id] = payload.version
}
ServerPlayConnectionEvents.DISCONNECT.register(ServerPlayConnectionEvents.Disconnect { handler: ServerPlayNetworkHandler, server: MinecraftServer? ->
installedClientMod.remove(handler.player)
ServerPlayConnectionEvents.DISCONNECT.register(ServerPlayConnectionEvents.Disconnect { handler, _ ->
installedClientMod.remove(handler.player.uuid)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RequestOpenScreenC2SPPacket(

@JvmStatic
fun register() {
PayloadTypeRegistry.configurationS2C().register(ID, CODEC)
PayloadTypeRegistry.playC2S().register(ID, CODEC)
ServerPlayNetworking.registerGlobalReceiver(ID) { payload: RequestOpenScreenC2SPPacket?, context: ServerPlayNetworking.Context? ->
val blockPos = BlockPos(payload!!.pos[0], payload.pos[1], payload.pos[2])
var area: EnclosureArea? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SyncPermissionS2CPacket(
@JvmStatic
fun register() {

PayloadTypeRegistry.configurationS2C().register(ID, CODEC)
PayloadTypeRegistry.playS2C().register(ID, CODEC)
ClientPlayNetworking.registerGlobalReceiver(ID) { payload: SyncPermissionS2CPacket, context: ClientPlayNetworking.Context? ->
val client = MinecraftClient.getInstance()
val screen = client.currentScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SyncSelectionS2CPacket(
@JvmStatic
fun register() {

PayloadTypeRegistry.configurationS2C().register(ID, CODEC)
PayloadTypeRegistry.playS2C().register(ID, CODEC)
ClientPlayNetworking.registerGlobalReceiver(ID, ) { payload, context ->

if (ClientMain.clientSession == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UUIDCacheS2CPacket(
{ buf -> UUIDCacheS2CPacket(buf) })
@JvmStatic
fun register() {
PayloadTypeRegistry.configurationS2C().register(ID, CODEC)
PayloadTypeRegistry.playS2C().register(ID, CODEC)
ClientPlayNetworking.registerGlobalReceiver(ID) { payload, _ ->
uuid2name.clear()
payload.cache.keys.forEach { key -> uuid2name[payload.cache.getUuid(key)!!] = key }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ fun register(dispatcher: CommandDispatcher<ServerCommandSource>, access: Command
permission("enclosure.command.admin.clients", BuilderScope.Companion.DefaultPermission.OP)
executes {
EnclosureInstalledC2SPacket.installedClientMod.forEach {
source.sendMessage(Text.literal(it.key.nameForScoreboard + ": " + it.value.friendlyString))
source.sendMessage(
Text.literal(
"${source.server.playerManager.getPlayer(it.key)?.nameForScoreboard}: ${it.value.friendlyString}"
)
)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/enclosure.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"MixinVehicleEntity",
"MixinWitherEntity",
"MixinWitherSkullBlock",
"PacketDebug",
"workaround.MixinPersistentStateManager",
"workaround.MixinRegistrySyncManager",
"workaround.fuckMajong.MixinC2S"
]
}

0 comments on commit 5c724f1

Please sign in to comment.