Fixed multiplayer: cache in Mode/ModifierSettingsManager does not work on server, so it's removed. Also changed some packets to use separate classes for clientside handling, as recommended by the Forge Community Wiki.
This commit is contained in:
@@ -12,7 +12,7 @@ apply plugin: 'net.minecraftforge.gradle'
|
||||
apply plugin: 'eclipse'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
version = '1.18.1-2.30'
|
||||
version = '1.18.1-2.31'
|
||||
group = 'nl.requios.effortlessbuilding' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||
archivesBaseName = 'effortlessbuilding'
|
||||
|
||||
|
||||
@@ -16,33 +16,24 @@ import javax.annotation.Nonnull;
|
||||
@Mod.EventBusSubscriber
|
||||
public class ModeSettingsManager {
|
||||
|
||||
private static ModeSettings cache = null;
|
||||
|
||||
//Retrieves the buildsettings of a player through the modeCapability capability
|
||||
//Never returns null
|
||||
@Nonnull
|
||||
public static ModeSettings getModeSettings(Player player) {
|
||||
if (cache != null) return cache;
|
||||
|
||||
LazyOptional<ModeCapabilityManager.IModeCapability> modeCapability =
|
||||
player.getCapability(ModeCapabilityManager.MODE_CAPABILITY, null);
|
||||
|
||||
if (modeCapability.isPresent()) {
|
||||
ModeCapabilityManager.IModeCapability capability = modeCapability.orElse(null);
|
||||
cache = capability.getModeData();
|
||||
if (cache == null) {
|
||||
cache = new ModeSettings();
|
||||
capability.setModeData(cache);
|
||||
if (capability.getModeData() == null){
|
||||
capability.setModeData(new ModeSettings());
|
||||
}
|
||||
//Add invalidation listener, to invalidate cache
|
||||
modeCapability.addListener(self -> cache = null);
|
||||
return capability.getModeData();
|
||||
}
|
||||
|
||||
EffortlessBuilding.logger.warn("Player does not have modeCapability: " + player);
|
||||
//Return dummy settings
|
||||
return new ModeSettings();
|
||||
// throw new IllegalArgumentException("Player does not have modeCapability capability");
|
||||
}
|
||||
|
||||
public static void setModeSettings(Player player, ModeSettings modeSettings) {
|
||||
|
||||
@@ -17,27 +17,20 @@ import javax.annotation.Nonnull;
|
||||
@Mod.EventBusSubscriber
|
||||
public class ModifierSettingsManager {
|
||||
|
||||
private static ModifierSettings cache = null;
|
||||
|
||||
//Retrieves the buildsettings of a player through the modifierCapability capability
|
||||
//Never returns null
|
||||
@Nonnull
|
||||
public static ModifierSettings getModifierSettings(Player player) {
|
||||
if (cache != null) return cache;
|
||||
|
||||
LazyOptional<ModifierCapabilityManager.IModifierCapability> modifierCapability =
|
||||
player.getCapability(ModifierCapabilityManager.MODIFIER_CAPABILITY, null);
|
||||
|
||||
if (modifierCapability.isPresent()) {
|
||||
ModifierCapabilityManager.IModifierCapability capability = modifierCapability.orElse(null);
|
||||
cache = capability.getModifierData();
|
||||
if (cache == null) {
|
||||
cache = new ModifierSettings();
|
||||
capability.setModifierData(cache);
|
||||
if (capability.getModifierData() == null){
|
||||
capability.setModifierData(new ModifierSettings());
|
||||
}
|
||||
//Add invalidation listener, to invalidate cache
|
||||
modifierCapability.addListener(self -> cache = null);
|
||||
return cache;
|
||||
return capability.getModifierData();
|
||||
}
|
||||
|
||||
EffortlessBuilding.logger.warn("Player does not have modifierCapability: " + player);
|
||||
|
||||
@@ -6,11 +6,15 @@ import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||
import nl.requios.effortlessbuilding.buildmodifier.BlockSet;
|
||||
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
|
||||
import nl.requios.effortlessbuilding.render.BlockPreviewRenderer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Supplier;
|
||||
@@ -67,25 +71,31 @@ public class AddUndoMessage {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||
//Received clientside
|
||||
|
||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
||||
//Add to undo stack clientside
|
||||
//Only the appropriate player that needs to add this to the undo stack gets this message
|
||||
UndoRedo.addUndo(player, new BlockSet(
|
||||
new ArrayList<BlockPos>() {{
|
||||
add(message.getCoordinate());
|
||||
}},
|
||||
new ArrayList<BlockState>() {{
|
||||
add(message.getPreviousBlockState());
|
||||
}},
|
||||
new ArrayList<BlockState>() {{
|
||||
add(message.getNewBlockState());
|
||||
}},
|
||||
new Vec3(0, 0, 0),
|
||||
message.getCoordinate(), message.getCoordinate()));
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||
}
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public static class ClientHandler {
|
||||
public static void handle(AddUndoMessage message, Supplier<NetworkEvent.Context> ctx) {
|
||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
||||
//Add to undo stack clientside
|
||||
//Only the appropriate player that needs to add this to the undo stack gets this message
|
||||
UndoRedo.addUndo(player, new BlockSet(
|
||||
new ArrayList<BlockPos>() {{
|
||||
add(message.getCoordinate());
|
||||
}},
|
||||
new ArrayList<BlockState>() {{
|
||||
add(message.getPreviousBlockState());
|
||||
}},
|
||||
new ArrayList<BlockState>() {{
|
||||
add(message.getNewBlockState());
|
||||
}},
|
||||
new Vec3(0, 0, 0),
|
||||
message.getCoordinate(), message.getCoordinate()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import nl.requios.effortlessbuilding.buildmode.BuildModes;
|
||||
@@ -95,8 +98,7 @@ public class BlockPlacedMessage {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||
//Received clientside
|
||||
//Nod RenderHandler to do the dissolve shader effect
|
||||
BlockPreviewRenderer.onBlocksPlaced();
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||
} else {
|
||||
//Received serverside
|
||||
BuildModes.onBlockPlacedMessage(ctx.get().getSender(), message);
|
||||
@@ -105,4 +107,12 @@ public class BlockPlacedMessage {
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public static class ClientHandler {
|
||||
public static void handle(BlockPlacedMessage message, Supplier<NetworkEvent.Context> ctx) {
|
||||
//Nod RenderHandler to do the dissolve shader effect
|
||||
BlockPreviewRenderer.onBlocksPlaced();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@ package nl.requios.effortlessbuilding.network;
|
||||
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
|
||||
import nl.requios.effortlessbuilding.render.BlockPreviewRenderer;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -30,13 +34,20 @@ public class ClearUndoMessage {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||
//Received clientside
|
||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
||||
|
||||
//Add to undo stack clientside
|
||||
UndoRedo.clear(player);
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||
}
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public static class ClientHandler {
|
||||
public static void handle(ClearUndoMessage message, Supplier<NetworkEvent.Context> ctx) {
|
||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
||||
|
||||
//Add to undo stack clientside
|
||||
UndoRedo.clear(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||
public class PacketHandler {
|
||||
private static final String PROTOCOL_VERSION = "1";
|
||||
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
|
||||
new ResourceLocation(EffortlessBuilding.MODID, "main_channel"),
|
||||
new ResourceLocation(EffortlessBuilding.MODID, "main"),
|
||||
() -> PROTOCOL_VERSION,
|
||||
PROTOCOL_VERSION::equals,
|
||||
PROTOCOL_VERSION::equals
|
||||
|
||||
@@ -4,10 +4,14 @@ import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||
import nl.requios.effortlessbuilding.proxy.ClientProxy;
|
||||
import nl.requios.effortlessbuilding.render.BlockPreviewRenderer;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -44,19 +48,26 @@ public class RequestLookAtMessage {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||
//Received clientside
|
||||
//Send back your info
|
||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
||||
|
||||
//Prevent double placing in normal mode with placeStartPos false
|
||||
//Unless QuickReplace is on, then we do need to place start pos.
|
||||
if (ClientProxy.previousLookAt.getType() == HitResult.Type.BLOCK) {
|
||||
PacketHandler.INSTANCE.sendToServer(new BlockPlacedMessage((BlockHitResult) ClientProxy.previousLookAt, message.getPlaceStartPos()));
|
||||
} else {
|
||||
PacketHandler.INSTANCE.sendToServer(new BlockPlacedMessage());
|
||||
}
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||
}
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public static class ClientHandler {
|
||||
public static void handle(RequestLookAtMessage message, Supplier<NetworkEvent.Context> ctx) {
|
||||
//Send back your info
|
||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
||||
|
||||
//Prevent double placing in normal mode with placeStartPos false
|
||||
//Unless QuickReplace is on, then we do need to place start pos.
|
||||
if (ClientProxy.previousLookAt.getType() == HitResult.Type.BLOCK) {
|
||||
PacketHandler.INSTANCE.sendToServer(new BlockPlacedMessage((BlockHitResult) ClientProxy.previousLookAt, message.getPlaceStartPos()));
|
||||
} else {
|
||||
PacketHandler.INSTANCE.sendToServer(new BlockPlacedMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Send packet to client to translate and log the containing message
|
||||
*/
|
||||
public class TranslatedLogMessage {
|
||||
private final String prefix;
|
||||
private final String translationKey;
|
||||
|
||||
Reference in New Issue
Block a user