Skip to content

Commit

Permalink
update to 1.0.2-alpha.2
Browse files Browse the repository at this point in the history
让容器预览支持服务端
  • Loading branch information
plusls committed Aug 9, 2020
1 parent 37fa55b commit 9fd8b30
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ malilib 创建的配置界面没有考虑到汉化的情况,在加载汉化资

minihud 的查看结构和史莱姆区块渲染需要服务端安装了 carpet 或者 servux 才能正常使用(史莱姆区块可以手动输入种子),在安装此 mod 后,若是服务器未安装上述 2个 mod 但是安装了 bbor, 该 mod 可以自动从服务器获取 bbor 的数据并导入 minihud,从而让 minihud 在多人游戏中也能渲染结构和史莱姆区块。

### 多人游戏容器预览

tweakeroo 的容器预览功能在多人游戏中是不可用的,在安装此 mod 后可以让该功能在多人游戏中使用(需要服务端也安装 MasaGadget)

## 安装

该 mod 依赖 malilib, minihud, fabric-api
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies {

modCompile "fi.dy.masa.malilib:malilib-fabric-${project.minecraft_version}:${project.malilib_version}"
modCompile "fi.dy.masa.minihud:minihud-fabric-${project.minecraft_version}:${project.minihud_version}"

modCompile "fi.dy.masa.tweakeroo:tweakeroo-fabric-${project.minecraft_version}:${project.tweakeroo_version}"
}

