implement ServerCommandSourceMixin

This commit is contained in:
dfsek 2021-05-02 20:17:59 -07:00
parent abc069046c
commit c0368f1c6d
5 changed files with 28 additions and 35 deletions

View File

@ -362,7 +362,7 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
private RequiredArgumentBuilder<ServerCommandSource, String> assemble(RequiredArgumentBuilder<ServerCommandSource, String> in, CommandManager manager) {
return in.suggests((context, builder) -> {
List<String> args = parseCommand(context.getInput());
CommandSender sender = FabricAdapter.adapt(context.getSource());
CommandSender sender = (CommandSender) context.getSource();
try {
manager.tabComplete(args.remove(0), sender, args).forEach(builder::suggest);
} catch(CommandException e) {
@ -372,7 +372,7 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
}).executes(context -> {
List<String> args = parseCommand(context.getInput());
try {
manager.execute(args.remove(0), FabricAdapter.adapt(context.getSource()), args);
manager.execute(args.remove(0), (CommandSender) context.getSource(), args);
} catch(CommandException e) {
context.getSource().sendError(new LiteralText(e.getMessage()));
}

View File

@ -0,0 +1,25 @@
package com.dfsek.terra.fabric.mixin.entity;
import com.dfsek.terra.api.platform.CommandSender;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(ServerCommandSource.class)
@Implements(@Interface(iface = CommandSender.class, prefix = "vw$"))
public abstract class ServerCommandSourceMixin {
@Shadow
public abstract void sendFeedback(Text message, boolean broadcastToOps);
public void vw$sendMessage(String message) {
sendFeedback(new LiteralText(message), true);
}
public Object vw$getHandle() {
return this;
}
}

View File

@ -1,7 +1,6 @@
package com.dfsek.terra.fabric.world;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.block.BlockFace;
import com.dfsek.terra.api.platform.block.BlockType;
import com.dfsek.terra.api.platform.entity.EntityType;
@ -18,17 +17,13 @@ import com.dfsek.terra.fabric.world.block.data.FabricRotatable;
import com.dfsek.terra.fabric.world.block.data.FabricSlab;
import com.dfsek.terra.fabric.world.block.data.FabricStairs;
import com.dfsek.terra.fabric.world.block.data.FabricWaterlogged;
import com.dfsek.terra.fabric.world.entity.FabricCommandSender;
import com.dfsek.terra.fabric.world.entity.FabricEntityType;
import com.dfsek.terra.fabric.world.entity.FabricPlayer;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
@ -65,11 +60,6 @@ public final class FabricAdapter {
return new FabricBlockData(state);
}
public static CommandSender adapt(ServerCommandSource serverCommandSource) {
if(serverCommandSource.getEntity() instanceof PlayerEntity) return new FabricPlayer((PlayerEntity) serverCommandSource.getEntity());
return new FabricCommandSender(serverCommandSource);
}
public static Direction adapt(BlockFace face) {
switch(face) {
case NORTH:

View File

@ -1,23 +0,0 @@
package com.dfsek.terra.fabric.world.entity;
import com.dfsek.terra.api.platform.CommandSender;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
public class FabricCommandSender implements CommandSender {
private final ServerCommandSource delegate;
public FabricCommandSender(ServerCommandSource delegate) {
this.delegate = delegate;
}
@Override
public void sendMessage(String message) {
delegate.sendFeedback(new LiteralText(message), true);
}
@Override
public Object getHandle() {
return delegate;
}
}

View File

@ -5,6 +5,7 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinGeneratorOptions",
"entity.ServerCommandSourceMixin",
"world.ChunkRegionMixin",
"world.ProtoChunkMixin",
"world.WorldChunkMixin"