Skip to content

Commit

Permalink
refactor: make FakePlayerManager module and BetterInfo module sub…
Browse files Browse the repository at this point in the history
…-module of `carpet module` + fix: luckperms can't identify fake-player from carpet-fabric
  • Loading branch information
sakurawald committed Jul 18, 2024
1 parent a52a764 commit 107cb7d
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 98 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/github/sakurawald/Fuji.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
// TODO: command warmup module
// TODO: tppos module
// TODO: invsee module
// TODO: powertool modul
// TODO: powertool module
// TODO: code review for skin module
// TODO: hologram module
// TODO: kit module (/kit <editor/give>)

// TODO: check luckperms with carpet-fabric

// TODO: a lisp-like DSL (parser and code-walker) for specific command with context and placeholders (%fuji:play_time_total%).
// TODO: refactor command facility (selector, aop, options, parser)
// TODO: add native shell support command
Expand Down
142 changes: 76 additions & 66 deletions src/main/java/io/github/sakurawald/config/model/ConfigModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public class Modules {
public NewbieWelcome newbie_welcome = new NewbieWelcome();
public TeleportWarmup teleport_warmup = new TeleportWarmup();
public MOTD motd = new MOTD();
public FakePlayerManager fake_player_manager = new FakePlayerManager();
public BetterInfo better_info = new BetterInfo();
public CommandCooldown command_cooldown = new CommandCooldown();
public TopChunks top_chunks = new TopChunks();
public Chat chat = new Chat();
Expand Down Expand Up @@ -223,69 +221,6 @@ public class TeleportWarmup {
public double interrupt_distance = 1d;
}

@Documentation("""
Enable this module requires `carpet-fabric` mod installed.
This module provides some management for `fake-player` and `/player` command.
""")
public class FakePlayerManager {
public boolean enable = false;

@Documentation("""
How many fake-player can each player spawn (in different time)?
The tuple means (day_of_week, minutes_of_the_day, max_fake_player_per_player).
The range of day_of_week is [1,7].
The range of minutes_of_the_day is [0, 1440].
For example: (1, 0, 2) means if the days_of_week >= 1, and minutes_of_the_day >= 0, then the max_fake_player_per_player now is 2.
Besides, you can add multi rules, the rules are checked from up to down.
The first rule that matches current time will be used to decide the max_fake_player_per_player.
You can issue `/player who` to see the owner of the fake-player.
Only the owner can operates the fake-player. (Op can bypass this limit)
""")
public ArrayList<List<Integer>> caps_limit_rule = new ArrayList<>() {
{
this.add(List.of(1, 0, 2));
}
};

@Documentation("""
How long should we renew when a player issue command `/player renew`
The command `/player renew` allows the player to manually renew all of his `fake-player`.
If a fake-player don't gets renew, then it will expired and get killed.
Use-case: to avoid some long-term alive fake-player.
""")
public int renew_duration_ms = 1000 * 60 * 60 * 12;

@Documentation("""
The rule to transform the name of fake-player.
Use-case: add prefix or suffix for fake-player.
""")
public String transform_name = "_fake_%name%";

@Documentation("""
Should we use local skin for fake-player?
Enable this can prevent fetching skins from mojang official server each time the fake-player is spawned.
This is mainly used in some network siatuation if your network to mojang official server is bad.
""")
public boolean use_local_random_skins_for_fake_player = true;
}

@Documentation("""
Adds nbt query and entity query for carpet command `/info`.
""")
public class BetterInfo {
public boolean enable = false;
}

@Documentation("""
This module provides a cooldown before each command the player issued.
Expand Down Expand Up @@ -1132,7 +1067,7 @@ public class Anvil {
This module provides commands to open `remote functional blocks` (e.g. workbench, enderchest...)
""")
public class Functional {
public boolean enable = true;
public boolean enable = false;

public Workbench workbench = new Workbench();
@Documentation("This module provides `/workbench` command.")
Expand Down Expand Up @@ -1205,6 +1140,81 @@ public class Loom {
}

}

public Carpet carpet = new Carpet();
@Documentation("""
This module provides some purpose about `carpet-fabric` mod.
""")
public class Carpet {
public boolean enable = false;

public FakePlayerManager fake_player_manager = new FakePlayerManager();
@Documentation("""
Enable this module requires `carpet-fabric` mod installed.
This module provides some management for `fake-player` and `/player` command.
""")
public class FakePlayerManager {
public boolean enable = true;

@Documentation("""
How many fake-player can each player spawn (in different time)?
The tuple means (day_of_week, minutes_of_the_day, max_fake_player_per_player).
The range of day_of_week is [1,7].
The range of minutes_of_the_day is [0, 1440].
For example: (1, 0, 2) means if the days_of_week >= 1, and minutes_of_the_day >= 0, then the max_fake_player_per_player now is 2.
Besides, you can add multi rules, the rules are checked from up to down.
The first rule that matches current time will be used to decide the max_fake_player_per_player.
You can issue `/player who` to see the owner of the fake-player.
Only the owner can operates the fake-player. (Op can bypass this limit)
""")
public ArrayList<List<Integer>> caps_limit_rule = new ArrayList<>() {
{
this.add(List.of(1, 0, 2));
}
};

@Documentation("""
How long should we renew when a player issue command `/player renew`
The command `/player renew` allows the player to manually renew all of his `fake-player`.
If a fake-player don't gets renew, then it will expired and get killed.
Use-case: to avoid some long-term alive fake-player.
""")
public int renew_duration_ms = 1000 * 60 * 60 * 12;

@Documentation("""
The rule to transform the name of fake-player.
Use-case: add prefix or suffix for fake-player.
""")
public String transform_name = "_fake_%name%";

@Documentation("""
Should we use local skin for fake-player?
Enable this can prevent fetching skins from mojang official server each time the fake-player is spawned.
This is mainly used in some network siatuation if your network to mojang official server is bad.
""")
public boolean use_local_random_skins_for_fake_player = true;
}

public BetterInfo better_info = new BetterInfo();
@Documentation("""
Adds nbt query and entity query for carpet command `/info`.
""")
public class BetterInfo {
public boolean enable = true;
}

}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.initializer.better_info;
package io.github.sakurawald.module.initializer.carpet.better_info;

import com.mojang.brigadier.CommandDispatcher;
import io.github.sakurawald.module.initializer.ModuleInitializer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.initializer.fake_player_manager;
package io.github.sakurawald.module.initializer.carpet.fake_player_manager;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.Command;
Expand Down Expand Up @@ -85,7 +85,7 @@ public boolean hasFakePlayers(ServerPlayerEntity player) {

public void renewFakePlayers(ServerPlayerEntity player) {
String name = player.getGameProfile().getName();
int duration = Configs.configHandler.model().modules.fake_player_manager.renew_duration_ms;
int duration = Configs.configHandler.model().modules.carpet.fake_player_manager.renew_duration_ms;
long newTime = System.currentTimeMillis() + duration;
player2expiration.put(name, newTime);
MessageUtil.sendMessage(player, "fake_player_manager.renew.success", DateUtil.toStandardDateFormat(newTime));
Expand Down Expand Up @@ -140,7 +140,7 @@ public boolean canManipulateFakePlayer(CommandContext<ServerCommandSource> ctx,
}

private int getCurrentAmountLimit() {
ArrayList<List<Integer>> rules = Configs.configHandler.model().modules.fake_player_manager.caps_limit_rule;
ArrayList<List<Integer>> rules = Configs.configHandler.model().modules.carpet.fake_player_manager.caps_limit_rule;
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
int currentDays = currentDate.getDayOfWeek().getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.mixin.better_info;
package io.github.sakurawald.module.mixin.carpet.better_info;

import carpet.commands.InfoCommand;
import net.minecraft.block.entity.BlockEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.sakurawald.module.mixin.fake_player_manager;
package io.github.sakurawald.module.mixin.carpet.fake_player_manager;

import carpet.commands.PlayerCommand;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import io.github.sakurawald.Fuji;
import io.github.sakurawald.config.Configs;
import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.initializer.fake_player_manager.FakePlayerManagerInitializer;
import io.github.sakurawald.module.initializer.carpet.fake_player_manager.FakePlayerManagerInitializer;
import io.github.sakurawald.util.MessageUtil;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -26,7 +26,7 @@ public abstract class PlayerCommandMixin {

@Unique
private static String transformFakePlayerName(String fakePlayerName) {
return Configs.configHandler.model().modules.fake_player_manager.transform_name.replace("%name%", fakePlayerName);
return Configs.configHandler.model().modules.carpet.fake_player_manager.transform_name.replace("%name%", fakePlayerName);
}

@Redirect(method = "cantSpawn", at = @At(
Expand Down Expand Up @@ -56,7 +56,7 @@ private static String transformFakePlayerName(String fakePlayerName) {
}

/* fix: fake-player auth network laggy */
if (Configs.configHandler.model().modules.fake_player_manager.use_local_random_skins_for_fake_player) {
if (Configs.configHandler.model().modules.carpet.fake_player_manager.use_local_random_skins_for_fake_player) {
String fakePlayerName = StringArgumentType.getString(context, "player");
fakePlayerName = transformFakePlayerName(fakePlayerName);
Fuji.SERVER.getUserCache().add(module.createOfflineGameProfile(fakePlayerName));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.sakurawald.module.mixin.fake_player_manager;
package io.github.sakurawald.module.mixin.carpet.fake_player_manager;

import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.initializer.fake_player_manager.FakePlayerManagerInitializer;
import io.github.sakurawald.module.initializer.carpet.fake_player_manager.FakePlayerManagerInitializer;
import io.github.sakurawald.util.PlayerUtil;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.sakurawald.module.mixin.fake_player_manager;
package io.github.sakurawald.module.mixin.carpet.fake_player_manager;

import carpet.patches.EntityPlayerMPFake;
import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.initializer.fake_player_manager.FakePlayerManagerInitializer;
import io.github.sakurawald.module.initializer.carpet.fake_player_manager.FakePlayerManagerInitializer;
import io.github.sakurawald.util.MessageUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void disconnectAllPlayers(CallbackInfo ci) {
private void onPlayerConnected(ClientConnection connection, ServerPlayerEntity serverPlayer, ConnectedClientData commonListenerCookie, CallbackInfo ci) {
// if the player isn't a server player entity, it must be someone's fake player
if (serverPlayer.getClass() != ServerPlayerEntity.class
&& Configs.configHandler.model().modules.fake_player_manager.use_local_random_skins_for_fake_player) {
&& Configs.configHandler.model().modules.carpet.fake_player_manager.use_local_random_skins_for_fake_player) {
SkinRestorer.setSkinAsync(server, Collections.singleton(serverPlayer.getGameProfile()), () -> SkinRestorer.getSkinStorage().getRandomSkin(serverPlayer.getUuid()));
}
}
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/io/github/sakurawald/util/LuckPermsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,27 @@ private static LuckPerms getAPI() {
return luckPerms;
}

public static Tristate checkPermission(String player, String permission) {
if (getAPI() == null) {
public static Tristate checkPermission(ServerPlayerEntity player, String permission) {
if (getAPI() == null || PlayerUtil.isFakePlayer(player)) {
return Tristate.UNDEFINED;
}

return getAPI().getUserManager()
return getAPI().getPlayerAdapter(ServerPlayerEntity.class)
.getUser(player)
.getCachedData()
.getPermissionData()
.checkPermission(permission);
}

public static Tristate checkPermission(ServerPlayerEntity player, String permission) {
return checkPermission(player.getGameProfile().getName(), permission);
.getPermissionData().checkPermission(permission);
}

public static boolean hasPermission(PlayerEntity player, String permission) {
return checkPermission(player.getGameProfile().getName(), permission).asBoolean();
return checkPermission((ServerPlayerEntity) player, permission).asBoolean();
}

public static boolean hasPermission(ServerPlayerEntity player, String permission) {
return checkPermission(player.getGameProfile().getName(), permission).asBoolean();
return checkPermission(player, permission).asBoolean();
}

public static <T> @NonNull Optional<T> getMeta(ServerPlayerEntity player, String meta, @NonNull Function<String, ? extends T> valueTransformer) {
if (getAPI() == null) {
if (getAPI() == null || PlayerUtil.isFakePlayer(player)) {
return Optional.empty();
}

Expand All @@ -68,7 +63,7 @@ public static boolean hasPermission(ServerPlayerEntity player, String permission
}

public static <T> String getPrefix(ServerPlayerEntity player) {
if (getAPI() == null) {
if (getAPI() == null || PlayerUtil.isFakePlayer(player)) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/fuji.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"anti_build.EntityMixin",
"anti_build.ServerPlayerInteractionManagerMixin",
"back.ServerPlayerMixin",
"better_info.InfoCommandMixin",
"carpet.better_info.InfoCommandMixin",
"bypass_chat_speed.ServerGamePacketListenerImplMixin",
"bypass_max_player_limit.DedicatedPlayerManagerMixin",
"bypass_move_speed.ServerGamePacketListenerImplMixin",
Expand All @@ -30,9 +30,9 @@
"command_spy.CommandsMixin",
"deathlog.ServerPlayerMixin",
"functional.enchantment.EnchantmentScreenHandlerMixin",
"fake_player_manager.PlayerCommandMixin",
"fake_player_manager.PlayerListMixin",
"fake_player_manager.PlayerMixin",
"carpet.fake_player_manager.PlayerCommandMixin",
"carpet.fake_player_manager.PlayerListMixin",
"carpet.fake_player_manager.PlayerMixin",
"fix_player_list_cme.PlayerListMixin",
"fix_player_list_cme.ServerLevelMixin",
"whitelist.UserWhiteListMixin",
Expand Down

0 comments on commit 107cb7d

Please sign in to comment.