use of gregtech.api.gui.impl.ModularUIContainer in project GregTech by GregTechCE.
the class NetworkHandler method init.
public static void init() {
channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(GTValues.MODID);
channel.register(new NetworkHandler());
PacketEncoder<PacketUIWidgetUpdate> widgetUpdateEncoder = (packet, buf) -> {
buf.writeVarInt(packet.updateData.readableBytes());
buf.writeBytes(packet.updateData);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.widgetId);
};
PacketDecoder<PacketUIWidgetUpdate> widgetUpdateDecoder = (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
return new PacketUIWidgetUpdate(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
};
registerPacket(1, PacketUIOpen.class, new PacketCodec<>((packet, buf) -> {
buf.writeVarInt(packet.serializedHolder.readableBytes());
buf.writeBytes(packet.serializedHolder);
buf.writeVarInt(packet.uiFactoryId);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.initialWidgetUpdates.size());
for (PacketUIWidgetUpdate widgetUpdate : packet.initialWidgetUpdates) {
widgetUpdateEncoder.encode(widgetUpdate, buf);
}
}, (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
int uiFactoryId = buf.readVarInt();
int windowId = buf.readVarInt();
ArrayList<PacketUIWidgetUpdate> initialWidgetUpdates = new ArrayList<>();
int initialWidgetUpdatesCount = buf.readVarInt();
for (int i = 0; i < initialWidgetUpdatesCount; i++) {
initialWidgetUpdates.add(widgetUpdateDecoder.decode(buf));
}
return new PacketUIOpen(uiFactoryId, new PacketBuffer(copiedDataBuffer), windowId, initialWidgetUpdates);
}));
registerPacket(2, PacketUIWidgetUpdate.class, new PacketCodec<>((packet, buf) -> {
buf.writeVarInt(packet.updateData.readableBytes());
buf.writeBytes(packet.updateData);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.widgetId);
}, (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
return new PacketUIWidgetUpdate(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
}));
registerPacket(3, PacketUIClientAction.class, new PacketCodec<>((packet, buf) -> {
buf.writeVarInt(packet.updateData.readableBytes());
buf.writeBytes(packet.updateData);
buf.writeVarInt(packet.windowId);
buf.writeVarInt(packet.widgetId);
}, (buf) -> {
ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
directSliceBuffer.release();
return new PacketUIClientAction(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
}));
registerPacket(4, PacketBlockParticle.class, new PacketCodec<>((packet, buf) -> {
buf.writeBlockPos(packet.blockPos);
buf.writeFloat((float) packet.entityPos.x);
buf.writeFloat((float) packet.entityPos.y);
buf.writeFloat((float) packet.entityPos.z);
buf.writeVarInt(packet.particlesAmount);
}, (buf) -> new PacketBlockParticle(buf.readBlockPos(), new Vector3(buf.readFloat(), buf.readFloat(), buf.readFloat()), buf.readVarInt())));
registerPacket(5, PacketClipboard.class, new PacketCodec<>((packet, buf) -> {
buf.writeString(packet.text);
}, (buf) -> new PacketClipboard(buf.readString(32767))));
registerServerExecutor(PacketUIClientAction.class, (packet, handler) -> {
Container openContainer = handler.player.openContainer;
if (openContainer instanceof ModularUIContainer && openContainer.windowId == packet.windowId) {
ModularUI modularUI = ((ModularUIContainer) openContainer).getModularUI();
PacketBuffer buffer = packet.updateData;
modularUI.guiWidgets.get(packet.widgetId).handleClientAction(buffer.readVarInt(), buffer);
}
});
if (FMLCommonHandler.instance().getSide().isClient()) {
initClient();
}
}
use of gregtech.api.gui.impl.ModularUIContainer in project GregTech by GregTechCE.
the class GTUtility method findPlayersUsing.
public static List<EntityPlayerMP> findPlayersUsing(MetaTileEntity metaTileEntity, double radius) {
ArrayList<EntityPlayerMP> result = new ArrayList<>();
AxisAlignedBB box = new AxisAlignedBB(metaTileEntity.getPos()).expand(radius, radius, radius).expand(-radius, -radius, -radius);
List<EntityPlayerMP> entities = metaTileEntity.getWorld().getEntitiesWithinAABB(EntityPlayerMP.class, box);
for (EntityPlayerMP player : entities) {
if (player.openContainer instanceof ModularUIContainer) {
ModularUI modularUI = ((ModularUIContainer) player.openContainer).getModularUI();
if (modularUI.holder instanceof MetaTileEntityHolder && ((MetaTileEntityHolder) modularUI.holder).getMetaTileEntity() == metaTileEntity) {
result.add(player);
}
}
}
return result;
}
use of gregtech.api.gui.impl.ModularUIContainer in project GregTech by GregTechCE.
the class UIFactory method openUI.
public final void openUI(E holder, EntityPlayerMP player) {
if (player instanceof FakePlayer) {
return;
}
ModularUI uiTemplate = createUITemplate(holder, player);
uiTemplate.initWidgets();
player.getNextWindowId();
player.closeContainer();
int currentWindowId = player.currentWindowId;
PacketBuffer serializedHolder = new PacketBuffer(Unpooled.buffer());
writeHolderToSyncData(serializedHolder, holder);
int uiFactoryId = FACTORY_REGISTRY.getIDForObject(this);
ModularUIContainer container = new ModularUIContainer(uiTemplate);
container.windowId = currentWindowId;
// accumulate all initial updates of widgets in open packet
container.accumulateWidgetUpdateData = true;
uiTemplate.guiWidgets.values().forEach(Widget::detectAndSendChanges);
container.accumulateWidgetUpdateData = false;
ArrayList<PacketUIWidgetUpdate> updateData = new ArrayList<>(container.accumulatedUpdates);
container.accumulatedUpdates.clear();
PacketUIOpen packet = new PacketUIOpen(uiFactoryId, serializedHolder, currentWindowId, updateData);
NetworkHandler.channel.sendTo(NetworkHandler.packet2proxy(packet), player);
container.addListener(player);
player.openContainer = container;
// and fire forge event only in the end
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, container));
}
Aggregations