Migrated to modern PaperCommandManager

This commit is contained in:
OakLoaf
2025-09-30 19:36:50 +01:00
parent d97478d2c4
commit 64546bb154
4 changed files with 61 additions and 87 deletions

View File

@@ -1,62 +0,0 @@
/*
* This file is part of Terra.
*
* Terra is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Terra is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dfsek.terra.bukkit;
import org.bukkit.ChatColor;
import java.util.Optional;
import com.dfsek.terra.api.command.CommandSender;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
public class BukkitCommandSender implements CommandSender {
private final org.bukkit.command.CommandSender delegate;
public BukkitCommandSender(org.bukkit.command.CommandSender delegate) {
this.delegate = delegate;
}
@Override
public void sendMessage(String message) {
delegate.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
@Override
public Optional<Entity> getEntity() {
if(delegate instanceof org.bukkit.entity.Entity entity) {
return Optional.of(BukkitAdapter.adapt(entity));
}
return Optional.empty();
}
@Override
public Optional<Player> getPlayer() {
if(delegate instanceof org.bukkit.entity.Player player) {
return Optional.of(BukkitAdapter.adapt(player));
}
return Optional.empty();
}
@Override
public org.bukkit.command.CommandSender getHandle() {
return delegate;
}
}

View File

@@ -0,0 +1,46 @@
package com.dfsek.terra.bukkit;
import com.dfsek.terra.api.command.CommandSender;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.ChatColor;
import java.util.Optional;
public class CloudCommandSender implements CommandSender {
private final CommandSourceStack delegate;
public CloudCommandSender(CommandSourceStack delegate) {
this.delegate = delegate;
}
@Override
public void sendMessage(String message) {
delegate.getSender().sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
@Override
public Optional<Entity> getEntity() {
if(delegate instanceof org.bukkit.entity.Entity entity) {
return Optional.of(BukkitAdapter.adapt(entity));
}
return Optional.empty();
}
@Override
public Optional<Player> getPlayer() {
if(delegate instanceof org.bukkit.entity.Player player) {
return Optional.of(BukkitAdapter.adapt(player));
}
return Optional.empty();
}
@Override
public CommandSourceStack getHandle() {
return delegate;
}
}

View File

@@ -23,10 +23,8 @@ import org.bukkit.Bukkit;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.incendo.cloud.SenderMapper; import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.brigadier.CloudBrigadierManager;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.execution.ExecutionCoordinator; import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.LegacyPaperCommandManager; import org.incendo.cloud.paper.PaperCommandManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -73,7 +71,7 @@ public class TerraBukkitPlugin extends JavaPlugin {
platform.getEventManager().callEvent(new PlatformInitializationEvent()); platform.getEventManager().callEvent(new PlatformInitializationEvent());
try { try {
LegacyPaperCommandManager<CommandSender> commandManager = getCommandSenderPaperCommandManager(); PaperCommandManager<CommandSender> commandManager = getCommandSenderPaperCommandManager();
platform.getEventManager().callEvent(new CommandRegistrationEvent(commandManager)); platform.getEventManager().callEvent(new CommandRegistrationEvent(commandManager));
@@ -93,25 +91,15 @@ public class TerraBukkitPlugin extends JavaPlugin {
} }
@NotNull @NotNull
private LegacyPaperCommandManager<CommandSender> getCommandSenderPaperCommandManager() throws Exception { private PaperCommandManager<CommandSender> getCommandSenderPaperCommandManager() throws Exception {
// TODO: Update to PaperCommandManager PaperCommandManager<CommandSender> commandManager = PaperCommandManager.builder(SenderMapper.create(
LegacyPaperCommandManager<CommandSender> commandManager = new LegacyPaperCommandManager<>(
this,
ExecutionCoordinator.simpleCoordinator(),
SenderMapper.create(
BukkitAdapter::adapt, BukkitAdapter::adapt,
BukkitAdapter::adapt BukkitAdapter::adapt
)); ))
.executionCoordinator(ExecutionCoordinator.simpleCoordinator())
.buildOnEnable(this);
if(commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) { commandManager.brigadierManager().setNativeNumberSuggestions(false);
commandManager.registerBrigadier();
final CloudBrigadierManager<?, ?> brigManager = commandManager.brigadierManager();
if(brigManager != null) {
brigManager.setNativeNumberSuggestions(false);
}
} else if(commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
commandManager.registerAsynchronousCompletions();
}
return commandManager; return commandManager;
} }

View File

@@ -18,6 +18,9 @@
package com.dfsek.terra.bukkit.world; package com.dfsek.terra.bukkit.world;
import com.dfsek.terra.bukkit.CloudCommandSender;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -38,7 +41,6 @@ import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.ServerWorld; import com.dfsek.terra.api.world.ServerWorld;
import com.dfsek.terra.api.world.chunk.Chunk; import com.dfsek.terra.api.world.chunk.Chunk;
import com.dfsek.terra.api.world.info.WorldProperties; import com.dfsek.terra.api.world.info.WorldProperties;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.BukkitEntity; import com.dfsek.terra.bukkit.BukkitEntity;
import com.dfsek.terra.bukkit.BukkitPlayer; import com.dfsek.terra.bukkit.BukkitPlayer;
import com.dfsek.terra.bukkit.world.block.BukkitBlockTypeAndItem; import com.dfsek.terra.bukkit.world.block.BukkitBlockTypeAndItem;
@@ -157,16 +159,16 @@ public final class BukkitAdapter {
return Vector3.of(vector.getX(), vector.getY(), vector.getZ()); return Vector3.of(vector.getX(), vector.getY(), vector.getZ());
} }
public static CommandSender adapt(org.bukkit.command.CommandSender sender) { public static CommandSender adapt(CommandSourceStack sender) {
return new BukkitCommandSender(sender); return new CloudCommandSender(sender);
} }
public static Entity adapt(org.bukkit.entity.Entity entity) { public static Entity adapt(org.bukkit.entity.Entity entity) {
return new BukkitEntity(entity); return new BukkitEntity(entity);
} }
public static org.bukkit.command.CommandSender adapt(CommandSender sender) { public static CommandSourceStack adapt(CommandSender sender) {
return ((BukkitCommandSender) sender).getHandle(); return ((CloudCommandSender) sender).getHandle();
} }
public static ServerWorld adapt(org.bukkit.World world) { public static ServerWorld adapt(org.bukkit.World world) {