processResources {
Expand Down
5 changes: 4 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.9.0+build.204

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

# malilib version
malilib_version = 0.10.0-dev.21+arne.1
# minihud version
minihud_version = 0.19.0-dev.20200720.162605
# tweakeroo version
tweakeroo_version = 0.10.0-dev.20200720.162627


# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/io/github/plusls/MasaGadget/MasaGadgetMod.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.github.plusls.MasaGadget;

import io.github.plusls.MasaGadget.network.ClientNetworkHandler;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketByteBuf;
Expand All @@ -11,19 +14,22 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

public class MasaGadgetMod implements ModInitializer {
public class MasaGadgetMod implements ModInitializer, ClientModInitializer {
public static final String MODID = "masa_gadget_mod";
public static final Logger LOGGER = LogManager.getLogger("MasaGadgetMod");
public static boolean bborCompat = false;
public static CustomPayloadC2SPacket BBOR_SUBSCRIBE_PACKET = null;
public static String level = "INFO";
public static boolean masaGagdetInServer = false;

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
// System.out.println("Hello Fabric world!");
Configurator.setLevel(LOGGER.getName(), Level.toLevel(MasaGadgetMod.level));
ServerNetworkHandler.init();
}

@Override
public void onInitializeClient() {
if (FabricLoader.getInstance().isModLoaded("bbor")) {
LOGGER.info("BBOR detected.");
bborCompat = true;
Expand All @@ -32,7 +38,7 @@ public void onInitialize() {
new Identifier("bbor", "subscribe"),
new PacketByteBuf(Unpooled.buffer()));
}
Configurator.setLevel(LOGGER.getName(), Level.toLevel(MasaGadgetMod.level));
ClientNetworkHandler.init();
}

public static Identifier id(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin;
package io.github.plusls.MasaGadget.mixin.client;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.plusls.MasaGadget.mixin.client;

import net.minecraft.block.entity.*;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
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(ClientPlayNetworkHandler.class)
public abstract class MixinClientPlayNetworkHandler implements ClientPlayPacketListener {

@Shadow
private MinecraftClient client;

@Inject(method = "onBlockEntityUpdate(Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;)V",
at = @At(value = "RETURN"))
private void postOnBlockEntityUpdate(BlockEntityUpdateS2CPacket packet, CallbackInfo info) {
int blockEntityType = packet.getBlockEntityType();
BlockEntity blockEntity = this.client.world.getBlockEntity(packet.getPos());
if (blockEntityType == 0 && (
blockEntity instanceof ShulkerBoxBlockEntity ||
blockEntity instanceof HopperBlockEntity ||
blockEntity instanceof AbstractFurnaceBlockEntity ||
blockEntity instanceof DispenserBlockEntity // 包括了投掷器
)) {
blockEntity.fromTag(this.client.world.getBlockState(blockEntity.getPos()), packet.getCompoundTag());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin;
package io.github.plusls.MasaGadget.mixin.client;

import fi.dy.masa.minihud.util.DataStorage;
import io.github.plusls.MasaGadget.MasaGadgetMod;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.plusls.MasaGadget.mixin.client;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
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({MinecraftClient.class})
public abstract class MixinMinecraftClient {
@Inject(
method = {"disconnect(Lnet/minecraft/client/gui/screen/Screen;)V"},
at = {@At("HEAD")}
)
private void onDisconnectPre(Screen screen, CallbackInfo ci) {
MasaGadgetMod.masaGagdetInServer = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin;
package io.github.plusls.MasaGadget.mixin.client;

import fi.dy.masa.minihud.util.DataStorage;
import io.github.plusls.MasaGadget.util.ParseBborPacket;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin;
package io.github.plusls.MasaGadget.mixin.client.malilib;

import fi.dy.masa.minihud.util.DataStorage;
import io.github.plusls.MasaGadget.util.ParseBborPacket;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.plusls.MasaGadget.mixin.client.malilib;

import fi.dy.masa.malilib.util.InventoryUtils;
import io.github.plusls.MasaGadget.network.DataAccessor;
import net.minecraft.inventory.Inventory;
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;

@Mixin(value = InventoryUtils.class, remap = false)
public abstract class MixinInventoryUtils {
@Inject(method = "getInventory",
at = @At(value = "HEAD"))
private static void preGetInventory(World world, BlockPos pos, CallbackInfoReturnable<Inventory> info) {
DataAccessor.requestBlockEntity(pos);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.plusls.MasaGadget.mixin;
package io.github.plusls.MasaGadget.mixin.client.malilib;

import com.google.common.collect.ImmutableList;
import fi.dy.masa.malilib.config.IConfigBase;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.plusls.MasaGadget.mixin.client.tweakeroo;

import fi.dy.masa.malilib.hotkeys.IKeybind;
import fi.dy.masa.malilib.interfaces.IRenderer;
import fi.dy.masa.tweakeroo.event.RenderHandler;
import io.github.plusls.MasaGadget.MasaGadgetMod;
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.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;


@Mixin(value = RenderHandler.class, remap = false)
public abstract class MixinRenderHandler implements IRenderer {
// 未按下按键时若是 lastBlockPos 不为空, 则告诉服务端不需要更新 block entity
@Redirect(method = "onRenderGameOverlayPost",
at = @At(value = "INVOKE",
target = "Lfi/dy/masa/malilib/hotkeys/IKeybind;isKeybindHeld()Z",
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));
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.plusls.MasaGadget.mixin.server;

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;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
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(PlayerManager.class)
public abstract class MixinPlayerManager {

@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",
ordinal = 0
)
)
private void onOnPlayerConnect(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, ClientNetworkHandler.HELLO, buf);
MasaGadgetMod.LOGGER.debug("send hello!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.block.entity.LockableContainerBlockEntity;
import net.minecraft.inventory.SidedInventory;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.recipe.RecipeInputProvider;
import net.minecraft.recipe.RecipeUnlocker;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Tickable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(AbstractFurnaceBlockEntity.class)
public abstract class MixinAbstractFurnaceBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider, Tickable {

@Shadow
public abstract CompoundTag toTag(CompoundTag tag);

public MixinAbstractFurnaceBlockEntity() {
super(null);
}

@Override
public void markDirty() {
super.markDirty();
if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) {
((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos());
MasaGadgetMod.LOGGER.debug("update AbstractFurnaceBlockEntity: {}", this.pos);
}
}

@Override
public BlockEntityUpdateS2CPacket toUpdatePacket() {
return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
import net.minecraft.block.entity.DispenserBlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.server.world.ServerWorld;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;


@Mixin(DispenserBlockEntity.class)
public abstract class MixinDispenserBlockEntity extends LootableContainerBlockEntity {

@Shadow
public abstract CompoundTag toTag(CompoundTag tag);

public MixinDispenserBlockEntity() {
super(null);
}

@Override
public void markDirty() {
super.markDirty();
if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) {
((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos());
MasaGadgetMod.LOGGER.debug("update DispenserBlockEntity: {}", this.pos);
}
}

@Override
public BlockEntityUpdateS2CPacket toUpdatePacket() {
return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.plusls.MasaGadget.mixin.server.inventory;

import io.github.plusls.MasaGadget.MasaGadgetMod;
import io.github.plusls.MasaGadget.network.ServerNetworkHandler;
import net.minecraft.block.entity.Hopper;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Tickable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(HopperBlockEntity.class)
public abstract class MixinHopperBlockEntity extends LootableContainerBlockEntity implements Hopper, Tickable {

@Shadow
public abstract CompoundTag toTag(CompoundTag tag);

public MixinHopperBlockEntity() {
super(null);
}

@Override
public void markDirty() {
super.markDirty();
if (ServerNetworkHandler.lastBlockPosMap.containsValue(this.pos)) {
((ServerWorld) this.getWorld()).getChunkManager().markForUpdate(this.getPos());
MasaGadgetMod.LOGGER.debug("update HopperBlockEntity: {}", this.pos);
}
}

@Override
public BlockEntityUpdateS2CPacket toUpdatePacket() {
return new BlockEntityUpdateS2CPacket(this.pos, 0, this.toTag(new CompoundTag()));
}
}
Loading

0 comments on commit 9fd8b30

Please sign in to comment.