Skip to content

Commit

Permalink
fixed some networking problems
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Feb 29, 2024
1 parent 78e66b0 commit f4f6632
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ dependencies {
// exclude group: "net.minecraftforge", module: "forge"
//}

implementation "mcjty.theoneprobe:theoneprobe:${top_version}"
compileOnly "mcjty.theoneprobe:theoneprobe:${top_version}"

implementation "curse.maven:jade-324717:${jade_curse_id}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public BulkItemFilterMenu(int windowId, Inventory invPlayer, MFLocator loc) {
}

public void clearSlots() {
handler.setAutoSave(false);
for (int i = 0; i < handler.getSlots(); i++) {
handler.setStackInSlot(i, ItemStack.EMPTY);
}
handler.setAutoSave(true);
handler.save();

if (getRouter() != null && !getRouter().getLevel().isClientSide) {
Expand All @@ -86,12 +88,15 @@ public int mergeInventory(IItemHandler srcInv, Filter.Flags flags, boolean clear
}

int slot = 0;
handler.setAutoSave(false);
for (ItemStack stack : stacks.sortedList()) {
handler.setStackInSlot(slot++, stack);
}
while (slot < handler.getSlots()) {
handler.setStackInSlot(slot++, ItemStack.EMPTY);
}

handler.setAutoSave(true);
handler.save();

if (getRouter() != null && !getRouter().getLevel().isClientSide) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class BaseModuleHandler extends GhostItemHandler {
private final ItemStack holderStack;
protected final ModularRouterBlockEntity router;
private final String tagName;
private boolean autoSave = true;

public BaseModuleHandler(ItemStack holderStack, ModularRouterBlockEntity router, int size, String tagName) {
super(size);
Expand All @@ -26,6 +27,10 @@ public BaseModuleHandler(ItemStack holderStack, ModularRouterBlockEntity router,
deserializeNBT(ModuleHelper.validateNBT(holderStack).getCompound(tagName));
}

public void setAutoSave(boolean autoSave) {
this.autoSave = autoSave;
}

/**
* Get the itemstack which holds this filter. Could be a module, could be a bulk item filter...
*
Expand All @@ -37,10 +42,12 @@ public ItemStack getHolderStack() {

@Override
protected void onContentsChanged(int slot) {
save();
if (autoSave) {
save();

if (router != null) {
router.recompileNeeded(ModularRouterBlockEntity.COMPILE_MODULES);
if (router != null) {
router.recompileNeeded(ModularRouterBlockEntity.COMPILE_MODULES);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,22 @@
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;

public enum ClientPayloadHandler {
INSTANCE;

public static ClientPayloadHandler getInstance() {
return INSTANCE;
}

public void handleData(GuiSyncMessage message, PlayPayloadContext context) {
public class ClientPayloadHandler {
public static void handleData(GuiSyncMessage message, PlayPayloadContext context) {
context.workHandler().submitAsync(() -> {
if (Minecraft.getInstance().screen instanceof IResyncableGui syncable) {
syncable.resync(message.newStack());
}
});
}

public void handleData(ItemBeamMessage message, PlayPayloadContext context) {
public static void handleData(ItemBeamMessage message, PlayPayloadContext context) {
context.workHandler().submitAsync(() ->
Minecraft.getInstance().level.getBlockEntity(message.pos(), ModBlockEntities.MODULAR_ROUTER.get())
.ifPresent(te -> message.beams().forEach(te::addItemBeam)));
}

public void handleData(PushEntityMessage message, PlayPayloadContext context) {
public static void handleData(PushEntityMessage message, PlayPayloadContext context) {
context.workHandler().submitAsync(() -> {
Entity entity = Minecraft.getInstance().level.getEntity(message.entityId());
if (entity != null) {
Expand All @@ -47,7 +41,7 @@ public void handleData(PushEntityMessage message, PlayPayloadContext context) {
});
}

public void handleData(RouterUpgradesSyncMessage message, PlayPayloadContext context) {
public static void handleData(RouterUpgradesSyncMessage message, PlayPayloadContext context) {
context.workHandler().submitAsync(() -> {
Level level = Minecraft.getInstance().level;
if (level != null && level.isLoaded(message.pos())) {
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/me/desht/modularrouters/network/NetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,35 @@
public class NetworkHandler {
private static final String NETWORK_VERSION = "1.0";

@SuppressWarnings("Convert2MethodRef")
@SubscribeEvent
public static void register(final RegisterPayloadHandlerEvent event) {
final IPayloadRegistrar registrar = event.registrar(ModularRouters.MODID)
.versioned(NETWORK_VERSION);

// clientbound
registrar.play(GuiSyncMessage.ID, GuiSyncMessage::new, handler -> handler
.client(ClientPayloadHandler.getInstance()::handleData));
.client((message, context) -> ClientPayloadHandler.handleData(message, context)));
registrar.play(ItemBeamMessage.ID, ItemBeamMessage::fromNetwork, handler -> handler
.client(ClientPayloadHandler.getInstance()::handleData));
.client((message, context) -> ClientPayloadHandler.handleData(message, context)));
registrar.play(PushEntityMessage.ID, PushEntityMessage::new, handler -> handler
.client(ClientPayloadHandler.getInstance()::handleData));
.client((message, context) -> ClientPayloadHandler.handleData(message, context)));
registrar.play(RouterUpgradesSyncMessage.ID, RouterUpgradesSyncMessage::new, handler -> handler
.client(ClientPayloadHandler.getInstance()::handleData));
.client((message, context) -> ClientPayloadHandler.handleData(message, context)));

// serverbound
registrar.play(FilterSettingsMessage.ID, FilterSettingsMessage::new, handler -> handler
.server(ServerPayloadHandler.getInstance()::handleData));
.server((message, context) -> ServerPayloadHandler.handleData(message, context)));
registrar.play(ModuleFilterMessage.ID, ModuleFilterMessage::new, handler -> handler
.server(ServerPayloadHandler.getInstance()::handleData));
.server((message, context) -> ServerPayloadHandler.handleData(message, context)));
registrar.play(ModuleSettingsMessage.ID, ModuleSettingsMessage::new, handler -> handler
.server(ServerPayloadHandler.getInstance()::handleData));
.server((message, context) -> ServerPayloadHandler.handleData(message, context)));
registrar.play(OpenGuiMessage.ID, OpenGuiMessage::new, handler -> handler
.server(ServerPayloadHandler.getInstance()::handleData));
.server((message, context) -> ServerPayloadHandler.handleData(message, context)));
registrar.play(SyncUpgradeSettingsMessage.ID, SyncUpgradeSettingsMessage::new, handler -> handler
.server(ServerPayloadHandler.getInstance()::handleData));
.server((message, context) -> ServerPayloadHandler.handleData(message, context)));
registrar.play(ValidateModuleMessage.ID, ValidateModuleMessage::new, handler -> handler
.server(ServerPayloadHandler.getInstance()::handleData));
.server((message, context) -> ServerPayloadHandler.handleData(message, context)));

// bidirectional
registrar.play(RouterSettingsMessage.ID, RouterSettingsMessage::new, handler -> handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@

import java.util.Objects;

public enum ServerPayloadHandler {
INSTANCE;

public static ServerPayloadHandler getInstance() {
return INSTANCE;
}

public void handleData(FilterSettingsMessage message, PlayPayloadContext context) {
public class ServerPayloadHandler {
public static void handleData(FilterSettingsMessage message, PlayPayloadContext context) {
context.player().ifPresent(player -> context.workHandler().submitAsync(() -> {
MFLocator locator = message.locator();
ItemStack moduleStack = locator.getModuleStack(player);
Expand Down Expand Up @@ -60,7 +54,7 @@ public void handleData(FilterSettingsMessage message, PlayPayloadContext context
}));
}

public void handleData(ModuleFilterMessage message, PlayPayloadContext context) {
public static void handleData(ModuleFilterMessage message, PlayPayloadContext context) {
context.player().ifPresent(player -> context.workHandler().submitAsync(() -> {
AbstractContainerMenu c = player.containerMenu;
int slot = message.slot();
Expand All @@ -74,7 +68,7 @@ private static boolean isValidContainer(AbstractContainerMenu c) {
return c instanceof ModuleMenu || c instanceof BulkItemFilterMenu;
}

public void handleData(ModuleSettingsMessage message, PlayPayloadContext context) {
public static void handleData(ModuleSettingsMessage message, PlayPayloadContext context) {
context.player().ifPresent(player -> context.workHandler().submitAsync(() -> {
MFLocator locator = message.locator();
CompoundTag payload = message.payload();
Expand All @@ -95,7 +89,7 @@ public void handleData(ModuleSettingsMessage message, PlayPayloadContext context
}));
}

public void handleData(OpenGuiMessage message, PlayPayloadContext context) {
public static void handleData(OpenGuiMessage message, PlayPayloadContext context) {
context.player().ifPresent(player -> context.workHandler().submitAsync(() -> {
MFLocator locator = message.locator();
switch (message.op()) {
Expand All @@ -121,7 +115,7 @@ public void handleData(OpenGuiMessage message, PlayPayloadContext context) {
}));
}

public void handleData(SyncUpgradeSettingsMessage message, PlayPayloadContext context) {
public static void handleData(SyncUpgradeSettingsMessage message, PlayPayloadContext context) {
context.player().ifPresent(player -> context.workHandler().submitAsync(() -> {
ItemStack held = player.getItemInHand(message.hand());
if (held.getItem() instanceof SyncUpgrade) {
Expand All @@ -130,7 +124,7 @@ public void handleData(SyncUpgradeSettingsMessage message, PlayPayloadContext co
}));
}

public void handleData(ValidateModuleMessage message, PlayPayloadContext context) {
public static void handleData(ValidateModuleMessage message, PlayPayloadContext context) {
context.player().ifPresent(player -> context.workHandler().submitAsync(() -> {
ItemStack stack = player.getItemInHand(message.hand());
if (stack.getItem() instanceof ModuleItem moduleItem && player instanceof ServerPlayer sp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public RouterSettingsMessage(FriendlyByteBuf buffer) {

@Override
public void write(FriendlyByteBuf buffer) {
buffer.writeBlockPos(pos);
buffer.writeByte(redstoneBehaviour.ordinal());
buffer.writeBoolean(ecoMode);
buffer.writeByte(redstoneBehaviour.ordinal());
buffer.writeEnum(energyDirection);
buffer.writeBlockPos(pos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static SetofItemStack fromItemHandler(IItemHandler handler, Flags filterF
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
if (!stack.isEmpty()) {
itemStacks.add(stack);
itemStacks.add(stack.copy());
}
}
return new SetofItemStack(itemStacks, filterFlags);
Expand Down

0 comments on commit f4f6632

Please sign in to comment.