2023-03-15 21:52:13 +00:00
|
|
|
// SPDX-FileCopyrightText: 2017 The CC: Tweaked Developers
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-12-30 16:14:07 +00:00
|
|
|
package dan200.computercraft.shared.command;
|
2017-12-06 15:51:02 +00:00
|
|
|
|
2021-08-03 20:46:53 +00:00
|
|
|
import net.minecraft.commands.CommandSourceStack;
|
2023-08-27 11:15:55 +00:00
|
|
|
import net.minecraft.server.level.ServerPlayer;
|
2017-12-06 15:51:02 +00:00
|
|
|
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
2017-12-06 15:51:02 +00:00
|
|
|
/**
|
|
|
|
* The level a user must be at in order to execute a command.
|
|
|
|
*/
|
2021-08-03 20:46:53 +00:00
|
|
|
public enum UserLevel implements Predicate<CommandSourceStack> {
|
2017-12-06 15:51:02 +00:00
|
|
|
/**
|
|
|
|
* Can only be used by ops.
|
|
|
|
*/
|
|
|
|
OP,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be used by any op, or the player in SSP.
|
|
|
|
*/
|
|
|
|
OWNER_OP,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be used by anyone.
|
|
|
|
*/
|
|
|
|
ANYONE;
|
|
|
|
|
|
|
|
public int toLevel() {
|
|
|
|
return switch (this) {
|
|
|
|
case OP, OWNER_OP -> 2;
|
|
|
|
case ANYONE -> 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
@Override
|
2021-08-03 20:46:53 +00:00
|
|
|
public boolean test(CommandSourceStack source) {
|
2017-12-06 15:51:02 +00:00
|
|
|
if (this == ANYONE) return true;
|
2021-08-25 21:09:24 +00:00
|
|
|
if (this == OWNER_OP && isOwner(source)) return true;
|
2021-01-09 19:22:58 +00:00
|
|
|
return source.hasPermission(toLevel());
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
2021-08-25 21:09:24 +00:00
|
|
|
|
2023-08-27 11:15:55 +00:00
|
|
|
public boolean test(ServerPlayer source) {
|
|
|
|
if (this == ANYONE) return true;
|
|
|
|
if (this == OWNER_OP && isOwner(source)) return true;
|
|
|
|
return source.hasPermissions(toLevel());
|
2023-07-05 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 11:15:55 +00:00
|
|
|
public static boolean isOwner(CommandSourceStack source) {
|
2021-08-25 21:09:24 +00:00
|
|
|
var server = source.getServer();
|
2023-08-27 11:15:55 +00:00
|
|
|
var player = source.getPlayer();
|
2021-08-25 21:09:24 +00:00
|
|
|
return server.isDedicatedServer()
|
|
|
|
? source.getEntity() == null && source.hasPermission(4) && source.getTextName().equals("Server")
|
2023-08-27 11:15:55 +00:00
|
|
|
: player != null && server.isSingleplayerOwner(player.getGameProfile());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isOwner(ServerPlayer player) {
|
|
|
|
var server = player.getServer();
|
|
|
|
return server != null && server.isSingleplayerOwner(player.getGameProfile());
|
2021-08-25 21:09:24 +00:00
|
|
|
}
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|