Merge branch 'master' into 1.17

# Conflicts:
#	src/main/java/carpet/network/ServerNetworkHandler.java
#	src/main/java/carpet/script/utils/SnoopyCommandSource.java
This commit is contained in:
gnembon committed 2021-04-19 23:42:38 -03:00
commit 709d74ea47
4 files changed
+112 -140

No files matched your search

@@ -3,6 +3,7 @@ package carpet.network;
import carpet.CarpetServer; import carpet.CarpetServer;
import carpet.CarpetSettings; import carpet.CarpetSettings;
import carpet.helpers.TickSpeed; import carpet.helpers.TickSpeed;
import carpet.script.utils.SnoopyCommandSource;
import carpet.settings.ParsedRule; import carpet.settings.ParsedRule;
import carpet.settings.SettingsManager; import carpet.settings.SettingsManager;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@@ -12,10 +13,9 @@ import net.minecraft.nbt.NbtString;
import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtElement;
import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import java.util.ArrayList; import java.util.ArrayList;
@@ -93,38 +93,22 @@ public class ServerNetworkHandler
String command = commandData.getString("command"); String command = commandData.getString("command");
String id = commandData.getString("id"); String id = commandData.getString("id");
List<Text> output = new ArrayList<>(); List<Text> output = new ArrayList<>();
String[] error = {null}; Text[] error = {null};
int resultCode = -1; int resultCode = -1;
if (player.getServer() == null) if (player.getServer() == null)
{ {
error[0] = "No Server"; error[0] = new LiteralText("No Server");
} }
else else
{ {
resultCode = player.getServer().getCommandManager().execute( resultCode = player.getServer().getCommandManager().execute(
new ServerCommandSource(player, player.getPos(), player.getRotationClient(), new SnoopyCommandSource(player, error, output), command
player.world instanceof ServerWorld ? (ServerWorld) player.world : null,
player.server.getPermissionLevel(player.getGameProfile()), player.getName().getString(), player.getDisplayName(),
player.world.getServer(), player)
{
@Override
public void sendError(Text message)
{
error[0] = message.getString();
}
@Override
public void sendFeedback(Text message, boolean broadcastToOps)
{
output.add(message);
}
},
command
); );
}; }
NbtCompound result = new NbtCompound(); CompoundTag result = new CompoundTag();
result.putString("id", id); result.putString("id", id);
result.putInt("code", resultCode); result.putInt("code", resultCode);
if (error[0] != null) result.putString("error", error[0]); if (error[0] != null) result.putString("error", error[0].asString());
NbtList outputResult = new NbtList(); NbtList outputResult = new NbtList();
for (Text line: output) outputResult.add(NbtString.of(Text.Serializer.toJson(line))); for (Text line: output) outputResult.add(NbtString.of(Text.Serializer.toJson(line)));
if (!output.isEmpty()) result.put("output", outputResult); if (!output.isEmpty()) result.put("output", outputResult);
@@ -669,13 +669,17 @@ public class Auxiliary {
ServerCommandSource s = ((CarpetContext)c).s; ServerCommandSource s = ((CarpetContext)c).s;
try try
{ {
Value[] error = {Value.NULL}; Text[] error = {null};
List<Value> output = new ArrayList<>(); List<Text> output = new ArrayList<>();
Value retval = new NumericValue(s.getMinecraftServer().getCommandManager().execute( Value retval = new NumericValue(s.getMinecraftServer().getCommandManager().execute(
new SnoopyCommandSource(s, posf, error, output), new SnoopyCommandSource(s, posf, error, output),
lv.get(0).evalValue(c).getString()) lv.get(0).evalValue(c).getString())
); );
Value ret = ListValue.of(retval, ListValue.wrap(output), error[0]); Value ret = ListValue.of(
retval,
ListValue.wrap(output.stream().map(FormattedTextValue::new).collect(Collectors.toList())),
FormattedTextValue.of(error[0])
);
return (c_, t_) -> ret; return (c_, t_) -> ret;
} }
catch (Exception exc) catch (Exception exc)
@@ -1,10 +1,7 @@
package carpet.script.utils; package carpet.script.utils;
import carpet.CarpetSettings; import carpet.CarpetSettings;
import carpet.script.value.FormattedTextValue;
import carpet.script.value.Value;
import com.mojang.brigadier.ResultConsumer; import com.mojang.brigadier.ResultConsumer;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.argument.EntityAnchorArgumentType; import net.minecraft.command.argument.EntityAnchorArgumentType;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -14,41 +11,90 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec2f; import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.dimension.DimensionType;
import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
import java.util.function.BinaryOperator; import java.util.function.BinaryOperator;
public class SnoopyCommandSource extends ServerCommandSource public class SnoopyCommandSource extends ServerCommandSource
{ {
private Vec3d position; private final CommandOutput output;
private ServerWorld world; private final Vec3d position;
private String simpleName; private final ServerWorld world;
private Text name; private final int level;
private final String simpleName;
private final Text name;
private final MinecraftServer server; private final MinecraftServer server;
private Entity entity; // skipping silent since snooper is never silent
private ResultConsumer<ServerCommandSource> resultConsumer; private final Entity entity;
private EntityAnchorArgumentType.EntityAnchor entityAnchor; private final ResultConsumer<ServerCommandSource> resultConsumer;
private Vec2f rotation; private final EntityAnchorArgumentType.EntityAnchor entityAnchor;
private final Value[] error; private final Vec2f rotation;
private final List<Value> chatOutput; // good stuff
private final Text[] error;
private final List<Text> chatOutput;
public SnoopyCommandSource(ServerCommandSource original, Vec3d pos, Value[] error, List<Value> chatOutput) public SnoopyCommandSource(ServerCommandSource original, Vec3d pos, Text[] error, List<Text> chatOutput)
{ {
super(CommandOutput.DUMMY, pos, Vec2f.ZERO, original.getWorld(), CarpetSettings.runPermissionLevel, super(CommandOutput.DUMMY, pos, Vec2f.ZERO, original.getWorld(), CarpetSettings.runPermissionLevel,
original.getName(), original.getDisplayName(), original.getMinecraftServer(), original.getEntity(), false, original.getName(), original.getDisplayName(), original.getMinecraftServer(), original.getEntity(), false,
(ctx, succ, res) -> { }, EntityAnchorArgumentType.EntityAnchor.FEET); (ctx, succ, res) -> { }, EntityAnchorArgumentType.EntityAnchor.FEET);
position = pos; this.output = CommandOutput.DUMMY;
world = original.getWorld(); this.position = pos;
simpleName = original.getName(); this.world = original.getWorld();
name = original.getDisplayName(); this.level = CarpetSettings.runPermissionLevel;
server = original.getMinecraftServer(); this.simpleName = original.getName();
entity = original.getEntity(); this.name = original.getDisplayName();
resultConsumer = (ctx, succ, res) -> { }; this.server = original.getMinecraftServer();
entityAnchor = original.getEntityAnchor(); this.entity = original.getEntity();
rotation = Vec2f.ZERO; this.resultConsumer = (ctx, succ, res) -> { };
this.entityAnchor = original.getEntityAnchor();
this.rotation = Vec2f.ZERO;
this.error = error;
this.chatOutput = chatOutput;
}
public SnoopyCommandSource(ServerPlayerEntity player, Text[] error, List<Text> output) {
super(player, player.getPos(), player.getRotationClient(),
player.world instanceof ServerWorld ? (ServerWorld) player.world : null,
player.server.getPermissionLevel(player.getGameProfile()), player.getName().getString(), player.getDisplayName(),
player.world.getServer(), player);
this.output = player;
this.position = player.getPos();
this.world = player.world instanceof ServerWorld ? (ServerWorld) player.world : null;
this.level = player.server.getPermissionLevel(player.getGameProfile());
this.simpleName = player.getName().getString();
this.name = player.getDisplayName();
this.server = player.world.getServer();
this.entity = player;
this.resultConsumer = (ctx, succ, res) -> { };
this.entityAnchor = EntityAnchorArgumentType.EntityAnchor.FEET;
this.rotation = Vec2f.ZERO;
this.error = error;
this.chatOutput = output;
}
private SnoopyCommandSource(CommandOutput output, Vec3d pos, Vec2f rot, ServerWorld world, int level, String simpleName, Text name, MinecraftServer server, @Nullable Entity entity, ResultConsumer<ServerCommandSource> consumer, EntityAnchorArgumentType.EntityAnchor entityAnchor,
Text[] error, List<Text> chatOutput
) {
super(output, pos, rot, world, level,
simpleName, name, server, entity, false,
consumer, entityAnchor);
this.output = output;
this.position = pos;
this.rotation = rot;
this.world = world;
this.level = level;
this.simpleName = simpleName;
this.name = name;
this.server = server;
this.entity = entity;
this.resultConsumer = consumer;
this.entityAnchor = entityAnchor;
this.error = error; this.error = error;
this.chatOutput = chatOutput; this.chatOutput = chatOutput;
} }
@@ -56,31 +102,30 @@ public class SnoopyCommandSource extends ServerCommandSource
@Override @Override
public ServerCommandSource withEntity(Entity entity) public ServerCommandSource withEntity(Entity entity)
{ {
this.entity = entity; return new SnoopyCommandSource(output, position, rotation, world, level, entity.getName().getString(), entity.getDisplayName(), server, entity, resultConsumer, entityAnchor, error, chatOutput);
this.simpleName = entity.getName().getString();
this.name = entity.getDisplayName();
return this;
} }
@Override @Override
public ServerCommandSource withPosition(Vec3d position) public ServerCommandSource withPosition(Vec3d position)
{ {
this.position = position; return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, resultConsumer, entityAnchor, error, chatOutput);
return this;
} }
@Override @Override
public ServerCommandSource withRotation(Vec2f rotation) public ServerCommandSource withRotation(Vec2f rotation)
{ {
this.rotation = rotation; return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, resultConsumer, entityAnchor, error, chatOutput);
return this;
} }
@Override @Override
public ServerCommandSource withConsumer(ResultConsumer<ServerCommandSource> consumer) public ServerCommandSource withConsumer(ResultConsumer<ServerCommandSource> consumer)
{ {
this.resultConsumer = consumer; return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, consumer, entityAnchor, error, chatOutput);
return this; }
public ServerCommandSource mergeConsumers(ResultConsumer<ServerCommandSource> consumer, BinaryOperator<ResultConsumer<ServerCommandSource>> binaryOperator) {
ResultConsumer<ServerCommandSource> resultConsumer = binaryOperator.apply(this.resultConsumer, consumer);
return this.withConsumer(resultConsumer);
} }
//@Override // only used in fuctions and we really don't care to track these actually, besides the basic output //@Override // only used in fuctions and we really don't care to track these actually, besides the basic output
@@ -102,103 +147,37 @@ public class SnoopyCommandSource extends ServerCommandSource
@Override @Override
public ServerCommandSource withEntityAnchor(EntityAnchorArgumentType.EntityAnchor anchor) public ServerCommandSource withEntityAnchor(EntityAnchorArgumentType.EntityAnchor anchor)
{ {
entityAnchor = anchor; return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, resultConsumer, anchor, error, chatOutput);
return this;
} }
@Override @Override
public ServerCommandSource withWorld(ServerWorld world) public ServerCommandSource withWorld(ServerWorld world)
{ {
this.world = world;
double d = DimensionType.getCoordinateScaleFactor(this.world.getDimension(), world.getDimension()); double d = DimensionType.getCoordinateScaleFactor(this.world.getDimension(), world.getDimension());
this.position = new Vec3d(this.position.x * d, this.position.y, this.position.z * d); Vec3d position = new Vec3d(this.position.x * d, this.position.y, this.position.z * d);
return this; return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, resultConsumer, entityAnchor, error, chatOutput);
} }
@Override public ServerCommandSource withLookingAt(Vec3d position) throws CommandSyntaxException {
public Text getDisplayName() Vec3d vec3d = this.entityAnchor.positionAt(this);
{ double d = position.x - vec3d.x;
return name; double e = position.y - vec3d.y;
} double f = position.z - vec3d.z;
double g = (double) MathHelper.sqrt(d * d + f * f);
@Override float h = MathHelper.wrapDegrees((float)(-(MathHelper.atan2(e, g) * 57.2957763671875D)));
public String getName() float i = MathHelper.wrapDegrees((float)(MathHelper.atan2(f, d) * 57.2957763671875D) - 90.0F);
{ return this.withRotation(new Vec2f(h, i));
return simpleName;
}
@Override
public Vec3d getPosition()
{
return position;
}
@Override
public ServerWorld getWorld()
{
return world;
}
@Override
public Entity getEntity()
{
return entity;
}
@Override
public Entity getEntityOrThrow() throws CommandSyntaxException
{
if (entity == null) {
throw REQUIRES_ENTITY_EXCEPTION.create();
} else {
return entity;
}
}
@Override
public ServerPlayerEntity getPlayer() throws CommandSyntaxException
{
if (!(this.entity instanceof ServerPlayerEntity)) {
throw REQUIRES_PLAYER_EXCEPTION.create();
} else {
return (ServerPlayerEntity)this.entity;
}
}
@Override
public Vec2f getRotation()
{
return rotation;
}
@Override
public MinecraftServer getMinecraftServer()
{
return server;
}
@Override
public EntityAnchorArgumentType.EntityAnchor getEntityAnchor()
{
return entityAnchor;
}
@Override
public void onCommandComplete(CommandContext<ServerCommandSource> context, boolean success, int result)
{
if (this.resultConsumer != null) resultConsumer.onCommandComplete(context, success, result);
} }
@Override @Override
public void sendError(Text message) public void sendError(Text message)
{ {
error[0] = new FormattedTextValue(message); error[0] = message;
} }
@Override @Override
public void sendFeedback(Text message, boolean broadcastToOps) public void sendFeedback(Text message, boolean broadcastToOps)
{ {
chatOutput.add(new FormattedTextValue(message)); chatOutput.add(message);
} }
} }
@@ -42,6 +42,11 @@ public class FormattedTextValue extends StringValue
} }
} }
public static Value of(Text text) {
if (text == null) return Value.NULL;
return new FormattedTextValue(text);
}
@Override @Override
public String getString() { public String getString() {
return text.getString(); return text.getString();