-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
515 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
common/src/main/java/io/github/cadiboo/nocubes/network/NoCubesNetwork.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.github.cadiboo.nocubes.network; | ||
|
||
import io.github.cadiboo.nocubes.NoCubes; | ||
import io.github.cadiboo.nocubes.util.BlockStateSerializer; | ||
import io.github.cadiboo.nocubes.util.ModUtil; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Arrays; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
public class NoCubesNetwork { | ||
|
||
/** | ||
* From the minecraft wiki. | ||
* 1. Ops can bypass spawn protection. | ||
* 2. Ops can use /clear, /difficulty, /effect, /gamemode, /gamerule, /give, /summon, /setblock and /tp, and can edit command blocks. | ||
* 3. Ops can use /ban, /deop, /whitelist, /kick, and /op. | ||
* 4. Ops can use /stop. | ||
*/ | ||
public static final int REQUIRED_PERMISSION_LEVEL = 2; | ||
|
||
public static final String NETWORK_PROTOCOL_VERSION = "2"; | ||
|
||
public static boolean checkPermissionAndNotifyIfUnauthorised(Player player, @Nullable MinecraftServer connectedToServer) { | ||
if (connectedToServer != null && connectedToServer.isSingleplayerOwner(player.getGameProfile())) | ||
return true; | ||
if (player.hasPermissions(REQUIRED_PERMISSION_LEVEL)) | ||
return true; | ||
ModUtil.warnPlayer(player, NoCubes.MOD_ID + ".command.changeSmoothableNoPermission"); | ||
return false; | ||
} | ||
|
||
@FunctionalInterface | ||
public interface SendS2CUpdateSmoothable { | ||
void send(@Nullable ServerPlayer playerIfNotNullElseEveryone, boolean newValue, BlockState[] states); | ||
} | ||
|
||
public static void handleC2SRequestUpdateSmoothable( | ||
ServerPlayer sender, | ||
boolean newValue, BlockState[] states, | ||
Consumer<Runnable> enqueueWork, | ||
SendS2CUpdateSmoothable network | ||
) { | ||
if (NoCubesNetwork.checkPermissionAndNotifyIfUnauthorised(sender, sender.server)) { | ||
var statesToUpdate = Arrays.stream(states) | ||
.filter(s -> NoCubes.smoothableHandler.isSmoothable(s) != newValue) | ||
.toArray(BlockState[]::new); | ||
// Guards against useless config reload and/or someone spamming these packets to the server and the server spamming all clients | ||
if (statesToUpdate.length == 0) | ||
// Somehow the client is out of sync, just notify them | ||
network.send(sender, newValue, states); | ||
else { | ||
enqueueWork.accept(() -> ModUtil.platform.updateServerConfigSmoothable(newValue, statesToUpdate)); | ||
// Send back update to all clients | ||
network.send(null, newValue, statesToUpdate); | ||
} | ||
} | ||
} | ||
|
||
public interface Serializer { | ||
|
||
static void encodeS2CUpdateServerConfig(FriendlyByteBuf buffer, byte[] data) { | ||
buffer.writeByteArray(data); | ||
} | ||
|
||
static <T> T decodeS2CUpdateServerConfig(FriendlyByteBuf buffer, Function<byte[], T> constructor) { | ||
var data = buffer.readByteArray(); | ||
return constructor.apply(data); | ||
} | ||
|
||
static void encodeUpdateSmoothable(FriendlyByteBuf buffer, boolean newValue, BlockState[] states) { | ||
buffer.writeBoolean(newValue); | ||
BlockStateSerializer.writeBlockStatesTo(buffer, states); | ||
} | ||
|
||
static <T> T decodeUpdateSmoothable(FriendlyByteBuf buffer, UpdateSmoothableConstructor<T> constructor) { | ||
var newValue = buffer.readBoolean(); | ||
var states = BlockStateSerializer.readBlockStatesFrom(buffer); | ||
return constructor.apply(newValue, states); | ||
} | ||
|
||
interface UpdateSmoothableConstructor<T> { | ||
T apply(boolean newValue, BlockState[] states); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
common/src/main/java/io/github/cadiboo/nocubes/network/NoCubesNetworkClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.github.cadiboo.nocubes.network; | ||
|
||
import io.github.cadiboo.nocubes.NoCubes; | ||
import io.github.cadiboo.nocubes.client.ClientUtil; | ||
import io.github.cadiboo.nocubes.client.KeyMappings; | ||
import io.github.cadiboo.nocubes.config.NoCubesConfig; | ||
import io.github.cadiboo.nocubes.util.ModUtil; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static io.github.cadiboo.nocubes.client.RenderHelper.reloadAllChunks; | ||
|
||
public class NoCubesNetworkClient { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
/** | ||
* Only valid when connected to a server on the client. | ||
* Contains random values from the most recently pinged server otherwise. | ||
* Also valid for singleplayer integrated servers (always true). | ||
*/ | ||
public static boolean currentServerHasNoCubes = false; | ||
|
||
public static void handleS2CUpdateServerConfig(Consumer<Runnable> enqueueWork, byte[] configData) { | ||
ClientUtil.platform.receiveSyncedServerConfig(configData); | ||
} | ||
|
||
public static void handleS2CUpdateSmoothable(Consumer<Runnable> enqueueWork, boolean newValue, BlockState[] states) { | ||
enqueueWork.accept(() -> { | ||
NoCubes.smoothableHandler.setSmoothable(newValue, states); | ||
reloadAllChunks("the server told us that the smoothness of some states changed"); | ||
}); | ||
} | ||
|
||
public static void onJoinedServer(boolean forgeAlreadyLoadedDefaultConfig) { | ||
LOG.debug("Client joined server"); | ||
loadDefaultServerConfigIfWeAreOnAModdedServerWithoutNoCubes(forgeAlreadyLoadedDefaultConfig); | ||
ClientUtil.sendPlayerInfoMessage(); | ||
ClientUtil.warnPlayerIfVisualsDisabled(); | ||
if (!currentServerHasNoCubes) { | ||
// This lets players not phase through the ground on servers that don't have NoCubes installed | ||
NoCubesConfig.Server.collisionsEnabled = false; | ||
ClientUtil.warnPlayer(NoCubes.MOD_ID + ".notification.notInstalledOnServer", KeyMappings.translate(KeyMappings.TOGGLE_SMOOTHABLE_BLOCK_TYPE)); | ||
} | ||
} | ||
|
||
/** | ||
* This lets NoCubes load properly on modded servers that don't have it installed | ||
*/ | ||
private static void loadDefaultServerConfigIfWeAreOnAModdedServerWithoutNoCubes(boolean forgeAlreadyLoadedDefaultConfig) { | ||
if (currentServerHasNoCubes) { | ||
// Forge has synced the server config to us, no need to load the default (see ConfigSync.syncConfigs) | ||
LOG.debug("Not loading default server config - current server has NoCubes installed"); | ||
return; | ||
} | ||
|
||
if (forgeAlreadyLoadedDefaultConfig) { | ||
// Forge has already loaded the default server configs for us (see NetworkHooks#handleClientLoginSuccess(Connection)) | ||
LOG.debug("Not loading default server config - Forge has already loaded it for us"); | ||
return; | ||
} | ||
|
||
LOG.debug("Connected to a modded server that doesn't have NoCubes installed, loading default server config"); | ||
ModUtil.platform.loadDefaultServerConfig(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
fabric/src/main/java/io/github/cadiboo/nocubes/config/NoCubesConfigImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.github.cadiboo.nocubes.config; | ||
|
||
import io.github.cadiboo.nocubes.client.ClientUtil; | ||
import io.github.cadiboo.nocubes.client.KeyMappings; | ||
import io.github.cadiboo.nocubes.util.ModUtil; | ||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
|
||
import java.util.ArrayList; | ||
|
||
// TODO: REPLACE WITH AN ACTUAL CONFIG SYSTEM | ||
/** | ||
* @see NoCubesConfig | ||
*/ | ||
public final class NoCubesConfigImpl { | ||
|
||
public static void updateServerConfigSmoothable(boolean newValue, BlockState[] states) { | ||
warnConfigNotImplemented(); | ||
var whitelist = new ArrayList<String>(); | ||
var blacklist = new ArrayList<String>(); | ||
NoCubesConfig.Smoothables.updateUserDefinedSmoothableStringLists(newValue, states, whitelist, blacklist); | ||
NoCubesConfig.Smoothables.recomputeInMemoryLookup(BuiltInRegistries.BLOCK.stream(), whitelist, blacklist, true); | ||
} | ||
|
||
public static void loadDefaultServerConfig() { | ||
warnConfigNotImplemented(); | ||
NoCubesConfig.Common.debugEnabled = true; | ||
NoCubesConfig.Client.render = true; | ||
NoCubesConfig.Client.renderSelectionBox = true; | ||
NoCubesConfig.Client.selectionBoxColor = new ColorParser.Color(0, 0, 0, 0x66).toRenderableColor(); | ||
NoCubesConfig.Server.mesher = NoCubesConfig.Server.MesherType.SurfaceNets.instance; | ||
NoCubesConfig.Server.collisionsEnabled = true; | ||
NoCubesConfig.Server.tempMobCollisionsDisabled = true; | ||
NoCubesConfig.Server.extendFluidsRange = 3; | ||
updateServerConfigSmoothable(true, new BlockState[0]); | ||
} | ||
|
||
public static byte[] readConfigFileBytes() { | ||
return new byte[0]; | ||
} | ||
|
||
public static void receiveSyncedServerConfig(byte[] configData) { | ||
warnConfigNotImplemented(); | ||
// TODO: Actually use the data | ||
assert configData.length == 0; // Since we are just debugging, and have no config system yet | ||
ModUtil.platform.loadDefaultServerConfig(); | ||
} | ||
|
||
static void warnConfigNotImplemented() { | ||
// Copied and tweaked from the bit of NoCubesNetworkClient.onJoinedServer that sends the notification.notInstalledOnServer message | ||
var message = "!!! The Fabric version of NoCubes does not have a config system yet - any changes you make will not be saved"; | ||
ClientUtil.warnPlayer(message, KeyMappings.translate(KeyMappings.TOGGLE_SMOOTHABLE_BLOCK_TYPE)); | ||
} | ||
|
||
/** | ||
* Implementation of {@link NoCubesConfig.Common} | ||
*/ | ||
public static class Common { | ||
} | ||
|
||
/** | ||
* Implementation of {@link NoCubesConfig.Client} | ||
*/ | ||
public static class Client { | ||
public static void updateRender(boolean render) { | ||
warnConfigNotImplemented(); | ||
NoCubesConfig.Client.render = render; | ||
} | ||
} | ||
|
||
/** | ||
* Implementation of {@link NoCubesConfig.Server} | ||
*/ | ||
public static class Server { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.