Skip to content

Commit

Permalink
update to 1.0.3-alpha.1
Browse files Browse the repository at this point in the history
新增查看村民背包功能
  • Loading branch information
plusls committed Aug 10, 2020
1 parent 9fd8b30 commit b2c222a
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 50 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.9.0+build.204

# Mod Properties
mod_version = 1.0.2-alpha.2
mod_version = 1.0.3-alpha.1
maven_group = io.github.plusls
archives_base_name = MasaGadget

Expand Down
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();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import io.github.plusls.MasaGadget.network.DataAccessor;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
import io.netty.buffer.Unpooled;
import net.minecraft.client.MinecraftClient;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -24,13 +23,21 @@ public abstract class MixinRenderHandler implements IRenderer {
ordinal = 2))
private boolean redirectIsKeyBindHeld(IKeybind iKeybind) {
boolean ret = iKeybind.isKeybindHeld();

if (!ret && DataAccessor.lastBlockPos != null) {
DataAccessor.lastBlockPos = null;
MasaGadgetMod.LOGGER.debug("cancel requestBlockEntity");
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBoolean(false);
MinecraftClient.getInstance().getNetworkHandler().getConnection().send(new CustomPayloadC2SPacket(ServerNetworkHandler.REQUEST_BLOCK_ENTITY, buf));
if (!ret) {
if (DataAccessor.lastBlockPos != null) {
DataAccessor.lastBlockPos = null;
MasaGadgetMod.LOGGER.debug("cancel requestBlockEntity");
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBoolean(false);
ClientSidePacketRegistry.INSTANCE.sendToServer(ServerNetworkHandler.REQUEST_BLOCK_ENTITY, buf);
}
if (DataAccessor.lastEntityId != -1) {
DataAccessor.lastEntityId = -1;
MasaGadgetMod.LOGGER.debug("cancel requestEntity");
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBoolean(false);
ClientSidePacketRegistry.INSTANCE.sendToServer(ServerNetworkHandler.REQUEST_ENTITY, buf);
}
}
return ret;
}
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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.network.ClientConnection;
Expand All @@ -16,7 +15,7 @@

@Mixin(PlayerManager.class)
public abstract class MixinPlayerManager {

// 玩家上线后发送 hello 包,标明服务器存在 MasaGadget
@Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/packet/s2c/play/DifficultyS2CPacket;<init>(Lnet/minecraft/world/Difficulty;Z)V",
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;
package io.github.plusls.MasaGadget.mixin.server.block;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;
package io.github.plusls.MasaGadget.mixin.server.block;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;
package io.github.plusls.MasaGadget.mixin.server.block;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;
package io.github.plusls.MasaGadget.mixin.server.block;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
Expand Down
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.network.PacketContext;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;

public class ClientNetworkHandler {
public static final Identifier HELLO = MasaGadgetMod.id("hello");
public static final Identifier RESPONSE_ENTITY = MasaGadgetMod.id("response_entity");

public static void init() {
ClientSidePacketRegistry.INSTANCE.register(HELLO, ClientNetworkHandler::helloHandler);
ClientSidePacketRegistry.INSTANCE.register(RESPONSE_ENTITY, ClientNetworkHandler::responseEntityHandler);
}

private static void helloHandler(PacketContext packetContext, PacketByteBuf packetByteBuf) {
Expand All @@ -20,4 +26,17 @@ private static void helloHandler(PacketContext packetContext, PacketByteBuf pack
MasaGadgetMod.masaGagdetInServer = true;
}
}

// 反序列化实体数据
private static void responseEntityHandler(PacketContext packetContext, PacketByteBuf packetByteBuf) {
PlayerEntity player = packetContext.getPlayer();
World world = player.world;
int entityId = packetByteBuf.readInt();
CompoundTag tag = packetByteBuf.readCompoundTag();
Entity entity = world.getEntityById(entityId);
if (entity != null) {
MasaGadgetMod.LOGGER.debug("update entity!");
entity.fromTag(tag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.math.BlockPos;


@Environment(EnvType.CLIENT)
public class DataAccessor {
static public BlockPos lastBlockPos = null;
static public int lastEntityId = -1;

@Environment(EnvType.CLIENT)
static public void requestBlockEntity(BlockPos pos) {
if (!MasaGadgetMod.masaGagdetInServer) {
return;
}
if (MinecraftClient.getInstance().getNetworkHandler() == null) {
return;
}
if (lastBlockPos != null && lastBlockPos.equals(pos)) {
return;
}
Expand All @@ -31,4 +27,19 @@ static public void requestBlockEntity(BlockPos pos) {
buf.writeBlockPos(pos);
ClientSidePacketRegistry.INSTANCE.sendToServer(ServerNetworkHandler.REQUEST_BLOCK_ENTITY, buf);
}

static public void requestEntity(int entityId) {
if (!MasaGadgetMod.masaGagdetInServer) {
return;
}
if (lastEntityId != -1 && lastEntityId == entityId) {
return;
}
MasaGadgetMod.LOGGER.debug("requestEntity: {}", entityId);
lastEntityId = entityId;
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBoolean(true);
buf.writeInt(entityId);
ClientSidePacketRegistry.INSTANCE.sendToServer(ServerNetworkHandler.REQUEST_ENTITY, buf);
}
}
Loading

0 comments on commit b2c222a

Please sign in to comment.