Skip to content

Commit

Permalink
Add common project
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 14, 2024
1 parent b78761f commit fa498fc
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 120 deletions.
3 changes: 3 additions & 0 deletions cloud-fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ configurations {

exclude("org.checkerframework")
exclude("org.apiguardian")
exclude("cloud.commandframework", "cloud-minecraft-modded-common")
}
}

Expand All @@ -29,6 +30,8 @@ dependencies {
api(platform(libs.cloud.bom))
api(libs.cloud.core)
api(libs.cloud.brigadier)
api(project(":cloud-minecraft-modded-common", configuration = "namedElements"))
include(project(":cloud-minecraft-modded-common"))

modImplementation(platform(libs.fabricApi.bom))
modImplementation(libs.fabricApi.command.api.v2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import cloud.commandframework.SenderMapper;
import cloud.commandframework.execution.ExecutionCoordinator;
import cloud.commandframework.keys.CloudKey;
import cloud.commandframework.permission.PredicatePermission;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -56,44 +54,44 @@ public final class FabricClientCommandManager<C> extends FabricCommandManager<C,
*/
@API(status = API.Status.STABLE, since = "2.0.0")
public static @NonNull FabricClientCommandManager<@NonNull FabricClientCommandSource> createNative(
final @NonNull ExecutionCoordinator<FabricClientCommandSource> execCoordinator
final @NonNull ExecutionCoordinator<FabricClientCommandSource> execCoordinator
) {
return new FabricClientCommandManager<>(execCoordinator, SenderMapper.identity());
}

/**
* Create a new command manager instance.
*
* @param commandExecutionCoordinator Execution coordinator instance. The coordinator is in charge of executing incoming
* commands. Some considerations must be made when picking a suitable execution coordinator
* for your platform. For example, an entirely asynchronous coordinator is not suitable
* when the parsers used in that particular platform are not thread safe. If you have
* commands that perform blocking operations, however, it might not be a good idea to
* use a synchronous execution coordinator. In most cases you will want to pick between
* {@link ExecutionCoordinator#simpleCoordinator()} and
* {@link ExecutionCoordinator#asyncCoordinator()}
* @param senderMapper Function that maps {@link FabricClientCommandSource} to the command sender type
* @param commandExecutionCoordinator Execution coordinator instance. The coordinator is in charge of executing incoming
* commands. Some considerations must be made when picking a suitable execution coordinator
* for your platform. For example, an entirely asynchronous coordinator is not suitable
* when the parsers used in that particular platform are not thread safe. If you have
* commands that perform blocking operations, however, it might not be a good idea to
* use a synchronous execution coordinator. In most cases you will want to pick between
* {@link ExecutionCoordinator#simpleCoordinator()} and
* {@link ExecutionCoordinator#asyncCoordinator()}
* @param senderMapper Function that maps {@link FabricClientCommandSource} to the command sender type
* @since 1.5.0
*/
@API(status = API.Status.STABLE, since = "2.0.0")
public FabricClientCommandManager(
final @NonNull ExecutionCoordinator<C> commandExecutionCoordinator,
final @NonNull SenderMapper<FabricClientCommandSource, C> senderMapper
final @NonNull ExecutionCoordinator<C> commandExecutionCoordinator,
final @NonNull SenderMapper<FabricClientCommandSource, C> senderMapper
) {
super(
commandExecutionCoordinator,
senderMapper,
new FabricCommandRegistrationHandler.Client<>(),
() -> (FabricClientCommandSource) new ClientSuggestionProvider(
Minecraft.getInstance().getConnection(),
Minecraft.getInstance()
)
commandExecutionCoordinator,
senderMapper,
new FabricCommandRegistrationHandler.Client<>(),
() -> (FabricClientCommandSource) new ClientSuggestionProvider(
Minecraft.getInstance().getConnection(),
Minecraft.getInstance()
)
);

this.registerParsers();
this.registerDefaultExceptionHandlers(
FabricClientCommandSource::sendError,
source -> source.getPlayer().getGameProfile().getName()
FabricClientCommandSource::sendError,
source -> source.getPlayer().getGameProfile().getName()
);
}

Expand All @@ -114,100 +112,4 @@ private void registerParsers() {
public boolean hasPermission(final @NonNull C sender, final @NonNull String permission) {
return true;
}

/**
* Get a permission predicate which passes when the integrated server is running.
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> integratedServerRunning() {
return PredicatePermission.of(
CloudKey.of("integrated-server-running"),
sender -> Minecraft.getInstance().hasSingleplayerServer()
);
}

/**
* Get a permission predicate which passes when the integrated server is not running.
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> integratedServerNotRunning() {
return PredicatePermission.of(
CloudKey.of("integrated-server-not-running"),
sender -> !Minecraft.getInstance().hasSingleplayerServer()
);
}

/**
* Get a permission predicate which passes when cheats are enabled on the currently running integrated server.
*
* <p>This predicate will always pass if there is no integrated server running, i.e. when connected to a multiplayer server.</p>
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsAllowed() {
return cheatsAllowed(true);
}

/**
* Get a permission predicate which passes when cheats are enabled on the currently running integrated server.
*
* <p>When there is no integrated server running, i.e. when connected to a multiplayer server, the predicate will
* fall back to the provided boolean argument.</p>
*
* @param allowOnMultiplayer whether the predicate should pass on multiplayer servers
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsAllowed(final boolean allowOnMultiplayer) {
return PredicatePermission.of(CloudKey.of("cheats-allowed"), sender -> {
if (!Minecraft.getInstance().hasSingleplayerServer()) {
return allowOnMultiplayer;
}
return Minecraft.getInstance().getSingleplayerServer().getPlayerList().isAllowCheatsForAllPlayers()
|| Minecraft.getInstance().getSingleplayerServer().getWorldData().getAllowCommands();
});
}

/**
* Get a permission predicate which passes when cheats are disabled on the currently running integrated server.
*
* <p>This predicate will always pass if there is no integrated server running, i.e. when connected to a multiplayer server.</p>
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsDisallowed() {
return cheatsDisallowed(true);
}

/**
* Get a permission predicate which passes when cheats are disabled on the currently running integrated server.
*
* <p>When there is no integrated server running, i.e. when connected to a multiplayer server, the predicate will
* fall back to the provided boolean argument.</p>
*
* @param allowOnMultiplayer whether the predicate should pass on multiplayer servers
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsDisallowed(final boolean allowOnMultiplayer) {
return PredicatePermission.of(CloudKey.of("cheats-disallowed"), sender -> {
if (!Minecraft.getInstance().hasSingleplayerServer()) {
return allowOnMultiplayer;
}
return !Minecraft.getInstance().getSingleplayerServer().getPlayerList().isAllowCheatsForAllPlayers()
&& !Minecraft.getInstance().getSingleplayerServer().getWorldData().getAllowCommands();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import cloud.commandframework.fabric.FabricClientCommandManager;
import cloud.commandframework.fabric.argument.FabricVanillaArgumentParsers;
import cloud.commandframework.fabric.testmod.mixin.PauseScreenAccess;
import cloud.commandframework.minecraft.modded.ClientPredicatePermissions;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void onInitializeClient() {
.handler(ctx -> disconnectClient(Minecraft.getInstance())));

commandManager.command(base.literal("requires_cheats")
.permission(FabricClientCommandManager.cheatsAllowed(false))
.permission(ClientPredicatePermissions.cheatsAllowed(false))
.handler(ctx -> ctx.sender().sendFeedback(Component.literal("Cheats are enabled!"))));

// Test argument which requires CommandBuildContext/RegistryAccess
Expand Down
20 changes: 20 additions & 0 deletions cloud-minecraft-modded-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id("conventions.base")
id("conventions.publishing")
id("xyz.jpenilla.quiet-architectury-loom")
}

dependencies {
minecraft(libs.minecraft)
mappings(loom.officialMojangMappings())
compileOnly(libs.fabricLoader)

compileOnly(platform(libs.cloud.bom))
compileOnly(libs.cloud.core)
}

tasks.jar {
manifest {
attributes("Fabric-Loom-Remap" to true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.minecraft.modded;

import cloud.commandframework.keys.CloudKey;
import cloud.commandframework.permission.PredicatePermission;
import net.minecraft.client.Minecraft;
import org.checkerframework.checker.nullness.qual.NonNull;

public final class ClientPredicatePermissions {
private ClientPredicatePermissions() {
}

/**
* Get a permission predicate which passes when the integrated server is running.
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> integratedServerRunning() {
return PredicatePermission.of(
CloudKey.of("integrated-server-running"),
sender -> Minecraft.getInstance().hasSingleplayerServer()
);
}

/**
* Get a permission predicate which passes when the integrated server is not running.
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> integratedServerNotRunning() {
return PredicatePermission.of(
CloudKey.of("integrated-server-not-running"),
sender -> !Minecraft.getInstance().hasSingleplayerServer()
);
}

/**
* Get a permission predicate which passes when cheats are enabled on the currently running integrated server.
*
* <p>This predicate will always pass if there is no integrated server running, i.e. when connected to a multiplayer server.</p>
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsAllowed() {
return cheatsAllowed(true);
}

/**
* Get a permission predicate which passes when cheats are enabled on the currently running integrated server.
*
* <p>When there is no integrated server running, i.e. when connected to a multiplayer server, the predicate will
* fall back to the provided boolean argument.</p>
*
* @param allowOnMultiplayer whether the predicate should pass on multiplayer servers
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsAllowed(final boolean allowOnMultiplayer) {
return PredicatePermission.of(CloudKey.of("cheats-allowed"), sender -> {
if (!Minecraft.getInstance().hasSingleplayerServer()) {
return allowOnMultiplayer;
}
return Minecraft.getInstance().getSingleplayerServer().getPlayerList().isAllowCheatsForAllPlayers()
|| Minecraft.getInstance().getSingleplayerServer().getWorldData().getAllowCommands();
});
}

/**
* Get a permission predicate which passes when cheats are disabled on the currently running integrated server.
*
* <p>This predicate will always pass if there is no integrated server running, i.e. when connected to a multiplayer server.</p>
*
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsDisallowed() {
return cheatsDisallowed(true);
}

/**
* Get a permission predicate which passes when cheats are disabled on the currently running integrated server.
*
* <p>When there is no integrated server running, i.e. when connected to a multiplayer server, the predicate will
* fall back to the provided boolean argument.</p>
*
* @param allowOnMultiplayer whether the predicate should pass on multiplayer servers
* @param <C> sender type
* @return a predicate permission
* @since 1.5.0
*/
public static <C> @NonNull PredicatePermission<C> cheatsDisallowed(final boolean allowOnMultiplayer) {
return PredicatePermission.of(CloudKey.of("cheats-disallowed"), sender -> {
if (!Minecraft.getInstance().hasSingleplayerServer()) {
return allowOnMultiplayer;
}
return !Minecraft.getInstance().getSingleplayerServer().getPlayerList().isAllowCheatsForAllPlayers()
&& !Minecraft.getInstance().getSingleplayerServer().getWorldData().getAllowCommands();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Shared classes for modded Minecraft Cloud implementations.
*/
package cloud.commandframework.minecraft.modded;
9 changes: 9 additions & 0 deletions cloud-neoforge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ configurations {

exclude("org.checkerframework")
exclude("org.apiguardian")
exclude("cloud.commandframework", "cloud-minecraft-modded-common")
}
forgeExtra {
extendsFrom(api.get())
Expand All @@ -24,6 +25,7 @@ dependencies {
api(platform(libs.cloud.bom))
api(libs.cloud.core)
api(libs.cloud.brigadier)
compileOnlyApi(project(":cloud-minecraft-modded-common", configuration = "namedElements"))
}

tasks {
Expand All @@ -34,6 +36,13 @@ tasks {
expand(props)
}
}
jar {
from(
zipTree(
project(":cloud-minecraft-modded-common").tasks.named<AbstractArchiveTask>("jar").flatMap { it.archiveFile }
)
)
}
}

loom {
Expand Down
Loading

0 comments on commit fa498fc

Please sign in to comment.