Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8461214

Browse files
committed
Address code reviews
1 parent 67c3353 commit 8461214

File tree

24 files changed

+151
-151
lines changed

24 files changed

+151
-151
lines changed

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPI.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,18 @@ public static void onLoad(CommandAPIConfig<?> config) {
108108

109109
// Setup variables
110110
CommandAPI.config = new InternalConfig(config);
111-
InternalConfig platformConfig = CommandAPIVersionHandler.getConfig(config);
112111

113112
// Initialize handlers
114-
LoadContext loadContext = CommandAPIVersionHandler.getPlatform(platformConfig);
113+
LoadContext loadContext = CommandAPIVersionHandler.getPlatform(config);
115114
CommandAPIPlatform<?, ?, ?> platform = loadContext.platform();
116115
new CommandAPIHandler<>(platform);
117116
loadContext.context().run();
118117

119118
// Load the platform
120-
CommandAPIHandler.getInstance().onLoad(config);
119+
CommandAPIHandler.getInstance().onLoad();
121120

122121
// Log platform load
123-
final String platformClassHierarchy;
124-
{
125-
List<String> platformClassHierarchyList = new ArrayList<>();
126-
Class<?> platformClass = platform.getClass();
127-
// Goes up through class inheritance only (ending at Object, but we don't want to include that)
128-
// CommandAPIPlatform is an interface, so it is not included
129-
while (platformClass != null && platformClass != Object.class) {
130-
platformClassHierarchyList.add(platformClass.getSimpleName());
131-
platformClass = platformClass.getSuperclass();
132-
}
133-
platformClassHierarchy = String.join(" > ", platformClassHierarchyList);
134-
}
135-
logNormal("Loaded platform " + platformClassHierarchy);
122+
logNormal("Loaded platform " + getPlatformMessage(platform));
136123

137124
loaded = true;
138125
} else {
@@ -148,6 +135,22 @@ public static void onLoad(CommandAPIConfig<?> config) {
148135
}
149136
}
150137

138+
static String getPlatformMessage(Object platform) {
139+
final String platformClassHierarchy;
140+
{
141+
List<String> platformClassHierarchyList = new ArrayList<>();
142+
Class<?> platformClass = platform.getClass();
143+
// Goes up through class inheritance only (ending at Object, but we don't want to include that)
144+
// CommandAPIPlatform is an interface, so it is not included
145+
while (platformClass != null && platformClass != Object.class) {
146+
platformClassHierarchyList.add(platformClass.getSimpleName());
147+
platformClass = platformClass.getSuperclass();
148+
}
149+
platformClassHierarchy = String.join(" > ", platformClassHierarchyList);
150+
}
151+
return platformClassHierarchy;
152+
}
153+
151154
/**
152155
* Enables the CommandAPI. This should be placed at the start of your
153156
* <code>onEnable()</code> method.

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ protected CommandAPIHandler(CommandAPIPlatform<Argument, CommandSender, Source>
134134
CommandAPIHandler.instance = this;
135135
}
136136

137-
public void onLoad(CommandAPIConfig<?> config) {
137+
public void onLoad() {
138138
checkDependencies();
139-
platform.onLoad(config);
139+
platform.onLoad();
140140
}
141141

142142
private void checkDependencies() {

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIPlatform.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ public interface CommandAPIPlatform<Argument
3030
/**
3131
* Platform-specific stuff that should happen when the CommandAPI is loaded,
3232
* such as checking dependencies and initializing helper classes.
33-
*
34-
* @param config the configuration to use for the CommandAPI.
3533
*/
36-
void onLoad(CommandAPIConfig<?> config);
34+
void onLoad();
3735

