diff --git a/build.gradle b/build.gradle index 663edfe..fb98474 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.0-SNAPSHOT' + id 'fabric-loom' version '1.6-SNAPSHOT' id 'maven-publish' } diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/SLSCarpetSettings.java b/src/main/java/com/github/zly2006/carpetslsaddition/SLSCarpetSettings.java index 2297b50..fdfa63a 100644 --- a/src/main/java/com/github/zly2006/carpetslsaddition/SLSCarpetSettings.java +++ b/src/main/java/com/github/zly2006/carpetslsaddition/SLSCarpetSettings.java @@ -47,9 +47,6 @@ public class SLSCarpetSettings { @Rule(categories = {CARPET_ID, PCA, RuleCategory.FEATURE, NEED_CLIENT}) public static boolean emptyShulkerBoxStack = false; - @Rule(categories = {CARPET_ID, PCA, RuleCategory.FEATURE}) - public static boolean useDyeOnShulkerBox = false; - @Rule(categories = {CARPET_ID, PCA, PROTOCOL}, strict = false, options = {"#none"}) public static String xaeroWorldName = "#none"; diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/ServerMain.java b/src/main/java/com/github/zly2006/carpetslsaddition/ServerMain.java index 8d32b8e..50e37b7 100644 --- a/src/main/java/com/github/zly2006/carpetslsaddition/ServerMain.java +++ b/src/main/java/com/github/zly2006/carpetslsaddition/ServerMain.java @@ -45,7 +45,6 @@ public class ServerMain implements ModInitializer, CarpetExtension { public void onInitialize() { INSTANCE = this; CarpetServer.manageExtension(this); - PcaSyncProtocol.init(); } @Override diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/command/BotCommand.java b/src/main/java/com/github/zly2006/carpetslsaddition/command/BotCommand.java index 035d01b..298e4e4 100644 --- a/src/main/java/com/github/zly2006/carpetslsaddition/command/BotCommand.java +++ b/src/main/java/com/github/zly2006/carpetslsaddition/command/BotCommand.java @@ -316,7 +316,7 @@ public class BotCommand { private static int maxNameLength(MinecraftServer server) { - return server.getServerPort() >= 0 ? PlayerEntity.field_30643 : 40; + return 40; } private static int manipulate(CommandContext context, Consumer action) diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/item/MixinDyeItem.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/item/MixinDyeItem.java deleted file mode 100644 index c50bf3b..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/item/MixinDyeItem.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.item; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; -import net.minecraft.block.ShulkerBoxBlock; -import net.minecraft.block.entity.ShulkerBoxBlockEntity; -import net.minecraft.item.DyeItem; -import net.minecraft.item.Item; -import net.minecraft.item.ItemUsageContext; -import net.minecraft.util.ActionResult; -import net.minecraft.util.DyeColor; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(DyeItem.class) -public abstract class MixinDyeItem extends Item { - public MixinDyeItem(Settings settings) { - super(settings); - } - - @Shadow - public abstract DyeColor getColor(); - - @Override - public ActionResult useOnBlock(ItemUsageContext context) { - if (!SLSCarpetSettings.useDyeOnShulkerBox) { - return ActionResult.PASS; - } - World world = context.getWorld(); - BlockPos pos = context.getBlockPos(); - BlockState blockState = world.getBlockState(pos); - - if (blockState.isOf(Blocks.SHULKER_BOX)) { - if (!world.isClient()) { - ShulkerBoxBlockEntity blockEntity = (ShulkerBoxBlockEntity) world.getBlockEntity(pos); - BlockState newBlockState = ShulkerBoxBlock.get(getColor()).getDefaultState().with(ShulkerBoxBlock.FACING, blockState.get(ShulkerBoxBlock.FACING)); - - if (world.setBlockState(pos, newBlockState)) { - ShulkerBoxBlockEntity newBlockEntity = (ShulkerBoxBlockEntity) world.getBlockEntity(pos); - assert blockEntity != null; - assert newBlockEntity != null; - newBlockEntity.readInventoryNbt(blockEntity.createNbt()); - newBlockEntity.setCustomName(blockEntity.getCustomName()); - newBlockEntity.markDirty(); - context.getStack().decrement(1); - } - } - return ActionResult.success(world.isClient); - } - return ActionResult.PASS; - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/item/MixinPotionItem.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/item/MixinPotionItem.java deleted file mode 100644 index 99300ef..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/item/MixinPotionItem.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.item; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; -import net.minecraft.block.ShulkerBoxBlock; -import net.minecraft.block.entity.ShulkerBoxBlockEntity; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.*; -import net.minecraft.potion.PotionUtil; -import net.minecraft.potion.Potions; -import net.minecraft.util.ActionResult; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; -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.CallbackInfoReturnable; - -import java.util.Objects; - -@Mixin(PotionItem.class) -public abstract class MixinPotionItem extends Item { - public MixinPotionItem(Settings settings) { - super(settings); - } - - @Inject(method = "useOnBlock", at = @At(value = "HEAD"), cancellable = true) - public void useOnBlock(ItemUsageContext context, CallbackInfoReturnable cir) { - ItemStack itemStack = context.getStack(); - PlayerEntity player = context.getPlayer(); - - if (!SLSCarpetSettings.useDyeOnShulkerBox || - player == null || - itemStack.getItem() != Items.POTION || - PotionUtil.getPotion(itemStack) != Potions.WATER) { - return; - } - - World world = context.getWorld(); - BlockPos pos = context.getBlockPos(); - BlockState blockState = world.getBlockState(pos); - Block block = blockState.getBlock(); - - if (block instanceof ShulkerBoxBlock && ((ShulkerBoxBlock) block).getColor() != null) { - if (!world.isClient()) { - ShulkerBoxBlockEntity blockEntity = (ShulkerBoxBlockEntity) world.getBlockEntity(pos); - BlockState newBlockState = Blocks.SHULKER_BOX.getDefaultState(). - with(ShulkerBoxBlock.FACING, blockState.get(ShulkerBoxBlock.FACING)); - - if (world.setBlockState(pos, newBlockState)) { - ShulkerBoxBlockEntity newBlockEntity = (ShulkerBoxBlockEntity) world.getBlockEntity(pos); - assert blockEntity != null; - assert newBlockEntity != null; - newBlockEntity.readInventoryNbt(blockEntity.createNbt()); - newBlockEntity.setCustomName(blockEntity.getCustomName()); - newBlockEntity.markDirty(); - if (!player.isCreative()) { - context.getStack().decrement(1); - Objects.requireNonNull(context.getPlayer()).getInventory().insertStack(new ItemStack(Items.GLASS_BOTTLE)); - } - } - } - cir.setReturnValue(ActionResult.success(world.isClient)); - } - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/player/MixinPlayerInventory.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/player/MixinPlayerInventory.java deleted file mode 100644 index 7e036ed..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/player/MixinPlayerInventory.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.player; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil; -import me.fallenbreath.conditionalmixin.api.annotation.Condition; -import me.fallenbreath.conditionalmixin.api.annotation.Restriction; -import net.minecraft.entity.player.PlayerAbilities; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.inventory.Inventory; -import net.minecraft.item.ItemStack; -import net.minecraft.util.Nameable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -@Restriction( - conflict = @Condition("pca") -) -@Mixin(PlayerInventory.class) -public abstract class MixinPlayerInventory implements Inventory, Nameable { - @Redirect(method = "insertStack(ILnet/minecraft/item/ItemStack;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getAbilities()Lnet/minecraft/entity/player/PlayerAbilities;")) - private PlayerAbilities insertStack(PlayerEntity instance) { - if (SLSCarpetSettings.creativeNoInfinitePickup) { - AccessorPlayerAbilities accessor = (AccessorPlayerAbilities) instance.getAbilities(); - PlayerAbilities abilities = new PlayerAbilities(); - AccessorPlayerAbilities accessor1 = (AccessorPlayerAbilities) abilities; - accessor1.setCreativeMode(false); - accessor1.setFlying(accessor.isFlying()); - accessor1.setFlyingSpeed(accessor.getFlyingSpeed()); - accessor1.setInvulnerable(accessor.isInvulnerable()); - accessor1.setAllowFlying(accessor.isAllowFlying()); - accessor1.setAllowModifyWorld(accessor.isAllowModifyWorld()); - accessor1.setWalkingSpeed(accessor.getWalkingSpeed()); - return abilities; - } - return instance.getAbilities(); - } - - // 修改潜影盒拾取逻辑,使从地上捡起潜影盒时仍可堆叠 - @Redirect(method = "canStackAddMore", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isStackable()Z", ordinal = 0)) - private boolean canStackAddMoreIsStackable(ItemStack itemStack) { - return ShulkerBoxItemUtil.isStackable(itemStack); - } - - @Redirect(method = "canStackAddMore", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I", ordinal = 0)) - private int canStackAddMoreGetMaxCount(ItemStack itemStack) { - return ShulkerBoxItemUtil.getMaxCount(itemStack); - } - - @Redirect(method = "addStack(ILnet/minecraft/item/ItemStack;)I", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I", ordinal = -1)) - private int addStackGetMaxCount(ItemStack itemStack) { - return ShulkerBoxItemUtil.getMaxCount(itemStack); - } - - // 避免死循环 - @Redirect(method = "offer", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I", ordinal = 0)) - private int offerGetMaxCount(ItemStack itemStack) { - return ShulkerBoxItemUtil.getMaxCount(itemStack); - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinAbstractFurnaceBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinAbstractFurnaceBlockEntity.java deleted file mode 100644 index 64d2981..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinAbstractFurnaceBlockEntity.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.AbstractFurnaceBlockEntity; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.LockableContainerBlockEntity; -import net.minecraft.inventory.SidedInventory; -import net.minecraft.recipe.RecipeInputProvider; -import net.minecraft.recipe.RecipeUnlocker; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(AbstractFurnaceBlockEntity.class) -public abstract class MixinAbstractFurnaceBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider { - - - protected MixinAbstractFurnaceBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update AbstractFurnaceBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBarrelBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBarrelBlockEntity.java deleted file mode 100644 index c17279d..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBarrelBlockEntity.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BarrelBlockEntity; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.LootableContainerBlockEntity; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(BarrelBlockEntity.class) -public abstract class MixinBarrelBlockEntity extends LootableContainerBlockEntity { - - protected MixinBarrelBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update BarrelBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBeehiveBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBeehiveBlockEntity.java deleted file mode 100644 index eeb4035..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBeehiveBlockEntity.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BeehiveBlockEntity; -import net.minecraft.block.entity.BlockEntity; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.entity.Entity; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; -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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import java.util.List; -import java.util.Objects; - -@Mixin(BeehiveBlockEntity.class) -public abstract class MixinBeehiveBlockEntity extends BlockEntity { - - - public MixinBeehiveBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { - super(type, pos, state); - } - - @Inject(method = "tickBees", at = @At(value = "INVOKE", target = "Ljava/util/Iterator;remove()V", shift = At.Shift.AFTER)) - private static void postTickBees(World world, BlockPos pos, BlockState state, List bees, BlockPos flowerPos, CallbackInfo ci) { - BlockEntity blockEntity = world.getBlockEntity(pos); - if (blockEntity != null) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(blockEntity)) { - ServerMain.LOGGER.debug("update BeehiveBlockEntity: {}", pos); - } - } - } - - @Inject(method = "tryReleaseBee", at = @At(value = "RETURN")) - public void postTryReleaseBee(BlockState state, BeehiveBlockEntity.BeeState beeState, CallbackInfoReturnable> cir) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this) && cir.getReturnValue() != null) { - ServerMain.LOGGER.debug("update BeehiveBlockEntity: {}", this.pos); - } - } - - @Inject(method = "readNbt", at = @At(value = "RETURN")) - public void postFromTag(NbtCompound nbt, CallbackInfo ci) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update BeehiveBlockEntity: {}", this.pos); - } - } - - @Inject(method = "tryEnterHive(Lnet/minecraft/entity/Entity;ZI)V", at = @At(value = "INVOKE", - target = "Lnet/minecraft/entity/Entity;discard()V", ordinal = 0)) - public void postEnterHive(Entity entity, boolean hasNectar, int ticksInHive, CallbackInfo ci) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update BeehiveBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBrewingStandBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBrewingStandBlockEntity.java deleted file mode 100644 index b6e6005..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinBrewingStandBlockEntity.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.BrewingStandBlockEntity; -import net.minecraft.block.entity.LockableContainerBlockEntity; -import net.minecraft.inventory.SidedInventory; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(BrewingStandBlockEntity.class) -public abstract class MixinBrewingStandBlockEntity extends LockableContainerBlockEntity implements SidedInventory { - - protected MixinBrewingStandBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update BrewingStandBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinChestBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinChestBlockEntity.java deleted file mode 100644 index f411a2c..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinChestBlockEntity.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.ChestBlockEntity; -import net.minecraft.block.entity.LidOpenable; -import net.minecraft.block.entity.LootableContainerBlockEntity; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -// 由于陷阱箱继承自箱子,因此不用 mixin 陷阱箱 -// implements ChestAnimationProgress 会出错 不知道为啥 -@Mixin(ChestBlockEntity.class) -public abstract class MixinChestBlockEntity extends LootableContainerBlockEntity implements LidOpenable { - - protected MixinChestBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update ChestBlockEntity: {}", this.pos); - } - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinComparatorBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinComparatorBlockEntity.java deleted file mode 100644 index 5c00806..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinComparatorBlockEntity.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntity; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.ComparatorBlockEntity; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(ComparatorBlockEntity.class) -public abstract class MixinComparatorBlockEntity extends BlockEntity { - public MixinComparatorBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update ComparatorBlockEntity: {}", this.pos); - } - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinDispenserBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinDispenserBlockEntity.java deleted file mode 100644 index 109f0a6..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinDispenserBlockEntity.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.DispenserBlockEntity; -import net.minecraft.block.entity.LootableContainerBlockEntity; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(DispenserBlockEntity.class) -public abstract class MixinDispenserBlockEntity extends LootableContainerBlockEntity { - - protected MixinDispenserBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update DispenserBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinHopperBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinHopperBlockEntity.java deleted file mode 100644 index bc87e98..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinHopperBlockEntity.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.Hopper; -import net.minecraft.block.entity.HopperBlockEntity; -import net.minecraft.block.entity.LootableContainerBlockEntity; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; -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.CallbackInfoReturnable; - -import java.util.function.BooleanSupplier; - -@Mixin(HopperBlockEntity.class) -public abstract class MixinHopperBlockEntity extends LootableContainerBlockEntity implements Hopper { - - protected MixinHopperBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Inject(method = "insertAndExtract", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/HopperBlockEntity;markDirty(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V")) - private static void onInsertAndExtract(World world, BlockPos pos, BlockState state, HopperBlockEntity blockEntity, BooleanSupplier booleanSupplier, CallbackInfoReturnable cir) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(blockEntity)) { - ServerMain.LOGGER.debug("update HopperBlockEntity: {}", pos); - } - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update HopperBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinShulkerBoxBlockEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinShulkerBoxBlockEntity.java deleted file mode 100644 index 03a8ffc..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/block/MixinShulkerBoxBlockEntity.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.block; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.LootableContainerBlockEntity; -import net.minecraft.block.entity.ShulkerBoxBlockEntity; -import net.minecraft.inventory.SidedInventory; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(ShulkerBoxBlockEntity.class) -public abstract class MixinShulkerBoxBlockEntity extends LootableContainerBlockEntity implements SidedInventory { - - protected MixinShulkerBoxBlockEntity(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { - super(blockEntityType, blockPos, blockState); - } - - @Override - public void markDirty() { - super.markDirty(); - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncBlockEntityToClient(this)) { - ServerMain.LOGGER.debug("update ShulkerBoxBlockEntity: {}", this.pos); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinHorseBaseEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinHorseBaseEntity.java deleted file mode 100644 index 4ed776b..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinHorseBaseEntity.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.entity; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.entity.EntityType; -import net.minecraft.entity.JumpingMount; -import net.minecraft.entity.Saddleable; -import net.minecraft.entity.passive.AbstractHorseEntity; -import net.minecraft.entity.passive.AnimalEntity; -import net.minecraft.inventory.Inventory; -import net.minecraft.inventory.InventoryChangedListener; -import net.minecraft.world.World; -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; - -@Mixin(AbstractHorseEntity.class) -public abstract class MixinHorseBaseEntity extends AnimalEntity implements InventoryChangedListener, JumpingMount, Saddleable { - protected MixinHorseBaseEntity(EntityType entityType, World world) { - super(entityType, world); - } - - @Inject(method = "onInventoryChanged", at = @At(value = "HEAD")) - private void updateEntity(Inventory sender, CallbackInfo ci) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncEntityToClient(this)) { - ServerMain.LOGGER.debug("update HorseBaseEntity inventory: onInventoryChanged."); - } - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinMerchantEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinMerchantEntity.java deleted file mode 100644 index 6520c39..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinMerchantEntity.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.entity; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.entity.EntityType; -import net.minecraft.entity.Npc; -import net.minecraft.entity.passive.MerchantEntity; -import net.minecraft.entity.passive.PassiveEntity; -import net.minecraft.inventory.Inventory; -import net.minecraft.inventory.InventoryChangedListener; -import net.minecraft.inventory.SimpleInventory; -import net.minecraft.village.Merchant; -import net.minecraft.world.World; -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.CallbackInfo; - - -@Mixin(MerchantEntity.class) -public abstract class MixinMerchantEntity extends PassiveEntity implements Npc, Merchant, InventoryChangedListener { - @Final - @Shadow - private SimpleInventory inventory; - - protected MixinMerchantEntity(EntityType entityType, World world) { - super(entityType, world); - } - - - @Inject(method = "(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V", at = @At(value = "RETURN")) - private void addInventoryListener(EntityType entityType, World world, CallbackInfo info) { - if (this.getWorld().isClient()) { - return; - } - this.inventory.addListener(this); - } - - @Override - public void onInventoryChanged(Inventory inventory) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncEntityToClient(this)) { - ServerMain.LOGGER.debug("update villager inventory: onInventoryChanged."); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinStorageMinecartEntity.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinStorageMinecartEntity.java deleted file mode 100644 index 55d41f7..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/protocol/pcaSyncProtocol/entity/MixinStorageMinecartEntity.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.protocol.pcaSyncProtocol.entity; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import com.github.zly2006.carpetslsaddition.ServerMain; -import com.github.zly2006.carpetslsaddition.network.pca.PcaSyncProtocol; -import net.minecraft.entity.EntityType; -import net.minecraft.entity.vehicle.AbstractMinecartEntity; -import net.minecraft.entity.vehicle.StorageMinecartEntity; -import net.minecraft.inventory.Inventory; -import net.minecraft.screen.NamedScreenHandlerFactory; -import net.minecraft.world.World; -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; - -@Mixin(StorageMinecartEntity.class) -public abstract class MixinStorageMinecartEntity extends AbstractMinecartEntity implements Inventory, NamedScreenHandlerFactory { - protected MixinStorageMinecartEntity(EntityType entityType, World world) { - super(entityType, world); - } - - @Inject(method = "markDirty", at = @At(value = "RETURN")) - private void updateInventory(CallbackInfo ci) { - if (SLSCarpetSettings.pcaSyncProtocol && PcaSyncProtocol.syncEntityToClient(this)) { - ServerMain.LOGGER.debug("update StorageMinecartEntity inventory."); - } - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/screen/MixinScreenHandler.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/screen/MixinScreenHandler.java deleted file mode 100644 index 010c5d3..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/screen/MixinScreenHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.screen; - -import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil; -import me.fallenbreath.conditionalmixin.api.annotation.Condition; -import me.fallenbreath.conditionalmixin.api.annotation.Restriction; -import net.minecraft.item.ItemStack; -import net.minecraft.screen.ScreenHandler; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -@Restriction( - conflict = @Condition("pca") -) -@Mixin(ScreenHandler.class) -public class MixinScreenHandler { - @Redirect(method = "insertItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isStackable()Z", ordinal = 0)) - private boolean insertItemIsStackable(ItemStack itemStack) { - return ShulkerBoxItemUtil.isStackable(itemStack); - } - - @Redirect(method = "insertItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I")) - private int insertItemGetMaxCount0(ItemStack itemStack) { - return ShulkerBoxItemUtil.getMaxCount(itemStack); - } - - @Redirect(method = "internalOnSlotClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I", ordinal = -1)) - private int removeStackGetMaxCount(ItemStack itemStack) { - return ShulkerBoxItemUtil.getMaxCount(itemStack); - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/server/MixinPlayerManager.java b/src/main/java/com/github/zly2006/carpetslsaddition/mixin/server/MixinPlayerManager.java deleted file mode 100644 index 5fe4bbc..0000000 --- a/src/main/java/com/github/zly2006/carpetslsaddition/mixin/server/MixinPlayerManager.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.github.zly2006.carpetslsaddition.mixin.server; - -import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.PlayerManager; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.server.world.ServerWorld; -import net.minecraft.util.Identifier; -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.zip.CRC32; - -@Mixin(PlayerManager.class) -public class MixinPlayerManager { - @Inject(method = "sendWorldInfo", at = @At(value = "RETURN")) - public void preOnSendWorldInfo(ServerPlayerEntity player, ServerWorld world, CallbackInfo ci) { - if (SLSCarpetSettings.xaeroWorldName.equals("#none")) { - return; - } - Identifier xaeroworldmap = new Identifier("xaeroworldmap", "main"); - Identifier xaerominimap = new Identifier("xaerominimap", "main"); - - CRC32 crc = new CRC32(); - crc.update(SLSCarpetSettings.xaeroWorldName.getBytes()); - ByteBuf buf = Unpooled.buffer(); - buf.writeByte(0); - buf.writeInt((int) crc.getValue()); - - ServerPlayNetworking.send(player, xaeroworldmap, new PacketByteBuf(buf.duplicate())); - ServerPlayNetworking.send(player, xaerominimap, new PacketByteBuf(buf.duplicate())); - } -} diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/network/pca/PcaSyncProtocol.java b/src/main/java/com/github/zly2006/carpetslsaddition/network/pca/PcaSyncProtocol.java index 2207643..a81807d 100644 --- a/src/main/java/com/github/zly2006/carpetslsaddition/network/pca/PcaSyncProtocol.java +++ b/src/main/java/com/github/zly2006/carpetslsaddition/network/pca/PcaSyncProtocol.java @@ -42,383 +42,383 @@ import java.util.Set; import java.util.concurrent.locks.ReentrantLock; public class PcaSyncProtocol { - - public static final ReentrantLock lock = new ReentrantLock(true); - public static final ReentrantLock pairLock = new ReentrantLock(true); - // 发送包 - private static final Identifier ENABLE_PCA_SYNC_PROTOCOL = createId("enable_pca_sync_protocol"); - private static final Identifier DISABLE_PCA_SYNC_PROTOCOL = createId("disable_pca_sync_protocol"); - private static final Identifier UPDATE_ENTITY = createId("update_entity"); - private static final Identifier UPDATE_BLOCK_ENTITY = createId("update_block_entity"); - // 响应包 - private static final Identifier SYNC_BLOCK_ENTITY = createId("sync_block_entity"); - private static final Identifier SYNC_ENTITY = createId("sync_entity"); - private static final Identifier CANCEL_SYNC_BLOCK_ENTITY = createId("cancel_sync_block_entity"); - private static final Identifier CANCEL_SYNC_ENTITY = createId("cancel_sync_entity"); - private static final Map> playerWatchBlockPos = new HashMap<>(); - private static final Map> playerWatchEntity = new HashMap<>(); - private static final Map, Set> blockPosWatchPlayerSet = new HashMap<>(); - private static final Map, Set> entityWatchPlayerSet = new HashMap<>(); - private static final MutablePair identifierEntityPair = new MutablePair<>(); - private static final MutablePair identifierBlockPosPair = new MutablePair<>(); - - // 通知客户端服务器已启用 PcaSyncProtocol - public static void enablePcaSyncProtocol(@NotNull ServerPlayerEntity player) { - // 在这写如果是在 BC 端的情况下,ServerPlayNetworking.canSend 在这个时机调用会出现错误 - ServerMain.LOGGER.debug("Try enablePcaSyncProtocol: {}", player.getName().getString()); - // bc 端比较奇怪,canSend 工作不正常 - // if (ServerPlayNetworking.canSend(player, ENABLE_PCA_SYNC_PROTOCOL)) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - ServerPlayNetworking.send(player, ENABLE_PCA_SYNC_PROTOCOL, buf); - ServerMain.LOGGER.debug("send enablePcaSyncProtocol to {}!", player.getName().getString()); - lock.lock(); - lock.unlock(); - } - - // 通知客户端服务器已停用 PcaSyncProtocol - public static void disablePcaSyncProtocol(@NotNull ServerPlayerEntity player) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - ServerPlayNetworking.send(player, DISABLE_PCA_SYNC_PROTOCOL, buf); - ServerMain.LOGGER.debug("send disablePcaSyncProtocol to {}!", player.getName().getString()); - } - - // 通知客户端更新 Entity - // 包内包含 World 的 Identifier, entityId, entity 的 nbt 数据 - // 传输 World 是为了通知客户端该 Entity 属于哪个 World - public static void updateEntity(@NotNull ServerPlayerEntity player, @NotNull Entity entity) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeIdentifier(entity.getEntityWorld().getRegistryKey().getValue()); - buf.writeInt(entity.getId()); - buf.writeNbt(entity.writeNbt(new NbtCompound())); - ServerPlayNetworking.send(player, UPDATE_ENTITY, buf); - } - - // 通知客户端更新 BlockEntity - // 包内包含 World 的 Identifier, pos, blockEntity 的 nbt 数据 - // 传输 World 是为了通知客户端该 BlockEntity 属于哪个世界 - public static void updateBlockEntity(@NotNull ServerPlayerEntity player, @NotNull BlockEntity blockEntity) { - World world = blockEntity.getWorld(); - - // 在生成世界时可能会产生空指针 - if (world == null) { - return; - } - - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeIdentifier(world.getRegistryKey().getValue()); - buf.writeBlockPos(blockEntity.getPos()); - buf.writeNbt(blockEntity.createNbt()); - ServerPlayNetworking.send(player, UPDATE_BLOCK_ENTITY, buf); - } - - public static void init() { - ServerPlayNetworking.registerGlobalReceiver(SYNC_BLOCK_ENTITY, PcaSyncProtocol::syncBlockEntityHandler); - ServerPlayNetworking.registerGlobalReceiver(SYNC_ENTITY, PcaSyncProtocol::syncEntityHandler); - ServerPlayNetworking.registerGlobalReceiver(CANCEL_SYNC_BLOCK_ENTITY, PcaSyncProtocol::cancelSyncBlockEntityHandler); - ServerPlayNetworking.registerGlobalReceiver(CANCEL_SYNC_ENTITY, PcaSyncProtocol::cancelSyncEntityHandler); - ServerPlayConnectionEvents.JOIN.register(PcaSyncProtocol::onJoin); - ServerPlayConnectionEvents.DISCONNECT.register(PcaSyncProtocol::onDisconnect); - } - - private static void onDisconnect(ServerPlayNetworkHandler serverPlayNetworkHandler, MinecraftServer minecraftServer) { - if (SLSCarpetSettings.pcaSyncProtocol) { - ServerMain.LOGGER.debug("onDisconnect remove: {}", serverPlayNetworkHandler.player.getName().getString()); - } - } - - private static void onJoin(ServerPlayNetworkHandler serverPlayNetworkHandler, PacketSender packetSender, MinecraftServer minecraftServer) { - if (SLSCarpetSettings.pcaSyncProtocol) { - enablePcaSyncProtocol(serverPlayNetworkHandler.player); - } - } - - // 客户端通知服务端取消 BlockEntity 同步 - private static void cancelSyncBlockEntityHandler(MinecraftServer server, ServerPlayerEntity player, - ServerPlayNetworkHandler handler, PacketByteBuf buf, - PacketSender responseSender) { - if (!SLSCarpetSettings.pcaSyncProtocol) { - return; - } - ServerMain.LOGGER.debug("{} cancel watch blockEntity.", player.getName().getString()); - PcaSyncProtocol.clearPlayerWatchBlock(player); - } - - // 客户端通知服务端取消 Entity 同步 - private static void cancelSyncEntityHandler(MinecraftServer server, ServerPlayerEntity player, - ServerPlayNetworkHandler handler, PacketByteBuf buf, - PacketSender responseSender) { - if (!SLSCarpetSettings.pcaSyncProtocol) { - return; - } - ServerMain.LOGGER.debug("{} cancel watch entity.", player.getName().getString()); - PcaSyncProtocol.clearPlayerWatchEntity(player); - } - - // 客户端请求同步 BlockEntity - // 包内包含 pos - // 由于正常的场景一般不会跨世界请求数据,因此包内并不包含 World,以玩家所在的 World 为准 - private static void syncBlockEntityHandler(MinecraftServer server, ServerPlayerEntity player, - ServerPlayNetworkHandler handler, PacketByteBuf buf, - PacketSender responseSender) { - if (!SLSCarpetSettings.pcaSyncProtocol) { - return; - } - BlockPos pos = buf.readBlockPos(); - ServerWorld world = player.getServerWorld(); - BlockState blockState = world.getBlockState(pos); - clearPlayerWatchData(player); - ServerMain.LOGGER.debug("{} watch blockpos {}: {}", player.getName().getString(), pos, blockState); - - BlockEntity blockEntityAdj = null; - // 不是单个箱子则需要更新隔壁箱子 - if (blockState.getBlock() instanceof ChestBlock) { - if (blockState.get(ChestBlock.CHEST_TYPE) != ChestType.SINGLE) { - BlockPos posAdj = pos.offset(ChestBlock.getFacing(blockState)); - // The method in World now checks that the caller is from the same thread... - blockEntityAdj = world.getWorldChunk(posAdj).getBlockEntity(posAdj); - } - } else if (ServerMain.tisCarpetLoaded && blockState.isOf(Blocks.BARREL) && RuleHelper.getBooleanValue(getCarpetRule("largeBarrel"))) { - Direction directionOpposite = blockState.get(BarrelBlock.FACING).getOpposite(); - BlockPos posAdj = pos.offset(directionOpposite); - BlockState blockStateAdj = world.getBlockState(posAdj); - if (blockStateAdj.isOf(Blocks.BARREL) && blockStateAdj.get(BarrelBlock.FACING) == directionOpposite) { - blockEntityAdj = world.getWorldChunk(posAdj).getBlockEntity(posAdj); - } - } - - if (blockEntityAdj != null) { - updateBlockEntity(player, blockEntityAdj); - } - - // 本来想判断一下 blockState 类型做个白名单的,考虑到 client 已经做了判断就不在服务端做判断了 - // 就算被恶意攻击应该不会造成什么损失 - // 大不了 op 直接拉黑 - // The method in World now checks that the caller is from the same thread... - BlockEntity blockEntity = world.getWorldChunk(pos).getBlockEntity(pos); - if (blockEntity != null) { - updateBlockEntity(player, blockEntity); - } - - Pair pair = new ImmutablePair<>(player.getEntityWorld().getRegistryKey().getValue(), pos); - lock.lock(); - playerWatchBlockPos.put(player, pair); - if (!blockPosWatchPlayerSet.containsKey(pair)) { - blockPosWatchPlayerSet.put(pair, new HashSet<>()); - } - blockPosWatchPlayerSet.get(pair).add(player); - lock.unlock(); - } - - // 客户端请求同步 Entity - // 包内包含 entityId - // 由于正常的场景一般不会跨世界请求数据,因此包内并不包含 World,以玩家所在的 World 为准 - private static void syncEntityHandler(MinecraftServer server, ServerPlayerEntity player, - ServerPlayNetworkHandler handler, PacketByteBuf buf, - PacketSender responseSender) { - if (!SLSCarpetSettings.pcaSyncProtocol) { - return; - } - int entityId = buf.readInt(); - ServerWorld world = player.getServerWorld(); - Entity entity = world.getEntityById(entityId); - if (entity == null) { - ServerMain.LOGGER.debug("Can't find entity {}.", entityId); - } else { - clearPlayerWatchData(player); - if (entity instanceof PlayerEntity) { - if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.NOBODY) { - return; - } else if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.BOT) { - if (!(entity instanceof EntityPlayerMPFake)) { - return; - } - } else if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.OPS) { - if (!(entity instanceof EntityPlayerMPFake) && server.getPermissionLevel(player.getGameProfile()) < 2) { - return; - } - } else if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.OPS_AND_SELF) { - if (!(entity instanceof EntityPlayerMPFake) && - server.getPermissionLevel(player.getGameProfile()) < 2 && - entity != player) { - return; - } - } else if (SLSCarpetSettings.pcaSyncPlayerEntity != SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.EVERYONE) { - // wtf???? - ServerMain.LOGGER.warn("syncEntityHandler wtf???"); - return; - } - } - ServerMain.LOGGER.debug("{} watch entity {}: {}", player.getName().getString(), entityId, entity); - updateEntity(player, entity); - - Pair pair = new ImmutablePair<>(entity.getEntityWorld().getRegistryKey().getValue(), entity); - lock.lock(); - playerWatchEntity.put(player, pair); - if (!entityWatchPlayerSet.containsKey(pair)) { - entityWatchPlayerSet.put(pair, new HashSet<>()); - } - entityWatchPlayerSet.get(pair).add(player); - lock.unlock(); - } - } - - private static MutablePair getIdentifierEntityPair(Identifier identifier, Entity entity) { - pairLock.lock(); - identifierEntityPair.setLeft(identifier); - identifierEntityPair.setRight(entity); - pairLock.unlock(); - return identifierEntityPair; - } - - private static MutablePair getIdentifierBlockPosPair(Identifier identifier, BlockPos pos) { - pairLock.lock(); - identifierBlockPosPair.setLeft(identifier); - identifierBlockPosPair.setRight(pos); - pairLock.unlock(); - return identifierBlockPosPair; - } - - // 工具 - private static @Nullable Set getWatchPlayerList(@NotNull Entity entity) { - return entityWatchPlayerSet.get(getIdentifierEntityPair(entity.getEntityWorld().getRegistryKey().getValue(), entity)); - } - - private static @Nullable Set getWatchPlayerList(@NotNull World world, @NotNull BlockPos blockPos) { - return blockPosWatchPlayerSet.get(getIdentifierBlockPosPair(world.getRegistryKey().getValue(), blockPos)); - } - - public static boolean syncEntityToClient(@NotNull Entity entity) { - if (entity.getEntityWorld().isClient()) { - return false; - } - lock.lock(); - Set playerList = getWatchPlayerList(entity); - boolean ret = false; - if (playerList != null) { - for (ServerPlayerEntity player : playerList) { - updateEntity(player, entity); - ret = true; - } - } - lock.unlock(); - return ret; - } - - public static boolean syncBlockEntityToClient(@NotNull BlockEntity blockEntity) { - boolean ret = false; - World world = blockEntity.getWorld(); - BlockPos pos = blockEntity.getPos(); - // 在生成世界时可能会产生空指针 - if (world != null) { - if (world.isClient()) { - return false; - } - BlockState blockState = world.getBlockState(pos); - lock.lock(); - Set playerList = getWatchPlayerList(world, blockEntity.getPos()); - - Set playerListAdj = null; - - if (blockState.getBlock() instanceof ChestBlock) { - if (blockState.get(ChestBlock.CHEST_TYPE) != ChestType.SINGLE) { - // 如果是一个大箱子需要特殊处理 - // 上面不用 isOf 是为了考虑到陷阱箱的情况,陷阱箱继承自箱子 - BlockPos posAdj = pos.offset(ChestBlock.getFacing(blockState)); - playerListAdj = getWatchPlayerList(world, posAdj); - } - } else if (ServerMain.tisCarpetLoaded && blockState.isOf(Blocks.BARREL) && RuleHelper.getBooleanValue(getCarpetRule("largeBarrel"))) { - Direction directionOpposite = blockState.get(BarrelBlock.FACING).getOpposite(); - BlockPos posAdj = pos.offset(directionOpposite); - BlockState blockStateAdj = world.getBlockState(posAdj); - if (blockStateAdj.isOf(Blocks.BARREL) && blockStateAdj.get(BarrelBlock.FACING) == directionOpposite) { - playerListAdj = getWatchPlayerList(world, posAdj); - } - } - if (playerListAdj != null) { - if (playerList == null) { - playerList = playerListAdj; - } else { - playerList.addAll(playerListAdj); - } - } - - if (playerList != null) { - for (ServerPlayerEntity player : playerList) { - updateBlockEntity(player, blockEntity); - ret = true; - } - } - lock.unlock(); - } - return ret; - } - - private static void clearPlayerWatchEntity(ServerPlayerEntity player) { - lock.lock(); - Pair pair = playerWatchEntity.get(player); - if (pair != null) { - Set playerSet = entityWatchPlayerSet.get(pair); - playerSet.remove(player); - if (playerSet.isEmpty()) { - entityWatchPlayerSet.remove(pair); - } - playerWatchEntity.remove(player); - } - lock.unlock(); - } - - private static void clearPlayerWatchBlock(ServerPlayerEntity player) { - lock.lock(); - Pair pair = playerWatchBlockPos.get(player); - if (pair != null) { - Set playerSet = blockPosWatchPlayerSet.get(pair); - playerSet.remove(player); - if (playerSet.isEmpty()) { - blockPosWatchPlayerSet.remove(pair); - } - playerWatchBlockPos.remove(player); - } - lock.unlock(); - } - - // 停用 PcaSyncProtocol - public static void disablePcaSyncProtocolGlobal() { - lock.lock(); - playerWatchBlockPos.clear(); - playerWatchEntity.clear(); - blockPosWatchPlayerSet.clear(); - entityWatchPlayerSet.clear(); - lock.unlock(); - if (ServerMain.server != null) { - for (ServerPlayerEntity player : ServerMain.server.getPlayerManager().getPlayerList()) { - disablePcaSyncProtocol(player); - } - } - } - - // 启用 PcaSyncProtocol - public static void enablePcaSyncProtocolGlobal() { - if (ServerMain.server == null) { - return; - } - for (ServerPlayerEntity player : ServerMain.server.getPlayerManager().getPlayerList()) { - enablePcaSyncProtocol(player); - } - } - - - // 删除玩家数据 - public static void clearPlayerWatchData(ServerPlayerEntity player) { - PcaSyncProtocol.clearPlayerWatchBlock(player); - PcaSyncProtocol.clearPlayerWatchEntity(player); - } - - private static CarpetRule getCarpetRule(String rule){ - return CarpetServer.settingsManager.getCarpetRule(rule); - } - - @Contract(value = "_ -> new", pure = true) - private static @NotNull Identifier createId(String name) { - return new Identifier("pca", name); - } +// +// public static final ReentrantLock lock = new ReentrantLock(true); +// public static final ReentrantLock pairLock = new ReentrantLock(true); +// // 发送包 +// private static final Identifier ENABLE_PCA_SYNC_PROTOCOL = createId("enable_pca_sync_protocol"); +// private static final Identifier DISABLE_PCA_SYNC_PROTOCOL = createId("disable_pca_sync_protocol"); +// private static final Identifier UPDATE_ENTITY = createId("update_entity"); +// private static final Identifier UPDATE_BLOCK_ENTITY = createId("update_block_entity"); +// // 响应包 +// private static final Identifier SYNC_BLOCK_ENTITY = createId("sync_block_entity"); +// private static final Identifier SYNC_ENTITY = createId("sync_entity"); +// private static final Identifier CANCEL_SYNC_BLOCK_ENTITY = createId("cancel_sync_block_entity"); +// private static final Identifier CANCEL_SYNC_ENTITY = createId("cancel_sync_entity"); +// private static final Map> playerWatchBlockPos = new HashMap<>(); +// private static final Map> playerWatchEntity = new HashMap<>(); +// private static final Map, Set> blockPosWatchPlayerSet = new HashMap<>(); +// private static final Map, Set> entityWatchPlayerSet = new HashMap<>(); +// private static final MutablePair identifierEntityPair = new MutablePair<>(); +// private static final MutablePair identifierBlockPosPair = new MutablePair<>(); +// +// // 通知客户端服务器已启用 PcaSyncProtocol +// public static void enablePcaSyncProtocol(@NotNull ServerPlayerEntity player) { +// // 在这写如果是在 BC 端的情况下,ServerPlayNetworking.canSend 在这个时机调用会出现错误 +// ServerMain.LOGGER.debug("Try enablePcaSyncProtocol: {}", player.getName().getString()); +// // bc 端比较奇怪,canSend 工作不正常 +// // if (ServerPlayNetworking.canSend(player, ENABLE_PCA_SYNC_PROTOCOL)) { +// PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); +// ServerPlayNetworking.send(player, ENABLE_PCA_SYNC_PROTOCOL, buf); +// ServerMain.LOGGER.debug("send enablePcaSyncProtocol to {}!", player.getName().getString()); +// lock.lock(); +// lock.unlock(); +// } +// +// // 通知客户端服务器已停用 PcaSyncProtocol +// public static void disablePcaSyncProtocol(@NotNull ServerPlayerEntity player) { +// PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); +// ServerPlayNetworking.send(player, DISABLE_PCA_SYNC_PROTOCOL, buf); +// ServerMain.LOGGER.debug("send disablePcaSyncProtocol to {}!", player.getName().getString()); +// } +// +// // 通知客户端更新 Entity +// // 包内包含 World 的 Identifier, entityId, entity 的 nbt 数据 +// // 传输 World 是为了通知客户端该 Entity 属于哪个 World +// public static void updateEntity(@NotNull ServerPlayerEntity player, @NotNull Entity entity) { +// PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); +// buf.writeIdentifier(entity.getEntityWorld().getRegistryKey().getValue()); +// buf.writeInt(entity.getId()); +// buf.writeNbt(entity.writeNbt(new NbtCompound())); +// ServerPlayNetworking.send(player, UPDATE_ENTITY, buf); +// } +// +// // 通知客户端更新 BlockEntity +// // 包内包含 World 的 Identifier, pos, blockEntity 的 nbt 数据 +// // 传输 World 是为了通知客户端该 BlockEntity 属于哪个世界 +// public static void updateBlockEntity(@NotNull ServerPlayerEntity player, @NotNull BlockEntity blockEntity) { +// World world = blockEntity.getWorld(); +// +// // 在生成世界时可能会产生空指针 +// if (world == null) { +// return; +// } +// +// PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); +// buf.writeIdentifier(world.getRegistryKey().getValue()); +// buf.writeBlockPos(blockEntity.getPos()); +// buf.writeNbt(blockEntity.createNbt()); +// ServerPlayNetworking.send(player, UPDATE_BLOCK_ENTITY, buf); +// } +// +// public static void init() { +// ServerPlayNetworking.registerGlobalReceiver(SYNC_BLOCK_ENTITY, PcaSyncProtocol::syncBlockEntityHandler); +// ServerPlayNetworking.registerGlobalReceiver(SYNC_ENTITY, PcaSyncProtocol::syncEntityHandler); +// ServerPlayNetworking.registerGlobalReceiver(CANCEL_SYNC_BLOCK_ENTITY, PcaSyncProtocol::cancelSyncBlockEntityHandler); +// ServerPlayNetworking.registerGlobalReceiver(CANCEL_SYNC_ENTITY, PcaSyncProtocol::cancelSyncEntityHandler); +// ServerPlayConnectionEvents.JOIN.register(PcaSyncProtocol::onJoin); +// ServerPlayConnectionEvents.DISCONNECT.register(PcaSyncProtocol::onDisconnect); +// } +// +// private static void onDisconnect(ServerPlayNetworkHandler serverPlayNetworkHandler, MinecraftServer minecraftServer) { +// if (SLSCarpetSettings.pcaSyncProtocol) { +// ServerMain.LOGGER.debug("onDisconnect remove: {}", serverPlayNetworkHandler.player.getName().getString()); +// } +// } +// +// private static void onJoin(ServerPlayNetworkHandler serverPlayNetworkHandler, PacketSender packetSender, MinecraftServer minecraftServer) { +// if (SLSCarpetSettings.pcaSyncProtocol) { +// enablePcaSyncProtocol(serverPlayNetworkHandler.player); +// } +// } +// +// // 客户端通知服务端取消 BlockEntity 同步 +// private static void cancelSyncBlockEntityHandler(MinecraftServer server, ServerPlayerEntity player, +// ServerPlayNetworkHandler handler, PacketByteBuf buf, +// PacketSender responseSender) { +// if (!SLSCarpetSettings.pcaSyncProtocol) { +// return; +// } +// ServerMain.LOGGER.debug("{} cancel watch blockEntity.", player.getName().getString()); +// PcaSyncProtocol.clearPlayerWatchBlock(player); +// } +// +// // 客户端通知服务端取消 Entity 同步 +// private static void cancelSyncEntityHandler(MinecraftServer server, ServerPlayerEntity player, +// ServerPlayNetworkHandler handler, PacketByteBuf buf, +// PacketSender responseSender) { +// if (!SLSCarpetSettings.pcaSyncProtocol) { +// return; +// } +// ServerMain.LOGGER.debug("{} cancel watch entity.", player.getName().getString()); +// PcaSyncProtocol.clearPlayerWatchEntity(player); +// } +// +// // 客户端请求同步 BlockEntity +// // 包内包含 pos +// // 由于正常的场景一般不会跨世界请求数据,因此包内并不包含 World,以玩家所在的 World 为准 +// private static void syncBlockEntityHandler(MinecraftServer server, ServerPlayerEntity player, +// ServerPlayNetworkHandler handler, PacketByteBuf buf, +// PacketSender responseSender) { +// if (!SLSCarpetSettings.pcaSyncProtocol) { +// return; +// } +// BlockPos pos = buf.readBlockPos(); +// ServerWorld world = player.getServerWorld(); +// BlockState blockState = world.getBlockState(pos); +// clearPlayerWatchData(player); +// ServerMain.LOGGER.debug("{} watch blockpos {}: {}", player.getName().getString(), pos, blockState); +// +// BlockEntity blockEntityAdj = null; +// // 不是单个箱子则需要更新隔壁箱子 +// if (blockState.getBlock() instanceof ChestBlock) { +// if (blockState.get(ChestBlock.CHEST_TYPE) != ChestType.SINGLE) { +// BlockPos posAdj = pos.offset(ChestBlock.getFacing(blockState)); +// // The method in World now checks that the caller is from the same thread... +// blockEntityAdj = world.getWorldChunk(posAdj).getBlockEntity(posAdj); +// } +// } else if (ServerMain.tisCarpetLoaded && blockState.isOf(Blocks.BARREL) && RuleHelper.getBooleanValue(getCarpetRule("largeBarrel"))) { +// Direction directionOpposite = blockState.get(BarrelBlock.FACING).getOpposite(); +// BlockPos posAdj = pos.offset(directionOpposite); +// BlockState blockStateAdj = world.getBlockState(posAdj); +// if (blockStateAdj.isOf(Blocks.BARREL) && blockStateAdj.get(BarrelBlock.FACING) == directionOpposite) { +// blockEntityAdj = world.getWorldChunk(posAdj).getBlockEntity(posAdj); +// } +// } +// +// if (blockEntityAdj != null) { +// updateBlockEntity(player, blockEntityAdj); +// } +// +// // 本来想判断一下 blockState 类型做个白名单的,考虑到 client 已经做了判断就不在服务端做判断了 +// // 就算被恶意攻击应该不会造成什么损失 +// // 大不了 op 直接拉黑 +// // The method in World now checks that the caller is from the same thread... +// BlockEntity blockEntity = world.getWorldChunk(pos).getBlockEntity(pos); +// if (blockEntity != null) { +// updateBlockEntity(player, blockEntity); +// } +// +// Pair pair = new ImmutablePair<>(player.getEntityWorld().getRegistryKey().getValue(), pos); +// lock.lock(); +// playerWatchBlockPos.put(player, pair); +// if (!blockPosWatchPlayerSet.containsKey(pair)) { +// blockPosWatchPlayerSet.put(pair, new HashSet<>()); +// } +// blockPosWatchPlayerSet.get(pair).add(player); +// lock.unlock(); +// } +// +// // 客户端请求同步 Entity +// // 包内包含 entityId +// // 由于正常的场景一般不会跨世界请求数据,因此包内并不包含 World,以玩家所在的 World 为准 +// private static void syncEntityHandler(MinecraftServer server, ServerPlayerEntity player, +// ServerPlayNetworkHandler handler, PacketByteBuf buf, +// PacketSender responseSender) { +// if (!SLSCarpetSettings.pcaSyncProtocol) { +// return; +// } +// int entityId = buf.readInt(); +// ServerWorld world = player.getServerWorld(); +// Entity entity = world.getEntityById(entityId); +// if (entity == null) { +// ServerMain.LOGGER.debug("Can't find entity {}.", entityId); +// } else { +// clearPlayerWatchData(player); +// if (entity instanceof PlayerEntity) { +// if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.NOBODY) { +// return; +// } else if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.BOT) { +// if (!(entity instanceof EntityPlayerMPFake)) { +// return; +// } +// } else if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.OPS) { +// if (!(entity instanceof EntityPlayerMPFake) && server.getPermissionLevel(player.getGameProfile()) < 2) { +// return; +// } +// } else if (SLSCarpetSettings.pcaSyncPlayerEntity == SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.OPS_AND_SELF) { +// if (!(entity instanceof EntityPlayerMPFake) && +// server.getPermissionLevel(player.getGameProfile()) < 2 && +// entity != player) { +// return; +// } +// } else if (SLSCarpetSettings.pcaSyncPlayerEntity != SLSCarpetSettings.PCA_SYNC_PLAYER_ENTITY_OPTIONS.EVERYONE) { +// // wtf???? +// ServerMain.LOGGER.warn("syncEntityHandler wtf???"); +// return; +// } +// } +// ServerMain.LOGGER.debug("{} watch entity {}: {}", player.getName().getString(), entityId, entity); +// updateEntity(player, entity); +// +// Pair pair = new ImmutablePair<>(entity.getEntityWorld().getRegistryKey().getValue(), entity); +// lock.lock(); +// playerWatchEntity.put(player, pair); +// if (!entityWatchPlayerSet.containsKey(pair)) { +// entityWatchPlayerSet.put(pair, new HashSet<>()); +// } +// entityWatchPlayerSet.get(pair).add(player); +// lock.unlock(); +// } +// } +// +// private static MutablePair getIdentifierEntityPair(Identifier identifier, Entity entity) { +// pairLock.lock(); +// identifierEntityPair.setLeft(identifier); +// identifierEntityPair.setRight(entity); +// pairLock.unlock(); +// return identifierEntityPair; +// } +// +// private static MutablePair getIdentifierBlockPosPair(Identifier identifier, BlockPos pos) { +// pairLock.lock(); +// identifierBlockPosPair.setLeft(identifier); +// identifierBlockPosPair.setRight(pos); +// pairLock.unlock(); +// return identifierBlockPosPair; +// } +// +// // 工具 +// private static @Nullable Set getWatchPlayerList(@NotNull Entity entity) { +// return entityWatchPlayerSet.get(getIdentifierEntityPair(entity.getEntityWorld().getRegistryKey().getValue(), entity)); +// } +// +// private static @Nullable Set getWatchPlayerList(@NotNull World world, @NotNull BlockPos blockPos) { +// return blockPosWatchPlayerSet.get(getIdentifierBlockPosPair(world.getRegistryKey().getValue(), blockPos)); +// } +// +// public static boolean syncEntityToClient(@NotNull Entity entity) { +// if (entity.getEntityWorld().isClient()) { +// return false; +// } +// lock.lock(); +// Set playerList = getWatchPlayerList(entity); +// boolean ret = false; +// if (playerList != null) { +// for (ServerPlayerEntity player : playerList) { +// updateEntity(player, entity); +// ret = true; +// } +// } +// lock.unlock(); +// return ret; +// } +// +// public static boolean syncBlockEntityToClient(@NotNull BlockEntity blockEntity) { +// boolean ret = false; +// World world = blockEntity.getWorld(); +// BlockPos pos = blockEntity.getPos(); +// // 在生成世界时可能会产生空指针 +// if (world != null) { +// if (world.isClient()) { +// return false; +// } +// BlockState blockState = world.getBlockState(pos); +// lock.lock(); +// Set playerList = getWatchPlayerList(world, blockEntity.getPos()); +// +// Set playerListAdj = null; +// +// if (blockState.getBlock() instanceof ChestBlock) { +// if (blockState.get(ChestBlock.CHEST_TYPE) != ChestType.SINGLE) { +// // 如果是一个大箱子需要特殊处理 +// // 上面不用 isOf 是为了考虑到陷阱箱的情况,陷阱箱继承自箱子 +// BlockPos posAdj = pos.offset(ChestBlock.getFacing(blockState)); +// playerListAdj = getWatchPlayerList(world, posAdj); +// } +// } else if (ServerMain.tisCarpetLoaded && blockState.isOf(Blocks.BARREL) && RuleHelper.getBooleanValue(getCarpetRule("largeBarrel"))) { +// Direction directionOpposite = blockState.get(BarrelBlock.FACING).getOpposite(); +// BlockPos posAdj = pos.offset(directionOpposite); +// BlockState blockStateAdj = world.getBlockState(posAdj); +// if (blockStateAdj.isOf(Blocks.BARREL) && blockStateAdj.get(BarrelBlock.FACING) == directionOpposite) { +// playerListAdj = getWatchPlayerList(world, posAdj); +// } +// } +// if (playerListAdj != null) { +// if (playerList == null) { +// playerList = playerListAdj; +// } else { +// playerList.addAll(playerListAdj); +// } +// } +// +// if (playerList != null) { +// for (ServerPlayerEntity player : playerList) { +// updateBlockEntity(player, blockEntity); +// ret = true; +// } +// } +// lock.unlock(); +// } +// return ret; +// } +// +// private static void clearPlayerWatchEntity(ServerPlayerEntity player) { +// lock.lock(); +// Pair pair = playerWatchEntity.get(player); +// if (pair != null) { +// Set playerSet = entityWatchPlayerSet.get(pair); +// playerSet.remove(player); +// if (playerSet.isEmpty()) { +// entityWatchPlayerSet.remove(pair); +// } +// playerWatchEntity.remove(player); +// } +// lock.unlock(); +// } +// +// private static void clearPlayerWatchBlock(ServerPlayerEntity player) { +// lock.lock(); +// Pair pair = playerWatchBlockPos.get(player); +// if (pair != null) { +// Set playerSet = blockPosWatchPlayerSet.get(pair); +// playerSet.remove(player); +// if (playerSet.isEmpty()) { +// blockPosWatchPlayerSet.remove(pair); +// } +// playerWatchBlockPos.remove(player); +// } +// lock.unlock(); +// } +// +// // 停用 PcaSyncProtocol +// public static void disablePcaSyncProtocolGlobal() { +// lock.lock(); +// playerWatchBlockPos.clear(); +// playerWatchEntity.clear(); +// blockPosWatchPlayerSet.clear(); +// entityWatchPlayerSet.clear(); +// lock.unlock(); +// if (ServerMain.server != null) { +// for (ServerPlayerEntity player : ServerMain.server.getPlayerManager().getPlayerList()) { +// disablePcaSyncProtocol(player); +// } +// } +// } +// +// // 启用 PcaSyncProtocol +// public static void enablePcaSyncProtocolGlobal() { +// if (ServerMain.server == null) { +// return; +// } +// for (ServerPlayerEntity player : ServerMain.server.getPlayerManager().getPlayerList()) { +// enablePcaSyncProtocol(player); +// } +// } +// +// +// // 删除玩家数据 +// public static void clearPlayerWatchData(ServerPlayerEntity player) { +// PcaSyncProtocol.clearPlayerWatchBlock(player); +// PcaSyncProtocol.clearPlayerWatchEntity(player); +// } +// +// private static CarpetRule getCarpetRule(String rule){ +// return CarpetServer.settingsManager.getCarpetRule(rule); +// } +// +// @Contract(value = "_ -> new", pure = true) +// private static @NotNull Identifier createId(String name) { +// return new Identifier("pca", name); +// } } diff --git a/src/main/java/com/github/zly2006/carpetslsaddition/util/ShulkerBoxItemUtil.java b/src/main/java/com/github/zly2006/carpetslsaddition/util/ShulkerBoxItemUtil.java index bc8c266..aff02be 100644 --- a/src/main/java/com/github/zly2006/carpetslsaddition/util/ShulkerBoxItemUtil.java +++ b/src/main/java/com/github/zly2006/carpetslsaddition/util/ShulkerBoxItemUtil.java @@ -2,6 +2,9 @@ package com.github.zly2006.carpetslsaddition.util; import com.github.zly2006.carpetslsaddition.SLSCarpetSettings; import net.minecraft.block.ShulkerBoxBlock; +import net.minecraft.component.ComponentType; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.NbtComponent; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; @@ -13,11 +16,10 @@ public class ShulkerBoxItemUtil { public static boolean isEmptyShulkerBoxItem(ItemStack itemStack) { if (itemStack.getItem() instanceof BlockItem && ((BlockItem) itemStack.getItem()).getBlock() instanceof ShulkerBoxBlock) { - NbtCompound nbt = itemStack.getNbt(); - if (nbt != null && nbt.contains("BlockEntityTag", 10)) { - NbtCompound tag = nbt.getCompound("BlockEntityTag"); - if (tag.contains("Items", 9)) { - NbtList tagList = tag.getList("Items", 10); + NbtComponent tag = itemStack.getComponents().get(DataComponentTypes.BLOCK_ENTITY_DATA); + if (tag != null) { + if (tag.contains("Items")) { + NbtList tagList = tag.getNbt().getList("Items", 10); return tagList.size() <= 0; } } @@ -38,4 +40,4 @@ public class ShulkerBoxItemUtil { public static boolean isStackable(ItemStack itemStack) { return getMaxCount(itemStack) > 1 && (!itemStack.isDamageable() || !itemStack.isDamaged()); } -} \ No newline at end of file +} diff --git a/src/main/resources/slsaddition.mixins.json b/src/main/resources/slsaddition.mixins.json index a225cd3..369f403 100644 --- a/src/main/resources/slsaddition.mixins.json +++ b/src/main/resources/slsaddition.mixins.json @@ -22,26 +22,9 @@ "entity.MixinPlayerEntity", "entity.MixinServerPlayerEntity", "entity.MixinVillagerEntity", - "item.MixinDyeItem", - "item.MixinPotionItem", "player.AccessorPlayerAbilities", - "player.MixinPlayerInventory", - "protocol.pcaSyncProtocol.block.MixinAbstractFurnaceBlockEntity", - "protocol.pcaSyncProtocol.block.MixinBarrelBlockEntity", - "protocol.pcaSyncProtocol.block.MixinBeehiveBlockEntity", - "protocol.pcaSyncProtocol.block.MixinBrewingStandBlockEntity", - "protocol.pcaSyncProtocol.block.MixinChestBlockEntity", - "protocol.pcaSyncProtocol.block.MixinComparatorBlockEntity", - "protocol.pcaSyncProtocol.block.MixinDispenserBlockEntity", - "protocol.pcaSyncProtocol.block.MixinHopperBlockEntity", - "protocol.pcaSyncProtocol.block.MixinShulkerBoxBlockEntity", - "protocol.pcaSyncProtocol.entity.MixinHorseBaseEntity", - "protocol.pcaSyncProtocol.entity.MixinMerchantEntity", - "protocol.pcaSyncProtocol.entity.MixinStorageMinecartEntity", - "screen.MixinScreenHandler", "screen.MixinSlot", "server.MixinMinecraftServer", - "server.MixinPlayerManager", "world.MixinSleepManager", "world.MixinSpawnHelper", "world.MixinWorld"