Skip to content

Commit

Permalink
Fix Change Type, Fix Edge Case CME
Browse files Browse the repository at this point in the history
CME fix adapted from AE2-UEL/BetterP2P#4
  • Loading branch information
IntegerLimit committed Dec 28, 2024
1 parent 5212756 commit e95bd1d
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ private void properlyChangeAllP2PTypes(P2PLocation p2p, TunnelInfo newType, Call
return;
}

changeP2PType(tunnel, newType);

if (tunnel.getFrequency() == 0 || !tunnel.isActive()) {
if (tunnel.getFrequency() == 0) {
// Unbound or Inactive
// Can't rebind same frequency if inactive, as not registered in ae2 handling
cir.setReturnValue(true);
var result = changeP2PType(tunnel, newType);
cir.setReturnValue(result != null);
return;
}

Expand All @@ -94,12 +93,17 @@ private void properlyChangeAllP2PTypes(P2PLocation p2p, TunnelInfo newType, Call
TunnelCollection<?> outputs = tunnel.getProxy().getP2P().getOutputs(tunnel.getFrequency(),
tunnel.getClass());

for (var input : inputs) {
changeP2PType(input, newType);
}

for (var output : outputs) {
changeP2PType(output, newType);
// Add all parts to a new list, so we don't CME
List<PartP2PTunnel<?>> toModify = new ArrayList<>();
inputs.forEach(toModify::add);
outputs.forEach(toModify::add);

for (var modify : toModify) {
var result = changeP2PType(modify, newType);
if (result == null) {
cir.setReturnValue(false);
return;
}
}
} catch (GridAccessException ignored) {}
cir.setReturnValue(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.nomiceu.nomilabs.mixin.betterp2p;

import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

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;

import com.projecturanus.betterp2p.network.ModNetwork;
import com.projecturanus.betterp2p.network.PlayerRequest;
import com.projecturanus.betterp2p.network.packet.C2SLinkP2P;
import com.projecturanus.betterp2p.network.packet.ServerLinkP2PHandler;

/**
* Fixes an edge case CME.
*/
@Mixin(value = ServerLinkP2PHandler.class, remap = false)
public class ServerLinkP2PHandlerMixin {

@Inject(method = "onMessage(Lcom/projecturanus/betterp2p/network/packet/C2SLinkP2P;Lnet/minecraftforge/fml/common/network/simpleimpl/MessageContext;)Lnet/minecraftforge/fml/common/network/simpleimpl/IMessage;",
at = @At("HEAD"),
cancellable = true)
private void newMsgHandling(C2SLinkP2P message, MessageContext ctx, CallbackInfoReturnable<IMessage> cir) {
cir.setReturnValue(null);

if (!ctx.side.isServer()) return;
if (message.getInput() == null || message.getOutput() == null) return;

PlayerRequest state = ModNetwork.INSTANCE.getPlayerState().get(ctx.getServerHandler().player.getUniqueID());
if (state == null) return;

ctx.getServerHandler().server.addScheduledTask(() -> {
var result = state.getGridCache().linkP2P(message.getInput(), message.getOutput());

if (result != null) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.nomiceu.nomilabs.mixin.betterp2p;

import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

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;

import com.projecturanus.betterp2p.BetterP2P;
import com.projecturanus.betterp2p.network.ModNetwork;
import com.projecturanus.betterp2p.network.PlayerRequest;
import com.projecturanus.betterp2p.network.packet.*;
import com.projecturanus.betterp2p.util.p2p.TunnelInfo;

/**
* Fixes an edge case CME.
*/
@Mixin(value = ServerTypeChangeHandler.class, remap = false)
public class ServerTypeChangeHandlerMixin {

@Inject(method = "onMessage(Lcom/projecturanus/betterp2p/network/packet/C2STypeChange;Lnet/minecraftforge/fml/common/network/simpleimpl/MessageContext;)Lcom/projecturanus/betterp2p/network/packet/S2COpenGui;",
at = @At("HEAD"),
cancellable = true)
private void newMsgHandling(C2STypeChange message, MessageContext ctx, CallbackInfoReturnable<S2COpenGui> cir) {
cir.setReturnValue(null);

if (!ctx.side.isServer()) return;
if (message.getP2p() == null) return;

PlayerRequest state = ModNetwork.INSTANCE.getPlayerState().get(ctx.getServerHandler().player.getUniqueID());
TunnelInfo type = BetterP2P.proxy.getP2PFromIndex(message.getNewType());
if (state == null || type == null) return;

ctx.getServerHandler().server.addScheduledTask(() -> {
var result = state.getGridCache().changeAllP2Ps(message.getP2p(), type);

if (result) {
ModNetwork.INSTANCE.requestP2PList(ctx.getServerHandler().player, type.getIndex());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nomiceu.nomilabs.mixin.betterp2p;

import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

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;

import com.projecturanus.betterp2p.network.ModNetwork;
import com.projecturanus.betterp2p.network.PlayerRequest;
import com.projecturanus.betterp2p.network.packet.*;

/**
* Fixes an edge case CME.
*/
@Mixin(value = ServerUnlinkP2PHandler.class, remap = false)
public class ServerUnlinkP2PHandlerMixin {

@Inject(method = "onMessage(Lcom/projecturanus/betterp2p/network/packet/C2SUnlinkP2P;Lnet/minecraftforge/fml/common/network/simpleimpl/MessageContext;)Lcom/projecturanus/betterp2p/network/packet/S2COpenGui;",
at = @At("HEAD"),
cancellable = true)
private void newMsgHandling(C2SUnlinkP2P message, MessageContext ctx, CallbackInfoReturnable<S2COpenGui> cir) {
cir.setReturnValue(null);

if (!ctx.side.isServer()) return;
if (message.getP2p() == null) return;

PlayerRequest state = ModNetwork.INSTANCE.getPlayerState().get(ctx.getServerHandler().player.getUniqueID());
if (state == null) return;

ctx.getServerHandler().server.addScheduledTask(() -> {
state.getGridCache().unlinkP2P(message.getP2p());

ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public IMessage onMessage(LabsP2PAddAsInputMessage message, MessageContext ctx)
PlayerRequest state = ModNetwork.INSTANCE.getPlayerState().get(ctx.getServerHandler().player.getUniqueID());
if (state == null) return null;

boolean result = ((AccessibleGridServerCache) (Object) state.getGridCache())
.labs$addInput(message.location, message.sourceFrequency);

if (result) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
ctx.getServerHandler().server.addScheduledTask(() -> {
boolean result = ((AccessibleGridServerCache) (Object) state.getGridCache())
.labs$addInput(message.location, message.sourceFrequency);

if (result) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
});
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public IMessage onMessage(LabsP2PAddAsOutputMessage message, MessageContext ctx)
PlayerRequest state = ModNetwork.INSTANCE.getPlayerState().get(ctx.getServerHandler().player.getUniqueID());
if (state == null) return null;

boolean result = ((AccessibleGridServerCache) (Object) state.getGridCache())
.labs$addOutput(message.location, message.sourceFrequency);

if (result) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
ctx.getServerHandler().server.addScheduledTask(() -> {
boolean result = ((AccessibleGridServerCache) (Object) state.getGridCache())
.labs$addOutput(message.location, message.sourceFrequency);

if (result) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
});
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ public IMessage onMessage(LabsP2PChangeTypeMessage message, MessageContext ctx)
PlayerRequest state = ModNetwork.INSTANCE.getPlayerState().get(ctx.getServerHandler().player.getUniqueID());
if (state == null) return null;

PartP2PTunnel<?> result = ((AccessibleGridServerCache) (Object) state.getGridCache())
.labs$changeIsInput(message.location, message.isInput);

if (result != null) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
ctx.getServerHandler().server.addScheduledTask(() -> {
PartP2PTunnel<?> result = ((AccessibleGridServerCache) (Object) state.getGridCache())
.labs$changeIsInput(message.location, message.isInput);
if (result != null) {
ModNetwork.INSTANCE.requestP2PUpdate(ctx.getServerHandler().player);
}
});
return null;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/mixins.nomilabs.betterp2p.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"BetterMemoryCardModesMixin",
"CommonProxyMixin",
"GridServerCacheMixin",
"ServerRenameP2PTunnelMixin"
"ServerLinkP2PHandlerMixin",
"ServerRenameP2PTunnelMixin",
"ServerTypeChangeHandlerMixin",
"ServerUnlinkP2PHandlerMixin"
],
"client": [
"ClientProxyMixin",
Expand Down

0 comments on commit e95bd1d

Please sign in to comment.