3836
/**
3937
* Platform-specific stuff that should happen when the CommandAPI is enabled

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@
77
*/
88
public abstract class CommandAPIVersionHandler {
99

10-
static Object getVersion() {
11-
throw new IllegalStateException("You have the wrong copy of the CommandAPI! If you're shading, did you use commandapi-core instead of commandapi-{platform}-shade?");
12-
}
13-
1410
/**
1511
* Returns an instance of the version's implementation of CommandAPIPlatform.
1612
*
1713
* @return an instance of CommandAPIPlatform which can run on the currently active server
1814
*/
19-
static LoadContext getPlatform(InternalConfig config) {
15+
static LoadContext getPlatform(CommandAPIConfig<?> config) {
2016
throw new IllegalStateException("You have the wrong copy of the CommandAPI! If you're shading, did you use commandapi-core instead of commandapi-{platform}-shade?");
2117
}
2218

23-
static InternalConfig getConfig(CommandAPIConfig<?> config) {
24-
throw new IllegalStateException("You have the wrong copy of the CommandAPI! If you're shading, did you use commandapi-core instead of commandapi-{platform}-shade?");
25-
}
2619
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/BukkitPlatform.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public interface BukkitPlatform<Source> extends CommandAPIPlatform<Argument<?>, CommandSender, Source> {
1010

1111
@Override
12-
void onLoad(CommandAPIConfig<?> config);
12+
void onLoad();
1313

1414
CommandMap getCommandMap();
1515

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPIBukkitConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public <NBT> T initializeNBTAPI(Class<NBT> nbtContainerClass,
7979

8080
/**
8181
* Sets the default namespace to use when register commands
82+
* <p>
83+
* This namespace can't be {@code null} or empty and has to match the regex: {@code [0-9a-z_.-]+}
8284
*
8385
* @param namespace the namespace to use when register commands
8486
* @return this CommandAPIConfig

commandapi-platforms/commandapi-paper/commandapi-paper-core/src/main/java/dev/jorel/commandapi/CommandAPIPaper.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ public class CommandAPIPaper<Source> extends CommandAPIBukkit<Source> {
4040
private CommandAPILogger bootstrapLogger;
4141

4242
private LifecycleEventOwner lifecycleEventOwner;
43-
private final BundledNMS<Source> nms;
4443

4544
@SuppressWarnings("unchecked")
46-
protected CommandAPIPaper(InternalPaperConfig config) {
45+
protected CommandAPIPaper(InternalPaperConfig config, BundledNMS<Source> nms) {
4746
CommandAPIPaper.paper = this;
4847
setInternalConfig(config);
48+
this.lifecycleEventOwner = config.getLifecycleEventOwner();
4949

50-
VersionContext context = (VersionContext) CommandAPIVersionHandler.getVersion();
51-
context.context().accept(getLogger());
52-
context.additionalContext().accept(getLogger());
53-
this.nms = (BundledNMS<Source>) context.nms();
54-
super.nms = this.nms;
50+
super.nms = nms;
5551

5652
Class<? extends CommandSender> tempFeedbackForwardingCommandSender = null;
5753
Class<? extends CommandSender> tempNullCommandSender = null;
@@ -93,20 +89,13 @@ private static void setInternalConfig(InternalPaperConfig config) {
9389
@Override
9490
public BundledNMS<Source> getNMS() {
9591
if (nms != null) {
96-
return this.nms;
92+
return (BundledNMS<Source>) this.nms;
9793
}
9894
throw new IllegalStateException("Tried to access NMS instance, but it was null! Are you using CommandAPI features before calling CommandAPI#onLoad?");
9995
}
10096

10197
@Override
102-
public void onLoad(CommandAPIConfig<?> config) {
103-
if (config instanceof CommandAPIPaperConfig<? extends LifecycleEventOwner> paperConfig) {
104-
CommandAPIPaper.setInternalConfig(new InternalPaperConfig(paperConfig));
105-
this.lifecycleEventOwner = paperConfig.lifecycleEventOwner;
106-
} else {
107-
CommandAPI.logError("CommandAPIPaper was loaded with non-Paper config!");
108-
CommandAPI.logError("Attempts to access Paper-specific config variables will fail!");
109-
}
98+
public void onLoad() {
11099
super.onLoad();
111100
checkPaperDependencies();
112101
PaperCommandRegistration registration = (PaperCommandRegistration) CommandAPIBukkit.get().getCommandRegistrationStrategy();
@@ -120,7 +109,7 @@ public void onLoad(CommandAPIConfig<?> config) {
120109
@Override
121110
public void onEnable() {
122111
super.plugin = (JavaPlugin) Bukkit.getPluginManager().getPlugin(getConfiguration().getPluginName());
123-
CommandAPIPaper.getPaper().lifecycleEventOwner = super.plugin;
112+
this.lifecycleEventOwner = super.plugin;
124113

125114
new Schedulers(paper.isFoliaPresent).scheduleSyncDelayed(plugin, () -> {
126115
CommandAPIBukkit.get().getCommandRegistrationStrategy().runTasksAfterServerStart();
@@ -204,7 +193,7 @@ public Platform activePlatform() {
204193

205194
@Override
206195
public CommandRegistrationStrategy<Source> createCommandRegistrationStrategy() {
207-
return nms.createCommandRegistrationStrategy();
196+
return getNMS().createCommandRegistrationStrategy();
208197
}
209198

210199
@Override

commandapi-platforms/commandapi-paper/commandapi-paper-core/src/main/java/dev/jorel/commandapi/InternalPaperConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@
88
public class InternalPaperConfig extends InternalBukkitConfig {
99

1010
private final PluginMeta pluginMeta;
11+
private final LifecycleEventOwner lifecycleEventOwner;
1112
private final boolean isCommandAPIPlugin;
1213

1314
public InternalPaperConfig(CommandAPIPaperConfig<? extends LifecycleEventOwner> config) {
1415
super(config);
1516
this.pluginMeta = config.pluginMeta;
17+
this.lifecycleEventOwner = config.lifecycleEventOwner;
1618
this.isCommandAPIPlugin = config.isCommandAPIPlugin;
1719
}
1820

1921
boolean isCommandAPIPlugin() {
2022
return isCommandAPIPlugin;
2123
}
2224

25+
/**
26+
* @return the {@link LifecycleEventOwner} loading the CommandAPI
27+
*/
28+
public LifecycleEventOwner getLifecycleEventOwner() {
29+
return lifecycleEventOwner;
30+
}
31+
2332
/**
2433
* @return The {@link PluginMeta} of the plugin loading the CommandAPI
2534
*/

commandapi-platforms/commandapi-paper/commandapi-paper-core/src/main/java/dev/jorel/commandapi/VersionContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
import java.util.function.Consumer;
66

7-
public record VersionContext(BundledNMS<?> nms, Consumer<CommandAPILogger> context, Consumer<CommandAPILogger> additionalContext) {
8-
9-
public VersionContext(BundledNMS<?> nms, Consumer<CommandAPILogger> context) {
10-
this(nms, context, (logger) -> {});
11-
}
7+
public record VersionContext(BundledNMS<?> nms, Consumer<CommandAPILogger> context) {
128

139
public VersionContext(BundledNMS<?> nms) {
1410
this(nms, (logger) -> {});

commandapi-platforms/commandapi-paper/commandapi-paper-nms/commandapi-paper-api/src/main/java/dev/jorel/commandapi/nms/APITypeProvider.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,16 @@ public class APITypeProvider extends BundledNMS<CommandSourceStack> {
9494

9595
private final PaperNMS<CommandSourceStack> paperNMS;
9696

97+
private final SimpleCommandExceptionType noEntitiesFound;
98+
private final SimpleCommandExceptionType noPlayersFound;
99+
97100
public APITypeProvider(PaperNMS<?> paperNMS) {
98101
this.paperNMS = (PaperNMS<CommandSourceStack>) paperNMS;
99102
bukkitNMS();
103+
104+
GsonComponentSerializer gson = GsonComponentSerializer.gson();
105+
this.noEntitiesFound = new SimpleCommandExceptionType(paperNMS.bukkitNMS().generateMessageFromJson(gson.serialize(Component.translatable("argument.entity.notfound.entity"))));
106+
this.noPlayersFound = new SimpleCommandExceptionType(paperNMS.bukkitNMS().generateMessageFromJson(gson.serialize(Component.translatable("argument.entity.notfound.player"))));
100107
}
101108

102109
private ArgumentType<?> getArgumentType(ThrowingSupplier<ArgumentType<?>> paper, Supplier<ArgumentType<?>> nms) {
@@ -452,23 +459,20 @@ public Enchantment getEnchantment(CommandContext<CommandSourceStack> cmdCtx, Str
452459

453460
@Override
454461
public EntitySelectorParser getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, String key) {
455-
GsonComponentSerializer gson = GsonComponentSerializer.gson();
456-
SimpleCommandExceptionType NO_ENTITIES_FOUND = new SimpleCommandExceptionType(paperNMS.bukkitNMS().generateMessageFromJson(gson.serialize(Component.translatable("argument.entity.notfound.entity"))));
457-
SimpleCommandExceptionType NO_PLAYERS_FOUND = new SimpleCommandExceptionType(paperNMS.bukkitNMS().generateMessageFromJson(gson.serialize(Component.translatable("argument.entity.notfound.player"))));
458462
return new EntitySelectorParser(
459463
() -> cmdCtx.getArgument(key, PlayerSelectorArgumentResolver.class).resolve(cmdCtx.getSource()).getFirst(),
460464
() -> cmdCtx.getArgument(key, EntitySelectorArgumentResolver.class).resolve(cmdCtx.getSource()).getFirst(),
461465
(allowEmpty) -> {
462466
List<Player> players = cmdCtx.getArgument(key, PlayerSelectorArgumentResolver.class).resolve(cmdCtx.getSource());
463467
if (players.isEmpty() && !allowEmpty) {
464-
throw NO_PLAYERS_FOUND.create();
468+
throw noPlayersFound.create();
465469
}
466470
return players;
467471
},
468472
(allowEmpty) -> {
469473
List<Entity> entities = cmdCtx.getArgument(key, EntitySelectorArgumentResolver.class).resolve(cmdCtx.getSource());
470474
if (entities.isEmpty() && !allowEmpty) {
471-
throw NO_ENTITIES_FOUND.create();
475+
throw noEntitiesFound.create();
472476
}
473477
return entities;
474478
}
@@ -609,7 +613,7 @@ public OfflinePlayer getOfflinePlayer(CommandContext<CommandSourceStack> cmdCtx,
609613
}
610614

611615
private UUID getIdFromProfile(CommandContext<CommandSourceStack> ctx, String name) throws CommandSyntaxException {
612-
Collection<PlayerProfile> playerProfiles = ctx.getArgument(name, PlayerProfileListResolver.class).resolve((CommandSourceStack) ctx.getSource());
616+
Collection<PlayerProfile> playerProfiles = ctx.getArgument(name, PlayerProfileListResolver.class).resolve(ctx.getSource());
613617
UUID id = playerProfiles.iterator().next().getId();
614618
if (id == null) {
615619
throw new SimpleCommandExceptionType(BukkitTooltip.messageFromAdventureComponent(Component.translatable("argument.player.unknown"))).create();
@@ -636,7 +640,7 @@ public Recipe getRecipe(CommandContext<CommandSourceStack> cmdCtx, String key) t
636640
public Rotation getRotation(CommandContext<CommandSourceStack> cmdCtx, String key) throws CommandSyntaxException {
637641
return parseT(cmdCtx, key,
638642
(ctx, name) -> {
639-
io.papermc.paper.math.Rotation rotation = ctx.getArgument(name, RotationResolver.class).resolve((CommandSourceStack) ctx.getSource());
643+
io.papermc.paper.math.Rotation rotation = ctx.getArgument(name, RotationResolver.class).resolve(ctx.getSource());
640644
return new Rotation(rotation.yaw(), rotation.pitch());
641645
},
642646
(ctx, name) -> paperNMS.bukkitNMS().getRotation(ctx, name)
@@ -791,11 +795,11 @@ public List<PlayerProfile> getProfile(CommandContext<CommandSourceStack> cmdCtx,
791795

792796
@Override
793797
public NMS<CommandSourceStack> bukkitNMS() {
794-
return ((PaperNMS<CommandSourceStack>) paperNMS).bukkitNMS();
798+
return paperNMS.bukkitNMS();
795799
}
796800

797801
@Override
798802
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
799-
return ((PaperNMS<CommandSourceStack>) paperNMS).createCommandRegistrationStrategy();
803+
return paperNMS.createCommandRegistrationStrategy();
800804
}
801805
}

0 commit comments

Comments
 (0)