player actions

This commit is contained in:
gnembon committed 2019-07-17 03:58:16 -03:00
1 parent dc0dc9e406
commit 50d1d74579
10 files changed
+612 -1057

No files matched your search

+9 -9
View File
@@ -2,48 +2,48 @@ package carpet;
import carpet.settings.SettingsManager;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
public abstract class CarpetExtension
public interface CarpetExtension
{
/**
* When game started before world is loaded
*/
public void onGameStarted() {}
default void onGameStarted() {}
/**
* Runs once per loaded world once the World files / gamerules etc are fully loaded
* Can be loaded multiple times in SinglePlayer
*/
public void onServerLoaded(MinecraftServer server) {}
default void onServerLoaded(MinecraftServer server) {}
/**
* Runs once per game tick, as a first thing in the tick
*/
public void onTick(MinecraftServer server) {}
default void onTick(MinecraftServer server) {}
/**
* Register your own commands right after vanilla commands are added
* If that matters for you
*/
public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {}
default void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {}
/**
* Provide your own custom settings manager managed in the same way as base /carpet
* command, but separated to its own command as defined in SettingsManager.
*/
public SettingsManager customSettingsManager() {return null;}
default SettingsManager customSettingsManager() {return null;}
/**
* todiddalidoo
*/
public void onPlayerLoggedIn(PlayerEntity player) {}
default void onPlayerLoggedIn(ServerPlayerEntity player) {}
/**
* todiddalidoo
*/
public void onPlayerLoggedOut(PlayerEntity player) {}
default void onPlayerLoggedOut(ServerPlayerEntity player) {}
}
+12
View File
@@ -25,6 +25,7 @@ import carpet.utils.HUDController;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
public class CarpetServer // static for now - easier to handle all around the code, its one anyways
{
@@ -90,5 +91,16 @@ public class CarpetServer // static for now - easier to handle all around the co
extensions.forEach(e -> e.registerCommands(dispatcher));
//TestCommand.register(dispatcher);
}
public static void onPlayerLoggedIn(ServerPlayerEntity player)
{
extensions.forEach(e -> e.onPlayerLoggedIn(player));
}
public static void onPlayerLoggedOut(ServerPlayerEntity player)
{
extensions.forEach(e -> e.onPlayerLoggedOut(player));
}
}
+171 -369
View File
@@ -1,21 +1,28 @@
package carpet.commands;
import carpet.helpers.EntityPlayerActionPack;
import carpet.settings.CarpetSettings;
import carpet.fakes.ServerPlayerEntityInterface;
import carpet.patches.EntityPlayerMPFake;
import carpet.utils.Messenger;
import com.google.common.collect.Sets;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.arguments.DimensionArgumentType;
import net.minecraft.command.arguments.RotationArgumentType;
import net.minecraft.command.arguments.Vec3ArgumentType;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
@@ -24,147 +31,98 @@ import net.minecraft.world.dimension.DimensionType;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.function.Consumer;
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.server.command.CommandSource.suggestMatching;
public class PlayerCommand
{
// TODO: allow any order like execute
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("player").
requires((player) -> CarpetSettings.commandPlayer).
then(argument("player", word()).
suggests( (c, b) -> suggestMatching(getPlayers(c.getSource()), b)).
then(literal("stop").
executes(PlayerCommand::stop)).
then(literal("use").
executes(PlayerCommand::useOnce).
then(literal("once").
executes( PlayerCommand::useOnce)).
then(literal("continuous").
executes( PlayerCommand::useContinuous)).
then(literal("interval").
then(argument("ticks",integer(2)).
executes( PlayerCommand::useInterval)))).
then(literal("jump").
executes( PlayerCommand::jumpOnce).
then(literal("once").
executes( PlayerCommand::jumpOnce)).
then(literal("continuous").
executes( PlayerCommand::jumpContinuous)).
then(literal("interval").
then(argument("ticks",integer(2)).
executes( PlayerCommand::jumpInterval )))).
then(literal("attack").
executes(PlayerCommand::attackOnce).
then(literal("once").
executes(PlayerCommand::attackOnce)).
then(literal("continuous").
executes(PlayerCommand::attackContinuous)).
then(literal("interval").
then(argument("ticks",integer(2)).
executes(PlayerCommand::attackInterval)))).
then(literal("drop").
executes(PlayerCommand::dropItem)).
then(literal("swapHands").
executes(PlayerCommand::swapHands)).
then(literal("kill").
executes(PlayerCommand::kill)).
then(literal("shadow").
executes(PlayerCommand::shadow)).
then(literal("mount").
executes(PlayerCommand::mount)).
then(literal("dismount").
executes(PlayerCommand::dismount)).
then(literal("sneak").
executes(PlayerCommand::sneak)).
then(literal("sprint").
executes(PlayerCommand::sprint)).
then(literal("look").
then(literal("north").
executes(PlayerCommand::lookNorth)).
then(literal("south").
executes(PlayerCommand::lookSouth)).
then(literal("east").
executes(PlayerCommand::lookEast)).
then(literal("west").
executes(PlayerCommand::lookWest)).
then(literal("up").
executes(PlayerCommand::lookUp)).
then(literal("down").
executes(PlayerCommand::lookDown)).
then(argument("direction", RotationArgumentType.rotation()).
executes(PlayerCommand::lookAround))).
then(literal("turn").
then(literal("left").
executes(PlayerCommand::turnLeft)).
then(literal("right").
executes(PlayerCommand::turnRight)).
then(literal("back").
executes(PlayerCommand::turnBack)).
then(argument("direction",RotationArgumentType.rotation()).
executes(PlayerCommand::turn))).
then(literal("move").
then(literal("forward").
executes(PlayerCommand::moveForward)).
then(literal("backward").
executes(PlayerCommand::moveBackward)).
then(literal("left").
executes(PlayerCommand::strafeLeft)).
then(literal("right").
executes(PlayerCommand::strafeRight))).
then(literal("spawn").
executes(PlayerCommand::spawn).
then(literal("at").
then(argument("position", Vec3ArgumentType.vec3()).
executes(PlayerCommand::spawn).
then(literal("facing").
then(argument("direction",RotationArgumentType.rotation()).
executes(PlayerCommand::spawn).
then(literal("in").
then(argument("dimension", DimensionArgumentType.dimension()).
executes(PlayerCommand::spawn)))))))));
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("player")
.requires((player) -> CarpetSettings.commandPlayer)
.then(argument("player", StringArgumentType.word())
.suggests( (c, b) -> suggestMatching(getPlayers(c.getSource()), b))
.then(literal("stop").executes(PlayerCommand::stop))
.then(makeActionCommand("use", EntityPlayerActionPack.ActionType.USE))
.then(makeActionCommand("jump", EntityPlayerActionPack.ActionType.JUMP))
.then(makeActionCommand("attack", EntityPlayerActionPack.ActionType.ATTACK))
.then(makeActionCommand("drop", EntityPlayerActionPack.ActionType.DROP_ITEM))
.then(makeActionCommand("dropStack", EntityPlayerActionPack.ActionType.DROP_STACK))
.then(makeActionCommand("swapHands", EntityPlayerActionPack.ActionType.SWAP_HANDS))
.then(literal("kill").executes(PlayerCommand::kill))
.then(literal("shadow"). executes(PlayerCommand::shadow))
.then(literal("mount").executes(manipulation(EntityPlayerActionPack::mount)))
.then(literal("dismount").executes(manipulation(EntityPlayerActionPack::dismount)))
.then(literal("sneak").executes(manipulation(ap -> ap.setSneaking(true))))
.then(literal("unsneak").executes(manipulation(ap -> ap.setSneaking(false))))
.then(literal("sprint").executes(manipulation(ap -> ap.setSprinting(true))))
.then(literal("unsprint").executes(manipulation(ap -> ap.setSprinting(false))))
.then(literal("look")
.then(literal("north").executes(manipulation(ap -> ap.look(Direction.NORTH))))
.then(literal("south").executes(manipulation(ap -> ap.look(Direction.SOUTH))))
.then(literal("east").executes(manipulation(ap -> ap.look(Direction.EAST))))
.then(literal("west").executes(manipulation(ap -> ap.look(Direction.WEST))))
.then(literal("up").executes(manipulation(ap -> ap.look(Direction.UP))))
.then(literal("down").executes(manipulation(ap -> ap.look(Direction.DOWN))))
.then(argument("direction", RotationArgumentType.rotation())
.executes(c -> manipulate(c, ap -> ap.look(RotationArgumentType.getRotation(c, "direction").toAbsoluteRotation(c.getSource())))))
).then(literal("turn")
.then(literal("left").executes(c -> manipulate(c, ap -> ap.turn(-90, 0))))
.then(literal("right").executes(c -> manipulate(c, ap -> ap.turn(90, 0))))
.then(literal("back").executes(c -> manipulate(c, ap -> ap.turn(180, 0))))
.then(argument("rotation", RotationArgumentType.rotation())
.executes(c -> manipulate(c, ap -> ap.turn(RotationArgumentType.getRotation(c, "rotation").toAbsoluteRotation(c.getSource())))))
).then(literal("move")
.then(literal("forward").executes(c -> manipulate(c, ap -> ap.setForward(1))))
.then(literal("backward").executes(c -> manipulate(c, ap -> ap.setForward(-1))))
.then(literal("left").executes(c -> manipulate(c, ap -> ap.setStrafing(1))))
.then(literal("right").executes(c -> manipulate(c, ap -> ap.setStrafing(-1))))
).then(literal("spawn").executes(PlayerCommand::spawn)
.then(literal("at").then(argument("position", Vec3ArgumentType.vec3()).executes(PlayerCommand::spawn)
.then(literal("facing").then(argument("direction", RotationArgumentType.rotation()).executes(PlayerCommand::spawn)
.then(literal("in").then(argument("dimension", DimensionArgumentType.dimension()).executes(PlayerCommand::spawn)))
))
))
)
);
dispatcher.register(literalargumentbuilder);
}
private static LiteralArgumentBuilder<ServerCommandSource> makeActionCommand(String actionName, EntityPlayerActionPack.ActionType type)
{
return literal(actionName)
.executes(c -> action(c, type, EntityPlayerActionPack.Action.once()))
.then(literal("once").executes(c -> action(c, type, EntityPlayerActionPack.Action.once())))
.then(literal("continuous").executes(c -> action(c, type, EntityPlayerActionPack.Action.continuous())))
.then(literal("interval").then(argument("ticks", IntegerArgumentType.integer(2))
.executes(c -> action(c, type, EntityPlayerActionPack.Action.interval(IntegerArgumentType.getInteger(c, "ticks"))))));
}
private static Collection<String> getPlayers(ServerCommandSource source)
{
Set<String> players = Sets.newLinkedHashSet( Arrays.asList("Steve","Alex"));
Set<String> players = Sets.newLinkedHashSet(Arrays.asList("Steve", "Alex"));
players.addAll(source.getPlayerNames());
return players;
}
private static ServerPlayerEntity getPlayer(CommandContext<ServerCommandSource> context)
{
String playerName = getString(context,"player");
String playerName = StringArgumentType.getString(context, "player");
MinecraftServer server = context.getSource().getMinecraftServer();
return server.getPlayerManager().getPlayer(playerName);
}
private static boolean cantSpawn(CommandContext<ServerCommandSource> context)
{
String playerName = getString(context,"player");
PlayerEntity player = context.getSource().getMinecraftServer().getPlayerManager().getPlayer(playerName);
if (player != null)
{
Messenger.m(context.getSource(), "r Player ", "rb "+playerName, "r is already logged on");
return true;
}
return false;
}
private static boolean cantManipulate(CommandContext<ServerCommandSource> context)
{
PlayerEntity player = getPlayer(context);
if (player == null)
{
Messenger.m(context.getSource(),"r Can only manipulate existing players");
Messenger.m(context.getSource(), "r Can only manipulate existing players");
return true;
}
PlayerEntity sendingPlayer;
@@ -177,301 +135,145 @@ public class PlayerCommand
return false;
}
if (!(context.getSource().getMinecraftServer().getPlayerManager().isOperator(sendingPlayer.getGameProfile())))
if (!context.getSource().getMinecraftServer().getPlayerManager().isOperator(sendingPlayer.getGameProfile()))
{
if (!(sendingPlayer == player || player instanceof EntityPlayerMPFake))
if (sendingPlayer != player && !(player instanceof EntityPlayerMPFake))
{
Messenger.m(context.getSource(),"r Non OP players can't control other real players");
Messenger.m(context.getSource(), "r Non OP players can't control other real players");
return true;
}
}
return false;
}
private static boolean cantShadow(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context))
return true;
PlayerEntity player = getPlayer(context);
if (player instanceof EntityPlayerMPFake)
{
Messenger.m(context.getSource(), "r Only real players can be shadowed");
return true;
}
return false;
}
private static boolean cantReMove(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context))
return true;
if (cantManipulate(context)) return true;
PlayerEntity player = getPlayer(context);
if (!(player instanceof EntityPlayerMPFake))
if (player instanceof EntityPlayerMPFake) return false;
Messenger.m(context.getSource(), "r Only fake players can be moved or killed");
return true;
}
private static boolean cantSpawn(CommandContext<ServerCommandSource> context)
{
String playerName = StringArgumentType.getString(context, "player");
MinecraftServer server = context.getSource().getMinecraftServer();
PlayerManager manager = server.getPlayerManager();
PlayerEntity player = manager.getPlayer(playerName);
if (player != null)
{
Messenger.m(context.getSource(), "r Only fake players can be moved or killed");
Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is already logged on");
return true;
}
GameProfile profile = server.getUserCache().findByName(playerName);
if (manager.getUserBanList().contains(profile))
{
Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is banned");
return true;
}
if (manager.isWhitelistEnabled() && profile != null && manager.isWhitelisted(profile) && !context.getSource().hasPermissionLevel(2))
{
Messenger.m(context.getSource(), "r Whitelisted players can only be spawned by operators");
return true;
}
return false;
}
private static int stop(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().stop();
return 1;
}
private static int useOnce(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().useOnce();
return 1;
}
private static int useContinuous(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setUseForever();
return 1;
}
private static int useInterval(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
int ticks = getInteger(context, "ticks");
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setUse(ticks, 0);
return 1;
}
private static int jumpOnce(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().jumpOnce();
return 1;
}
private static int jumpContinuous(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setJumpForever();
return 1;
}
private static int jumpInterval(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
int ticks = getInteger(context, "ticks");
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setJump(ticks, 0);
return 1;
}
private static int attackOnce(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().attackOnce();
//((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().resetBlockRemoving(true);
return 1;
}
private static int attackContinuous(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setAttackForever();
return 1;
}
private static int attackInterval(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
int ticks = getInteger(context, "ticks");
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setAttack(ticks, 0);
return 1;
}
private static int dropItem(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().dropItem();
return 1;
}
private static int swapHands(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().swapHands();
return 1;
}
private static int kill(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
getPlayer(context).kill();
return 1;
}
private static int shadow(CommandContext<ServerCommandSource> context)
@FunctionalInterface
interface SupplierWithCommandSyntaxException<T>
{
if (cantShadow(context)) return 0;
EntityPlayerMPFake.createShadow(
context.getSource().getMinecraftServer(),
getPlayer(context));
return 1;
T get() throws CommandSyntaxException;
}
private static int mount(CommandContext<ServerCommandSource> context)
private static <T> T tryGetArg(SupplierWithCommandSyntaxException<T> a, SupplierWithCommandSyntaxException<T> b) throws CommandSyntaxException
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().mount();
return 1;
}
private static int dismount(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().dismount();
return 1;
}
private static int sneak(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setSneaking(true);
return 1;
}
private static int sprint(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setSprinting(true);
return 1;
}
private static int lookNorth(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look("north");
return 1;
}
private static int lookSouth(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look("south");
return 1;
}
private static int lookEast(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look("east");
return 1;
}
private static int lookWest(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look("west");
return 1;
}
private static int lookUp(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look("up");
return 1;
}
private static int lookDown(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look("down");
return 1;
}
private static int lookAround(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
Vec2f vec2f = RotationArgumentType.getRotation(context, "direction").toAbsoluteRotation(context.getSource());
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().look(vec2f.y,vec2f.x);
return 1;
}
private static int turnRight(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().turn("right");
return 1;
}
private static int turnLeft(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().turn("left");
return 1;
}
private static int turnBack(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().turn("back");
return 1;
}
private static int turn(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
Vec2f vec2f = RotationArgumentType.getRotation(context, "direction").toAbsoluteRotation(context.getSource());
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().turn(vec2f.y,vec2f.x);
return 1;
}
private static int moveForward(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setForward(1.0F);
return 1;
}
private static int moveBackward(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setForward(-1.0F);
return 1;
}
private static int strafeLeft(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setStrafing(-1.0F);
return 1;
}
private static int strafeRight(CommandContext<ServerCommandSource> context)
{
if (cantReMove(context)) return 0;
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().setStrafing(1.0F);
return 1;
try
{
return a.get();
}
catch (IllegalArgumentException e)
{
return b.get();
}
}
private static int spawn(CommandContext<ServerCommandSource> context) throws CommandSyntaxException
{
if (cantSpawn(context)) return 0;
Vec3d pos;
Vec2f facing;
DimensionType dim;
try
{
pos = Vec3ArgumentType.getVec3(context, "position");
}
catch (IllegalArgumentException e)
{
pos = context.getSource().getPosition();
}
try
{
facing = RotationArgumentType.getRotation(context, "direction").toAbsoluteRotation(context.getSource());
}
catch (IllegalArgumentException e)
{
facing = context.getSource().getRotation();
}
try
{
dim = DimensionArgumentType.getDimensionArgument(context, "dimension");
}
catch (IllegalArgumentException e)
{
dim = context.getSource().getWorld().dimension.getType();
}
ServerCommandSource source = context.getSource();
Vec3d pos = tryGetArg(
() -> Vec3ArgumentType.getVec3(context, "position"),
source::getPosition
);
Vec2f facing = tryGetArg(
() -> RotationArgumentType.getRotation(context, "direction").toAbsoluteRotation(context.getSource()),
source::getRotation
);
DimensionType dim = tryGetArg(
() -> DimensionArgumentType.getDimensionArgument(context, "dimension"),
() -> source.getWorld().dimension.getType()
);
GameMode mode = GameMode.CREATIVE;
try
{
ServerPlayerEntity player = context.getSource().getPlayer();
mode = player.interactionManager.getGameMode();
}
catch (CommandSyntaxException ignored)
catch (CommandSyntaxException ignored) {}
String playerName = StringArgumentType.getString(context, "player");
MinecraftServer server = source.getMinecraftServer();
PlayerEntity player = EntityPlayerMPFake.createFake(playerName, server, pos.x, pos.y, pos.z, facing.y, facing.x, dim, mode);
if (player == null)
{
}
PlayerEntity p = EntityPlayerMPFake.createFake(
getString(context,"player"),
context.getSource().getMinecraftServer(),
pos.x, pos.y, pos.z,
facing.y, facing.x,
dim,
mode);
if (p == null)
{
Messenger.m(context.getSource(), "rb Player "+getString(context,"player")+" doesn't exist " +
Messenger.m(context.getSource(), "rb Player " + StringArgumentType.getString(context, "player") + " doesn't exist " +
"and cannot spawn in online mode. Turn the server offline to spawn non-existing players");
}
return 1;
}
private static int stop(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
ServerPlayerEntity player = getPlayer(context);
((ServerPlayerEntityInterface) player).getActionPack().stop();
return 1;
}
private static int manipulate(CommandContext<ServerCommandSource> context, Consumer<EntityPlayerActionPack> action)
{
if (cantManipulate(context)) return 0;
ServerPlayerEntity player = getPlayer(context);
action.accept(((ServerPlayerEntityInterface) player).getActionPack());
return 1;
}
private static Command<ServerCommandSource> manipulation(Consumer<EntityPlayerActionPack> action)
{
return c -> manipulate(c, action);
}
private static int action(CommandContext<ServerCommandSource> context, EntityPlayerActionPack.ActionType type, EntityPlayerActionPack.Action action)
{
return manipulate(context, ap -> ap.start(type, action));
}
private static int shadow(CommandContext<ServerCommandSource> context)
{
ServerPlayerEntity player = getPlayer(context);
if (player instanceof EntityPlayerMPFake)
{
Messenger.m(context.getSource(), "r Cannot shadow fake players");
return 0;
}
EntityPlayerMPFake.createShadow(player.server, player);
return 1;
}
}
File diff suppressed because it is too large. Load diff
+79 -5
View File
@@ -1,16 +1,90 @@
package carpet.helpers;
import net.minecraft.entity.Entity;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RayTraceContext;
import net.minecraft.world.World;
import java.util.Optional;
import java.util.function.Predicate;
public class Tracer
{
public static HitResult traceEntityLook(Entity entity, double reach) {
Vec3d vec3d_1 = entity.getCameraPosVec(1.0F);
Vec3d vec3d_2 = entity.getRotationVec(1.0F);
Vec3d vec3d_3 = vec3d_1.add(vec3d_2.x * reach, vec3d_2.y * reach, vec3d_2.z * reach);
return entity.world.rayTrace(new RayTraceContext(vec3d_1, vec3d_3, RayTraceContext.ShapeType.OUTLINE, RayTraceContext.FluidHandling.ANY, entity));
public static HitResult rayTrace(Entity source, float partialTicks, double reach, boolean fluids)
{
BlockHitResult blockHit = rayTraceBlocks(source, partialTicks, reach, fluids);
double maxSqDist = reach * reach;
if (blockHit != null)
{
maxSqDist = blockHit.getPos().squaredDistanceTo(source.getCameraPosVec(partialTicks));
}
EntityHitResult entityHit = rayTraceEntities(source, partialTicks, reach, maxSqDist);
return entityHit == null ? blockHit : entityHit;
}
public static BlockHitResult rayTraceBlocks(Entity source, float partialTicks, double reach, boolean fluids)
{
Vec3d pos = source.getCameraPosVec(partialTicks);
Vec3d rotation = source.getRotationVec(partialTicks);
Vec3d reachEnd = pos.add(rotation.x * reach, rotation.y * reach, rotation.z * reach);
return source.world.rayTrace(new RayTraceContext(pos, reachEnd, RayTraceContext.ShapeType.OUTLINE, fluids ? RayTraceContext.FluidHandling.ANY : RayTraceContext.FluidHandling.NONE, source));
}
public static EntityHitResult rayTraceEntities(Entity source, float partialTicks, double reach, double maxSqDist)
{
Vec3d pos = source.getCameraPosVec(partialTicks);
Vec3d reachVec = source.getRotationVec(partialTicks).multiply(reach);
Box box = source.getBoundingBox().stretch(reachVec).expand(1);
return rayTraceEntities(source, pos, pos.add(reachVec), box, e -> !e.isSpectator() && e.collides(), maxSqDist);
}
public static EntityHitResult rayTraceEntities(Entity source, Vec3d start, Vec3d end, Box box, Predicate<Entity> predicate, double maxSqDistance)
{
World world = source.world;
double targetDistance = maxSqDistance;
Entity target = null;
Vec3d targetHitPos = null;
for (Entity current : world.getEntities(source, box, predicate))
{
Box currentBox = current.getBoundingBox().expand(current.getTargetingMargin());
Optional<Vec3d> currentHit = currentBox.rayTrace(start, end);
if (currentBox.contains(start))
{
if (targetDistance >= 0)
{
target = current;
targetHitPos = currentHit.orElse(start);
targetDistance = 0;
}
}
else if (currentHit.isPresent())
{
Vec3d currentHitPos = currentHit.get();
double currentDistance = start.squaredDistanceTo(currentHitPos);
if (currentDistance < targetDistance || targetDistance == 0)
{
if (current.getRootVehicle() == source.getRootVehicle())
{
if (targetDistance == 0)
{
target = current;
targetHitPos = currentHitPos;
}
}
else
{
target = current;
targetHitPos = currentHitPos;
targetDistance = currentDistance;
}
}
}
}
if (target == null) return null;
return new EntityHitResult(target, targetHitPos);
}
}
@@ -3,8 +3,12 @@ package carpet.mixins;
import carpet.settings.CarpetSettings;
import net.minecraft.client.network.packet.EntityPositionS2CPacket;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@@ -12,22 +16,27 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(Entity.class)
public abstract class Entity_horseFixMixin
@Mixin(PlayerEntity.class)
public abstract class PlayerEntity_horseFixMixin extends Entity
{
@Shadow private Entity vehicle;
@Shadow public abstract World getEntityWorld();
@Shadow public abstract ActionResult interact(Entity entity_1, Hand hand_1);
public PlayerEntity_horseFixMixin(EntityType<?> entityType_1, World world_1)
{
super(entityType_1, world_1);
}
@Inject(method = "stopRiding", at = @At("HEAD"))
private void resetNavigation(CallbackInfo ci)
{
if (vehicle != null && vehicle instanceof MobEntity && CarpetSettings.horseWanderingFix)
if (this.getVehicle() != null && CarpetSettings.horseWanderingFix)
{
((MobEntity) vehicle).getNavigation().stop();
if (getVehicle() instanceof MobEntity)
((MobEntity) getVehicle()).getNavigation().stop();
if (!getEntityWorld().isClient)
{
((ServerWorld) getEntityWorld()).method_14178().sendToNearbyPlayers((Entity)(Object)this, new EntityPositionS2CPacket(vehicle));
((ServerWorld) getEntityWorld()).method_14178().sendToNearbyPlayers((Entity)(Object)this, new EntityPositionS2CPacket(getVehicle()));
}
}
}
@@ -0,0 +1,30 @@
package carpet.mixins;
import carpet.CarpetServer;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.packet.CustomPayloadC2SPacket;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandler_coreMixin {
@Shadow
public ServerPlayerEntity player;
@Inject(method = "<init>", at = @At("RETURN"))
private void onPlayerConnect(MinecraftServer server, ClientConnection client, ServerPlayerEntity player, CallbackInfo ci) {
CarpetServer.onPlayerLoggedIn(player);
}
@Inject(method = "onDisconnected", at = @At("HEAD"))
private void onPlayerDisconnect(Text reason, CallbackInfo ci) {
CarpetServer.onPlayerLoggedOut(this.player);
}
}
@@ -12,7 +12,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin
public class ServerPlayNetworkHandler_tickMixin
{
@Shadow public ServerPlayerEntity player;
@@ -78,6 +78,8 @@ public class EntityPlayerMPFake extends ServerPlayerEntity
interactionManagerIn.setGameMode(player.interactionManager.getGameMode());
((ServerPlayerEntityInterface) playerShadow).getActionPack().copyFrom(((ServerPlayerEntityInterface) player).getActionPack());
playerShadow.stepHeight = 0.6F;
playerShadow.dataTracker.set(PLAYER_MODEL_BIT_MASK, player.getDataTracker().get(PLAYER_MODEL_BIT_MASK));
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(playerShadow, (byte) (player.headYaw * 256 / 360)), playerShadow.dimension);
server.getPlayerManager().sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, playerShadow));