generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 29
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
16 changed files
with
268 additions
and
50 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
40 changes: 40 additions & 0 deletions
40
src/main/java/io/github/plusls/MasaGadget/mixin/client/MixinAbstractTraderEntity.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,40 @@ | ||
package io.github.plusls.MasaGadget.mixin.client; | ||
|
||
import net.minecraft.entity.Npc; | ||
import net.minecraft.entity.passive.AbstractTraderEntity; | ||
import net.minecraft.entity.passive.PassiveEntity; | ||
import net.minecraft.inventory.InventoryChangedListener; | ||
import net.minecraft.inventory.SimpleInventory; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.village.Trader; | ||
import net.minecraft.world.World; | ||
import org.spongepowered.asm.mixin.Dynamic; | ||
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(AbstractTraderEntity.class) | ||
public abstract class MixinAbstractTraderEntity extends PassiveEntity implements Npc, Trader, InventoryChangedListener { | ||
@Final | ||
@Dynamic | ||
@Shadow | ||
private SimpleInventory inventory; | ||
|
||
public MixinAbstractTraderEntity(World world) { | ||
super(null, null); | ||
} | ||
|
||
// mojang 的 SimpleInventory 实现的有问题,readTags 时不会清空原有数据需要手动清空 | ||
@Inject(method = "readCustomDataFromTag(Lnet/minecraft/nbt/CompoundTag;)V", | ||
at = @At(value = "INVOKE", | ||
target = "Lnet/minecraft/inventory/SimpleInventory;readTags(Lnet/minecraft/nbt/ListTag;)V", | ||
ordinal = 0)) | ||
private void preReadTags(CompoundTag tag, CallbackInfo info) { | ||
if (this.world.isClient()) { | ||
this.inventory.clear(); | ||
} | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
src/main/java/io/github/plusls/MasaGadget/mixin/client/malilib/MixinInventoryUtils.java
This file was deleted.
Oops, something went wrong.
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
42 changes: 42 additions & 0 deletions
42
src/main/java/io/github/plusls/MasaGadget/mixin/client/tweakeroo/MixinRenderUtils.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,42 @@ | ||
package io.github.plusls.MasaGadget.mixin.client.tweakeroo; | ||
|
||
import fi.dy.masa.malilib.util.InventoryUtils; | ||
import fi.dy.masa.tweakeroo.renderer.RenderUtils; | ||
import io.github.plusls.MasaGadget.network.DataAccessor; | ||
import net.minecraft.block.entity.*; | ||
import net.minecraft.entity.passive.VillagerEntity; | ||
import net.minecraft.inventory.Inventory; | ||
import net.minecraft.inventory.SimpleInventory; | ||
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.Redirect; | ||
|
||
@Mixin(value = RenderUtils.class, remap = false) | ||
public abstract class MixinRenderUtils { | ||
@Redirect(method = "renderInventoryOverlay", | ||
at = @At(value = "INVOKE", | ||
target = "Lfi/dy/masa/malilib/util/InventoryUtils;getInventory(Lnet/minecraft/class_1937;Lnet/minecraft/class_2338;)Lnet/minecraft/class_1263;", | ||
ordinal = 0)) | ||
private static Inventory redirectGetBlockInventory(World world, BlockPos pos) { | ||
BlockEntity blockEntity = world.getWorldChunk(pos).getBlockEntity(pos); | ||
if (blockEntity instanceof AbstractFurnaceBlockEntity || | ||
blockEntity instanceof DispenserBlockEntity || | ||
blockEntity instanceof HopperBlockEntity || | ||
blockEntity instanceof ShulkerBoxBlockEntity | ||
) { | ||
DataAccessor.requestBlockEntity(pos); | ||
} | ||
return InventoryUtils.getInventory(world, pos); | ||
} | ||
|
||
@Redirect(method = "renderInventoryOverlay", | ||
at = @At(value = "INVOKE", | ||
target = "Lnet/minecraft/entity/passive/VillagerEntity;getInventory()Lnet/minecraft/inventory/SimpleInventory;", | ||
ordinal = 0, remap = true)) | ||
private static SimpleInventory redirectGetVillagerInventory(VillagerEntity entity) { | ||
DataAccessor.requestEntity(entity.getEntityId()); | ||
return entity.getInventory(); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/io/github/plusls/MasaGadget/mixin/server/MixinServerPlayNetworkHandler.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,29 @@ | ||
package io.github.plusls.MasaGadget.mixin.server; | ||
|
||
import io.github.plusls.MasaGadget.network.ServerNetworkHandler; | ||
import net.minecraft.network.listener.PacketListener; | ||
import net.minecraft.server.network.ServerPlayNetworkHandler; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Text; | ||
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; | ||
|
||
import java.util.UUID; | ||
|
||
|
||
@Mixin(ServerPlayNetworkHandler.class) | ||
public abstract class MixinServerPlayNetworkHandler implements PacketListener { | ||
@Shadow | ||
public ServerPlayerEntity player; | ||
|
||
// 玩家离线后从 ServerNetworkHandler 中剔除玩家 | ||
@Inject(method = "onDisconnected(Lnet/minecraft/text/Text;)V", at = @At("HEAD")) | ||
private void onDisconnect(Text reason, CallbackInfo ci) { | ||
UUID playerUuid = player.getUuid(); | ||
ServerNetworkHandler.lastBlockPosMap.remove(playerUuid); | ||
ServerNetworkHandler.lastEntityUuidMap.remove(playerUuid); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tory/MixinAbstractFurnaceBlockEntity.java → ...lock/MixinAbstractFurnaceBlockEntity.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
2 changes: 1 addition & 1 deletion
2
.../inventory/MixinDispenserBlockEntity.java → ...rver/block/MixinDispenserBlockEntity.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
2 changes: 1 addition & 1 deletion
2
...ver/inventory/MixinHopperBlockEntity.java → .../server/block/MixinHopperBlockEntity.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
2 changes: 1 addition & 1 deletion
2
...inventory/MixinShulkerBoxBlockEntity.java → ...ver/block/MixinShulkerBoxBlockEntity.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
64 changes: 64 additions & 0 deletions
64
src/main/java/io/github/plusls/MasaGadget/mixin/server/entity/MixinAbstractTraderEntity.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,64 @@ | ||
package io.github.plusls.MasaGadget.mixin.server.entity; | ||
|
||
import io.github.plusls.MasaGadget.MasaGadgetMod; | ||
import io.github.plusls.MasaGadget.network.ClientNetworkHandler; | ||
import io.github.plusls.MasaGadget.network.ServerNetworkHandler; | ||
import io.netty.buffer.Unpooled; | ||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; | ||
import net.minecraft.entity.EntityType; | ||
import net.minecraft.entity.Npc; | ||
import net.minecraft.entity.passive.AbstractTraderEntity; | ||
import net.minecraft.entity.passive.PassiveEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.inventory.Inventory; | ||
import net.minecraft.inventory.InventoryChangedListener; | ||
import net.minecraft.inventory.SimpleInventory; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.village.Trader; | ||
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; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Mixin(AbstractTraderEntity.class) | ||
public abstract class MixinAbstractTraderEntity extends PassiveEntity implements Npc, Trader, InventoryChangedListener { | ||
@Final | ||
@Shadow | ||
private SimpleInventory inventory; | ||
|
||
public MixinAbstractTraderEntity(World world) { | ||
super(null, null); | ||
} | ||
|
||
@Inject(method = "<init>(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V", at = @At(value = "RETURN")) | ||
private void postInit(EntityType<? extends AbstractTraderEntity> entityType, World world, CallbackInfo info) { | ||
if (this.world.isClient()) { | ||
return; | ||
} | ||
this.inventory.addListener(this); | ||
} | ||
|
||
@Override | ||
public void onInventoryChanged(Inventory inventory) { | ||
int entityId = this.getEntityId(); | ||
for (Map.Entry<UUID, Integer> entry : ServerNetworkHandler.lastEntityUuidMap.entrySet()) { | ||
if (entry.getValue() == entityId) { | ||
PlayerEntity player = (PlayerEntity) ((ServerWorld) this.world).getEntity(entry.getKey()); | ||
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); | ||
buf.writeInt(entityId); | ||
buf.writeCompoundTag(this.toTag(new CompoundTag())); | ||
ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, ClientNetworkHandler.RESPONSE_ENTITY, buf); | ||
MasaGadgetMod.LOGGER.debug("update villager inventory: onInventoryChanged."); | ||
return; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.