Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite netcode #320

Merged
merged 4 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ dependencies {
"compile"(files("etc/CTM-MC1.12.2-1.0.2.31.jar"))

"testImplementation"("junit:junit:4.13.1")
"implementation"("org.projectlombok:lombok:1.18.16")
}

configure<JavaPluginConvention> {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gregtech/api/block/BlockCustomParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import codechicken.lib.vec.Vector3;
import gregtech.api.net.NetworkHandler;
import gregtech.api.net.PacketBlockParticle;
import gregtech.api.net.packets.PacketBlockParticle;
import gregtech.api.net.NetworkUtils;
import gregtech.api.util.ParticleHandlerUtil;
import net.minecraft.block.Block;
import net.minecraft.block.material.MapColor;
Expand Down Expand Up @@ -71,7 +72,7 @@ public boolean addRunningEffects(@Nonnull IBlockState state, World world, @Nonnu
@Override
public boolean addLandingEffects(@Nonnull IBlockState state, @Nonnull WorldServer worldObj, @Nonnull BlockPos blockPosition, @Nonnull IBlockState iblockstate, EntityLivingBase entity, int numberOfParticles) {
PacketBlockParticle packet = new PacketBlockParticle(blockPosition, new Vector3(entity.posX, entity.posY, entity.posZ), numberOfParticles);
NetworkHandler.channel.sendToAllTracking(packet.toFMLPacket(), NetworkHandler.blockPoint(worldObj, blockPosition));
NetworkHandler.channel.sendToAllTracking(packet.toFMLPacket(), NetworkUtils.blockPoint(worldObj, blockPosition));
return true;
}
}
6 changes: 3 additions & 3 deletions src/main/java/gregtech/api/gui/UIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import gregtech.api.gui.impl.ModularUIContainer;
import gregtech.api.gui.impl.ModularUIGui;
import gregtech.api.net.NetworkHandler;
import gregtech.api.net.PacketUIOpen;
import gregtech.api.net.PacketUIWidgetUpdate;
import gregtech.api.net.packets.PacketUIOpen;
import gregtech.api.net.packets.PacketUIWidgetUpdate;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
Expand Down Expand Up @@ -54,7 +54,7 @@ public final void openUI(E holder, EntityPlayerMP player) {
container.accumulatedUpdates.clear();

PacketUIOpen packet = new PacketUIOpen(uiFactoryId, serializedHolder, currentWindowId, updateData);
NetworkHandler.channel.sendTo(NetworkHandler.packet2proxy(packet), player);
NetworkHandler.channel.sendTo(packet.toFMLPacket(), player);

container.addListener(player);
player.openContainer = container;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/gregtech/api/gui/impl/ModularUIContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import gregtech.api.gui.Widget;
import gregtech.api.gui.widgets.WidgetUIAccess;
import gregtech.api.net.NetworkHandler;
import gregtech.api.net.PacketUIClientAction;
import gregtech.api.net.PacketUIWidgetUpdate;
import gregtech.api.net.packets.PacketUIClientAction;
import gregtech.api.net.packets.PacketUIWidgetUpdate;
import gregtech.api.util.GTUtility;
import gregtech.api.util.PerTickIntCounter;
import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -236,8 +236,7 @@ public void writeClientAction(Widget widget, int updateId, Consumer<PacketBuffer
packetBuffer.writeVarInt(updateId);
payloadWriter.accept(packetBuffer);
if (modularUI.entityPlayer instanceof EntityPlayerSP) {
PacketUIClientAction widgetUpdate = new PacketUIClientAction(windowId, widgetId, packetBuffer);
NetworkHandler.channel.sendToServer(NetworkHandler.packet2proxy(widgetUpdate));
NetworkHandler.channel.sendToServer(new PacketUIClientAction(windowId, widgetId, packetBuffer).toFMLPacket());
}
}

Expand All @@ -250,7 +249,7 @@ public void writeUpdateInfo(Widget widget, int updateId, Consumer<PacketBuffer>
if (modularUI.entityPlayer instanceof EntityPlayerMP) {
PacketUIWidgetUpdate widgetUpdate = new PacketUIWidgetUpdate(windowId, widgetId, packetBuffer);
if (!accumulateWidgetUpdateData) {
NetworkHandler.channel.sendTo(NetworkHandler.packet2proxy(widgetUpdate), (EntityPlayerMP) modularUI.entityPlayer);
NetworkHandler.channel.sendTo(widgetUpdate.toFMLPacket(), (EntityPlayerMP) modularUI.entityPlayer);
} else {
accumulatedUpdates.add(widgetUpdate);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/gui/impl/ModularUIGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.Widget;
import gregtech.api.gui.widgets.SlotWidget;
import gregtech.api.net.PacketUIWidgetUpdate;
import gregtech.api.net.packets.PacketUIWidgetUpdate;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import gregtech.api.items.gui.PlayerInventoryHolder;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.metaitem.stats.IItemBehaviour;
import gregtech.api.net.CPacketPluginSynced;
import gregtech.api.net.packets.PacketPluginSynced;
import gregtech.api.net.NetworkHandler;
import gregtech.api.util.IDirtyNotifiable;
import gregtech.common.gui.widget.monitor.WidgetPluginConfig;
import gregtech.common.metatileentities.multi.electric.centralmonitor.MetaTileEntityMonitorScreen;
import io.netty.buffer.Unpooled;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down Expand Up @@ -115,8 +116,13 @@ public void readPluginData(int id, PacketBuffer buf) {
* @param id PacketID
* @param buf PacketBuffer
*/
public final void writePluginAction(int id, @Nonnull Consumer<PacketBuffer> buf) {
NetworkHandler.channel.sendToServer(new CPacketPluginSynced(this, id, buf).toFMLPacket());
public final void writePluginAction(int id, @Nonnull Consumer<PacketBuffer> dataWriter) {
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
dataWriter.accept(buffer);
NetworkHandler.channel.sendToServer(new PacketPluginSynced(
this.getScreen().getWorld().provider.getDimension(),
this.getScreen().getPos(),
id, buffer).toFMLPacket());
}

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.gui.IUIHolder;
import gregtech.api.net.CPacketRecoverMTE;
import gregtech.api.net.packets.PacketRecoverMTE;
import gregtech.api.net.NetworkHandler;
import gregtech.api.util.GTLog;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -126,7 +126,7 @@ public void update() {
if (metaTileEntity != null) {
metaTileEntity.update();
} else if (world.isRemote) { // recover the mte
NetworkHandler.channel.sendToServer(new CPacketRecoverMTE(world, getPos()).toFMLPacket());
NetworkHandler.channel.sendToServer(new PacketRecoverMTE(world.provider.getDimension(), getPos()).toFMLPacket());
}

if (this.needToUpdateLightning) {
Expand Down
65 changes: 0 additions & 65 deletions src/main/java/gregtech/api/net/CPacketPluginSynced.java

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/java/gregtech/api/net/CPacketRecoverMTE.java

This file was deleted.

77 changes: 77 additions & 0 deletions src/main/java/gregtech/api/net/IPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package gregtech.api.net;

import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

/**
* The general structure of Network Packets. <br><br>
* <p>
* To implement a new packet, implement both {@link IPacket#encode(PacketBuffer)} and
* {@link IPacket#decode(PacketBuffer)}, and register the packet in {@link NetworkHandler#init()} method.<br><br>
* <p>
* Additionally, do one of the following:<p>
* - If this Packet is to be received on the SERVER, implement {@link IPacket#executeServer(NetHandlerPlayServer)}
* and register in {@link NetworkHandler#initServer()}.
* <p>
* - If this Packet is to be received on the CLIENT, implement {@link IPacket#executeClient(NetHandlerPlayClient)}
* and register in {@link NetworkHandler#initClient()}.<br><br>
* <p>
* Lastly, add the {@link lombok.NoArgsConstructor} annotation to your Packet class.
*/
public interface IPacket {

/**
* Used to write data from a Packet into a PacketBuffer.<br><br>
* <p>
* This is the first step in sending a Packet to a different thread,
* and is done on the "sending" side.
*
* @param buf The PacketBuffer to write Packet data to.
*/
void encode(PacketBuffer buf);

/**
* Used to read data from a PacketBuffer into this Packet.<br><br>
* <p>
* This is the next step of sending a Packet to a different thread,
* and is done on the "receiving" side.
*
* @param buf The PacketBuffer to read Packet data from.
*/
void decode(PacketBuffer buf);

/**
* Used to execute code on the client, after receiving a packet from the server.<br><br>
* <p>
* CANNOT be implemented with {@link IPacket#executeServer(NetHandlerPlayServer)}, only one at a time is supported.
*
* @param handler Network handler that contains useful data and helpers.
*/
@SideOnly(Side.CLIENT)
default void executeClient(NetHandlerPlayClient handler) {
}

/**
* Used to execute code on the server, after receiving a packet from the client.<br><br>
* <p>
* CANNOT be implemented with {@link IPacket#executeClient(NetHandlerPlayClient)}, only one at a time is supported.
*
* @param handler Network handler that contains useful data and helpers.
*/
default void executeServer(NetHandlerPlayServer handler) {
}

/**
* Convenience method that redirects to {@link NetworkUtils#packet2proxy(IPacket)}. Converts an instance of this
* class to a Packet that Forge can understand.<br><br>
*
* @return An FMLProxyPacket representation of this Packet.
*/
default FMLProxyPacket toFMLPacket() {
return NetworkUtils.packet2proxy(this);
}
}
45 changes: 0 additions & 45 deletions src/main/java/gregtech/api/net/KeysPacket.java

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/java/gregtech/api/net/KeysUpdateHandler.java

This file was deleted.

Loading