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: 'eclipse'
|
||||||
apply plugin: 'maven-publish'
|
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
|
group = 'nl.requios.effortlessbuilding' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||||
archivesBaseName = 'effortlessbuilding'
|
archivesBaseName = 'effortlessbuilding'
|
||||||
|
|
||||||
|
|||||||
@@ -16,33 +16,24 @@ import javax.annotation.Nonnull;
|
|||||||
@Mod.EventBusSubscriber
|
@Mod.EventBusSubscriber
|
||||||
public class ModeSettingsManager {
|
public class ModeSettingsManager {
|
||||||
|
|
||||||
private static ModeSettings cache = null;
|
|
||||||
|
|
||||||
//Retrieves the buildsettings of a player through the modeCapability capability
|
//Retrieves the buildsettings of a player through the modeCapability capability
|
||||||
//Never returns null
|
//Never returns null
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static ModeSettings getModeSettings(Player player) {
|
public static ModeSettings getModeSettings(Player player) {
|
||||||
if (cache != null) return cache;
|
|
||||||
|
|
||||||
LazyOptional<ModeCapabilityManager.IModeCapability> modeCapability =
|
LazyOptional<ModeCapabilityManager.IModeCapability> modeCapability =
|
||||||
player.getCapability(ModeCapabilityManager.MODE_CAPABILITY, null);
|
player.getCapability(ModeCapabilityManager.MODE_CAPABILITY, null);
|
||||||
|
|
||||||
if (modeCapability.isPresent()) {
|
if (modeCapability.isPresent()) {
|
||||||
ModeCapabilityManager.IModeCapability capability = modeCapability.orElse(null);
|
ModeCapabilityManager.IModeCapability capability = modeCapability.orElse(null);
|
||||||
cache = capability.getModeData();
|
if (capability.getModeData() == null){
|
||||||
if (cache == null) {
|
capability.setModeData(new ModeSettings());
|
||||||
cache = new ModeSettings();
|
|
||||||
capability.setModeData(cache);
|
|
||||||
}
|
}
|
||||||
//Add invalidation listener, to invalidate cache
|
|
||||||
modeCapability.addListener(self -> cache = null);
|
|
||||||
return capability.getModeData();
|
return capability.getModeData();
|
||||||
}
|
}
|
||||||
|
|
||||||
EffortlessBuilding.logger.warn("Player does not have modeCapability: " + player);
|
EffortlessBuilding.logger.warn("Player does not have modeCapability: " + player);
|
||||||
//Return dummy settings
|
//Return dummy settings
|
||||||
return new ModeSettings();
|
return new ModeSettings();
|
||||||
// throw new IllegalArgumentException("Player does not have modeCapability capability");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setModeSettings(Player player, ModeSettings modeSettings) {
|
public static void setModeSettings(Player player, ModeSettings modeSettings) {
|
||||||
|
|||||||
@@ -17,27 +17,20 @@ import javax.annotation.Nonnull;
|
|||||||
@Mod.EventBusSubscriber
|
@Mod.EventBusSubscriber
|
||||||
public class ModifierSettingsManager {
|
public class ModifierSettingsManager {
|
||||||
|
|
||||||
private static ModifierSettings cache = null;
|
|
||||||
|
|
||||||
//Retrieves the buildsettings of a player through the modifierCapability capability
|
//Retrieves the buildsettings of a player through the modifierCapability capability
|
||||||
//Never returns null
|
//Never returns null
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static ModifierSettings getModifierSettings(Player player) {
|
public static ModifierSettings getModifierSettings(Player player) {
|
||||||
if (cache != null) return cache;
|
|
||||||
|
|
||||||
LazyOptional<ModifierCapabilityManager.IModifierCapability> modifierCapability =
|
LazyOptional<ModifierCapabilityManager.IModifierCapability> modifierCapability =
|
||||||
player.getCapability(ModifierCapabilityManager.MODIFIER_CAPABILITY, null);
|
player.getCapability(ModifierCapabilityManager.MODIFIER_CAPABILITY, null);
|
||||||
|
|
||||||
if (modifierCapability.isPresent()) {
|
if (modifierCapability.isPresent()) {
|
||||||
ModifierCapabilityManager.IModifierCapability capability = modifierCapability.orElse(null);
|
ModifierCapabilityManager.IModifierCapability capability = modifierCapability.orElse(null);
|
||||||
cache = capability.getModifierData();
|
if (capability.getModifierData() == null){
|
||||||
if (cache == null) {
|
capability.setModifierData(new ModifierSettings());
|
||||||
cache = new ModifierSettings();
|
|
||||||
capability.setModifierData(cache);
|
|
||||||
}
|
}
|
||||||
//Add invalidation listener, to invalidate cache
|
return capability.getModifierData();
|
||||||
modifierCapability.addListener(self -> cache = null);
|
|
||||||
return cache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EffortlessBuilding.logger.warn("Player does not have modifierCapability: " + player);
|
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.network.FriendlyByteBuf;
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.world.phys.Vec3;
|
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.fml.LogicalSide;
|
||||||
import net.minecraftforge.network.NetworkEvent;
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||||
import nl.requios.effortlessbuilding.buildmodifier.BlockSet;
|
import nl.requios.effortlessbuilding.buildmodifier.BlockSet;
|
||||||
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
|
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
|
||||||
|
import nl.requios.effortlessbuilding.render.BlockPreviewRenderer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -67,25 +71,31 @@ public class AddUndoMessage {
|
|||||||
ctx.get().enqueueWork(() -> {
|
ctx.get().enqueueWork(() -> {
|
||||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||||
//Received clientside
|
//Received clientside
|
||||||
|
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, 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()));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ctx.get().setPacketHandled(true);
|
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.BlockHitResult;
|
||||||
import net.minecraft.world.phys.HitResult;
|
import net.minecraft.world.phys.HitResult;
|
||||||
import net.minecraft.world.phys.Vec3;
|
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.fml.LogicalSide;
|
||||||
import net.minecraftforge.network.NetworkEvent;
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
import nl.requios.effortlessbuilding.buildmode.BuildModes;
|
import nl.requios.effortlessbuilding.buildmode.BuildModes;
|
||||||
@@ -95,8 +98,7 @@ public class BlockPlacedMessage {
|
|||||||
ctx.get().enqueueWork(() -> {
|
ctx.get().enqueueWork(() -> {
|
||||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||||
//Received clientside
|
//Received clientside
|
||||||
//Nod RenderHandler to do the dissolve shader effect
|
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||||
BlockPreviewRenderer.onBlocksPlaced();
|
|
||||||
} else {
|
} else {
|
||||||
//Received serverside
|
//Received serverside
|
||||||
BuildModes.onBlockPlacedMessage(ctx.get().getSender(), message);
|
BuildModes.onBlockPlacedMessage(ctx.get().getSender(), message);
|
||||||
@@ -105,4 +107,12 @@ public class BlockPlacedMessage {
|
|||||||
ctx.get().setPacketHandled(true);
|
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.world.entity.player.Player;
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
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.fml.LogicalSide;
|
||||||
import net.minecraftforge.network.NetworkEvent;
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||||
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
|
import nl.requios.effortlessbuilding.buildmodifier.UndoRedo;
|
||||||
|
import nl.requios.effortlessbuilding.render.BlockPreviewRenderer;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@@ -30,13 +34,20 @@ public class ClearUndoMessage {
|
|||||||
ctx.get().enqueueWork(() -> {
|
ctx.get().enqueueWork(() -> {
|
||||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||||
//Received clientside
|
//Received clientside
|
||||||
Player player = EffortlessBuilding.proxy.getPlayerEntityFromContext(ctx);
|
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||||
|
|
||||||
//Add to undo stack clientside
|
|
||||||
UndoRedo.clear(player);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ctx.get().setPacketHandled(true);
|
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 {
|
public class PacketHandler {
|
||||||
private static final String PROTOCOL_VERSION = "1";
|
private static final String PROTOCOL_VERSION = "1";
|
||||||
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
|
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
|
||||||
new ResourceLocation(EffortlessBuilding.MODID, "main_channel"),
|
new ResourceLocation(EffortlessBuilding.MODID, "main"),
|
||||||
() -> PROTOCOL_VERSION,
|
() -> PROTOCOL_VERSION,
|
||||||
PROTOCOL_VERSION::equals,
|
PROTOCOL_VERSION::equals,
|
||||||
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.network.FriendlyByteBuf;
|
||||||
import net.minecraft.world.phys.BlockHitResult;
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
import net.minecraft.world.phys.HitResult;
|
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.fml.LogicalSide;
|
||||||
import net.minecraftforge.network.NetworkEvent;
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||||
import nl.requios.effortlessbuilding.proxy.ClientProxy;
|
import nl.requios.effortlessbuilding.proxy.ClientProxy;
|
||||||
|
import nl.requios.effortlessbuilding.render.BlockPreviewRenderer;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@@ -44,19 +48,26 @@ public class RequestLookAtMessage {
|
|||||||
ctx.get().enqueueWork(() -> {
|
ctx.get().enqueueWork(() -> {
|
||||||
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
|
||||||
//Received clientside
|
//Received clientside
|
||||||
//Send back your info
|
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientHandler.handle(message, ctx));
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ctx.get().setPacketHandled(true);
|
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;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send packet to client to translate and log the containing message
|
||||||
|
*/
|
||||||
public class TranslatedLogMessage {
|
public class TranslatedLogMessage {
|
||||||
private final String prefix;
|
private final String prefix;
|
||||||
private final String translationKey;
|
private final String translationKey;
|
||||||
|
|||||||
Reference in New Issue
Block a user