Skip to content

Commit

Permalink
Command API (v2) and Permissions API skeleton (FabricMC#99)
Browse files Browse the repository at this point in the history
* Add stuff

* Fix imports

* it builds now

* Move the testmod

* permisisons api api?
  • Loading branch information
BoogieMonster1O1 authored Nov 29, 2021
1 parent 7de9e7e commit 1f3fe90
Show file tree
Hide file tree
Showing 93 changed files with 9,561 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ allprojects {
license {
header rootProject.file('HEADER')
include '**/*.java'
exclude '**/lib/sponge/*.java'
exclude '**/lib/sponge/**/*.java'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.util;

import net.minecraft.util.math.BlockPos;

public class Location<T> {
private final T t;
private final BlockPos pos;

public Location(T t, BlockPos pos) {
this.t = t;
this.pos = pos;
}

public BlockPos getPos() {
return this.pos;
}

public T getT() {
return this.t;
}
}
5 changes: 5 additions & 0 deletions modules/server/permissions/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = getSubprojectVersion(project, "1.0.0")

dependencies {
implementation project(path: ':legacy-fabric-api-base', configuration: 'dev')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.permission.v1;

import net.minecraft.command.CommandSource;

/**
* Represents a {@link CommandSource} that can not be able to run
* a command if they do not have the permission to do so.
*/
public interface PermissibleCommandSource extends CommandSource {
boolean hasPermission(String perm);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.permission.v1;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;

public class PermissionsApiHolder {
private static final Logger LOGGER = LogManager.getLogger();
private static PlayerPermissionsApi PLAYER_PERMISSIONS_API = null;

public static boolean setPlayerPermissionsApi(PlayerPermissionsApi api) {
if (PLAYER_PERMISSIONS_API == null) {
PLAYER_PERMISSIONS_API = api;
return true;
}

LOGGER.error("Cannot register player permissions api with id {}. There is already a permissions api implementor!", api.getId());
return false;
}

public static PlayerPermissionsApi getPlayerPermissionsApi() {
return PLAYER_PERMISSIONS_API != null ? PLAYER_PERMISSIONS_API : FallbackPlayerPermissionsApi.INSTANCE;
}

private static class FallbackPlayerPermissionsApi implements PlayerPermissionsApi {
private static final FallbackPlayerPermissionsApi INSTANCE = new FallbackPlayerPermissionsApi();

private FallbackPlayerPermissionsApi() {
}

@Override
public String getId() {
return "legacy-fabric-fallback-permissions-api";
}

@Override
public boolean hasPermission(ServerPlayerEntity player, String perm) {
return MinecraftServer.getServer().getPlayerManager().isOperator(player.getGameProfile());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.permission.v1;

import net.minecraft.server.network.ServerPlayerEntity;

public interface PlayerPermissionsApi {
String getId();

boolean hasPermission(ServerPlayerEntity player, String perm);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.permission;

import org.spongepowered.asm.mixin.Mixin;

import net.minecraft.world.CommandBlockExecutor;

import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;

@Mixin(CommandBlockExecutor.class)
public abstract class CommandBlockExecutorMixin implements PermissibleCommandSource {
@Override
public boolean hasPermission(String perm) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.permission;

import org.spongepowered.asm.mixin.Mixin;

import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;

@Mixin(targets = "net/minecraft/command/CommandStats$1")
public abstract class CommandStats_1Mixin implements PermissibleCommandSource {
@Override
public boolean hasPermission(String perm) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.permission;

import org.spongepowered.asm.mixin.Mixin;

import net.minecraft.server.command.Console;

import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;

@Mixin(Console.class)
public abstract class ConsoleMixin implements PermissibleCommandSource {
@Override
public boolean hasPermission(String perm) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.permission;

import org.spongepowered.asm.mixin.Mixin;

import net.minecraft.entity.Entity;

import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;

@Mixin(Entity.class)
public abstract class EntityMixin implements PermissibleCommandSource {
@Override
public boolean hasPermission(String perm) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.permission;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import net.minecraft.entity.Entity;

import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;

@Mixin(targets = "net/minecraft/server/command/ExecuteCommand$1")
public abstract class ExecuteCommand_1Mixin implements PermissibleCommandSource {
@Shadow
public abstract Entity getEntity();

@Override
public boolean hasPermission(String perm) {
return ((PermissibleCommandSource) this.getEntity()).hasPermission(perm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2020 - 2021 Legacy Fabric
* Copyright (c) 2016 - 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.mixin.permission;

import org.spongepowered.asm.mixin.Mixin;

import net.minecraft.server.network.ServerPlayerEntity;

import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;
import net.legacyfabric.fabric.api.permission.v1.PermissionsApiHolder;

@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin implements PermissibleCommandSource {
@Override
public boolean hasPermission(String perm) {
return PermissionsApiHolder.getPlayerPermissionsApi().hasPermission((ServerPlayerEntity) (Object) this, perm);
}
}
Loading

0 comments on commit 1f3fe90

Please sign in to comment.