Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/AlexIIL/LibNetworkStack
https://github.com/AlexIIL/LibNetworkStack
fabricmc library minecraft minecraft-fabric minecraft-mod mod
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/AlexIIL/LibNetworkStack
- Owner: AlexIIL
- License: mpl-2.0
- Created: 2019-06-01T17:57:18.000Z (over 5 years ago)
- Default Branch: 0.10.x-1.20.x
- Last Pushed: 2024-06-01T05:45:51.000Z (7 months ago)
- Last Synced: 2024-10-15T06:22:33.117Z (3 months ago)
- Topics: fabricmc, library, minecraft, minecraft-fabric, minecraft-mod, mod
- Language: Java
- Size: 594 KB
- Stars: 11
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-fabric - LibNetworkStack - Adds a networking layer for mods to communicate data more easily. ([Wiki](https://github.com/AlexIIL/LibNetworkStack/wiki)) `MPL-2.0` (Uncategorized / Uncategorized)
README
# LibNetworkStack
This is a library mod for the [Fabric](https://fabricmc.net/) API, based around [Minecraft](https://minecraft.net).
## Maven
Currently you can use this by adding this to your build.gradle:
```
repositories {
maven {
name = "AlexIIL"
url = "https://maven.alexiil.uk/"
}
}dependencies {
modImplementation "alexiil.mc.lib:libnetworkstack-base:0.7.1"
}
```Please use the javadoc list [here](https://alexiil.uk/javadoc) to determine the right LNS version for your minecraft version.
## Getting Started
You can either look at the [wiki](https://github.com/AlexIIL/LibNetworkStack/wiki) for a brief overview, or look at [SimplePipes](https://github.com/AlexIIL/SimplePipes) source code for a use-case mod that uses this. To get help you can either open an issue here, or ping AlexIIL on the fabric or CottonMC discord servers.
## A simple example
For example if you wanted to send data from your custom BlockEntity called "ElectricFurnaceBlockEntity", you would do this:
```java
import alexiil.mc.lib.net.ActiveConnection;
import alexiil.mc.lib.net.IMsgReadCtx;
import alexiil.mc.lib.net.IMsgWriteCtx;
import alexiil.mc.lib.net.InvalidInputDataException;
import alexiil.mc.lib.net.NetByteBuf;
import alexiil.mc.lib.net.NetIdDataK;
import alexiil.mc.lib.net.NetIdDataK.IMsgDataWriterK;
import alexiil.mc.lib.net.NetIdSignalK;
import alexiil.mc.lib.net.ParentNetIdSingle;
import alexiil.mc.lib.net.impl.CoreMinecraftNetUtil;
import alexiil.mc.lib.net.impl.McNetworkStack;public class ElectricFurnaceBlockEntity extends BlockEntity {
// All base code left out
public static final ParentNetIdSingle NET_PARENT;
public static final NetIdDataK ID_CHANGE_BRIGHTNESS;
public static final NetIdSignalK ID_TURN_ON, ID_TURN_OFF;static {
NET_PARENT = McNetworkStack.BLOCK_ENTITY.subType(
// Assuming the modid is "electrical_nightmare"
ElectricFurnaceBlockEntity.class, "electrical_nightmare:electric_furnace"
);
// We don't need to re-specify the modid for this because the parent already did
// and we don't expect any other mods to add new id's.
ID_CHANGE_BRIGHTNESS = NET_PARENT.idData("CHANGE_BRIGHTNESS").setReceiver(
ElectricFurnaceBlockEntity::receiveBrightnessChange
);
ID_TURN_ON = NET_PARENT.idSignal("TURN_ON").setReceiver(ElectricFurnaceBlockEntity::receiveTurnOn);
ID_TURN_OFF = NET_PARENT.idSignal("TURN_OFF").setReceiver(ElectricFurnaceBlockEntity::receiveTurnOff);
}/** Used for rendering - somehow. */
public int clientBrightness;
public boolean clientIsOn;public ElectricFurnaceBlockEntity() {
super(null);
}/** Sends the new brightness, to be rendered on the front of the block.
* This should be called on the server side.
*
* @param newBrightness A value between 0 and 255. */
protected final void sendBrightnessChange(int newBrightness) {
for (ActiveConnection connection : CoreMinecraftNetUtil.getPlayersWatching(getWorld(), getPos())) {
ID_CHANGE_BRIGHTNESS.send(connection, this, new IMsgDataWriterK() {// In your own code you probably want to change this to a lambda
// but the full types are used here for clarity.@Override
public void write(ElectricFurnaceBlockEntity be, NetByteBuf buf, IMsgWriteCtx ctx) {
ctx.assertServerSide();buf.writeByte(newBrightness);
}
});
}
}/** Sends the new on/off state, to be rendered on the front of the block.
* This should be called on the server side. */
protected final void sendSwitchState(boolean isOn) {
for (ActiveConnection connection : CoreMinecraftNetUtil.getPlayersWatching(getWorld(), getPos())) {
(isOn ? ID_TURN_ON : ID_TURN_OFF).send(connection, this);
}
}protected void receiveBrightnessChange(NetByteBuf buf, IMsgReadCtx ctx) throws InvalidInputDataException {
// Ensure that this was sent by the server, to the client (and that we are on the client side)
ctx.assertClientSide();this.clientBrightness = buf.readUnsignedByte();
}protected void receiveTurnOn(IMsgReadCtx ctx) throws InvalidInputDataException {
// Ensure that this was sent by the server, to the client (and that we are on the client side)
ctx.assertClientSide();this.clientIsOn = true;
}protected void receiveTurnOff(IMsgReadCtx ctx) throws InvalidInputDataException {
// Ensure that this was sent by the server, to the client (and that we are on the client side)
ctx.assertClientSide();this.clientIsOn = false;
}
}```