mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-05 07:16:10 +00:00
improve CommandSender API
This commit is contained in:
@@ -5,11 +5,19 @@
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.entity;
|
||||
package com.dfsek.terra.api.command;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.Player;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public interface CommandSender extends Handle {
|
||||
void sendMessage(String message);
|
||||
|
||||
Optional<Entity> getEntity();
|
||||
|
||||
Optional<Player> getPlayer();
|
||||
}
|
||||
@@ -7,11 +7,12 @@
|
||||
|
||||
package com.dfsek.terra.api.entity;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
|
||||
|
||||
public interface Entity extends CommandSender {
|
||||
public interface Entity extends Handle {
|
||||
Vector3 position();
|
||||
|
||||
void position(Vector3 position);
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.dfsek.terra.api.event.events.platform;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
import com.dfsek.terra.api.event.events.Event;
|
||||
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.command.arguments.RegistryArgument;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
|
||||
|
||||
@@ -17,9 +17,16 @@
|
||||
|
||||
package com.dfsek.terra.bukkit;
|
||||
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.Player;
|
||||
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class BukkitCommandSender implements CommandSender {
|
||||
@@ -34,6 +41,22 @@ public class BukkitCommandSender implements CommandSender {
|
||||
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;
|
||||
|
||||
@@ -58,9 +58,4 @@ public class BukkitEntity implements Entity {
|
||||
public ServerWorld world() {
|
||||
return BukkitAdapter.adapt(entity.getWorld());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
entity.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,10 @@ import com.dfsek.terra.api.world.ServerWorld;
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
|
||||
|
||||
public class BukkitPlayer extends BukkitCommandSender implements Player {
|
||||
public class BukkitPlayer implements Player {
|
||||
private final org.bukkit.entity.Player delegate;
|
||||
|
||||
public BukkitPlayer(org.bukkit.entity.Player delegate) {
|
||||
super(delegate);
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@@ -60,9 +59,4 @@ public class BukkitPlayer extends BukkitCommandSender implements Player {
|
||||
public ServerWorld world() {
|
||||
return BukkitAdapter.adapt(delegate.getWorld());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
delegate.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import cloud.commandframework.bukkit.CloudBukkitCapabilities;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.paper.PaperCommandManager;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
|
||||
@@ -18,10 +18,13 @@
|
||||
package com.dfsek.terra.bukkit.world;
|
||||
|
||||
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
|
||||
import com.dfsek.terra.bukkit.BukkitEntity;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.TreeType;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
@@ -31,14 +34,13 @@ import com.dfsek.terra.api.block.state.properties.enums.Axis;
|
||||
import com.dfsek.terra.api.block.state.properties.enums.Half;
|
||||
import com.dfsek.terra.api.block.state.properties.enums.RailShape;
|
||||
import com.dfsek.terra.api.block.state.properties.enums.RedstoneConnection;
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
import com.dfsek.terra.api.inventory.ItemStack;
|
||||
import com.dfsek.terra.api.inventory.item.Enchantment;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
import com.dfsek.terra.bukkit.BukkitCommandSender;
|
||||
import com.dfsek.terra.bukkit.BukkitEntity;
|
||||
import com.dfsek.terra.bukkit.BukkitPlayer;
|
||||
import com.dfsek.terra.bukkit.world.block.BukkitBlockTypeAndItem;
|
||||
import com.dfsek.terra.bukkit.world.block.data.BukkitBlockState;
|
||||
@@ -164,11 +166,13 @@ public final class BukkitAdapter {
|
||||
}
|
||||
|
||||
public static CommandSender adapt(org.bukkit.command.CommandSender sender) {
|
||||
if(sender instanceof Player) return new BukkitPlayer((Player) sender);
|
||||
if(sender instanceof Entity) return new BukkitEntity((Entity) sender);
|
||||
return new BukkitCommandSender(sender);
|
||||
}
|
||||
|
||||
public static Entity adapt(org.bukkit.entity.Entity entity) {
|
||||
return new BukkitEntity(entity);
|
||||
}
|
||||
|
||||
public static org.bukkit.command.CommandSender adapt(CommandSender sender) {
|
||||
return ((BukkitCommandSender) sender).getHandle();
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ package com.dfsek.terra.fabric;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.fabric.FabricServerCommandManager;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
|
||||
|
||||
@@ -60,8 +60,4 @@ public abstract class EntityMixin {
|
||||
public ServerWorld terra$world() {
|
||||
return (ServerWorld) world;
|
||||
}
|
||||
|
||||
public void terra$sendMessage(String message) {
|
||||
sendSystemMessage(new LiteralText(message), UUID.randomUUID()); // TODO: look into how this actually works and make it less jank
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,22 @@
|
||||
|
||||
package com.dfsek.terra.fabric.mixin.implementations.entity;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.Text;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spongepowered.asm.mixin.Implements;
|
||||
import org.spongepowered.asm.mixin.Interface;
|
||||
import org.spongepowered.asm.mixin.Intrinsic;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
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;
|
||||
|
||||
|
||||
@Mixin(ServerCommandSource.class)
|
||||
@@ -35,7 +41,27 @@ public abstract class ServerCommandSourceMixin {
|
||||
@Shadow
|
||||
public abstract void sendFeedback(Text message, boolean broadcastToOps);
|
||||
|
||||
@Shadow
|
||||
public abstract ServerPlayerEntity getPlayer() throws CommandSyntaxException;
|
||||
|
||||
@Shadow
|
||||
@Nullable
|
||||
public abstract net.minecraft.entity.@Nullable Entity getEntity();
|
||||
|
||||
public void terra$sendMessage(String message) {
|
||||
sendFeedback(new LiteralText(message), true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Optional<Entity> terra$getEntity() {
|
||||
return Optional.ofNullable((Entity) getEntity());
|
||||
}
|
||||
|
||||
public Optional<Player> terra$getPlayer() {
|
||||
try {
|
||||
return Optional.ofNullable((Player) getPlayer());
|
||||
} catch(CommandSyntaxException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user