Merge pull request #54 from gnembon/player_actions

player actions
This commit is contained in:
gnembon authored and GitHub committed 2019-07-17 01:00:28 -03:00
commit 19f910ba6b
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 carpet.settings.SettingsManager;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource; 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 * 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 * Runs once per loaded world once the World files / gamerules etc are fully loaded
* Can be loaded multiple times in SinglePlayer * 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 * 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 * Register your own commands right after vanilla commands are added
* If that matters for you * 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 * 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. * command, but separated to its own command as defined in SettingsManager.
*/ */
public SettingsManager customSettingsManager() {return null;} default SettingsManager customSettingsManager() {return null;}
/** /**
* todiddalidoo * todiddalidoo
*/ */
public void onPlayerLoggedIn(PlayerEntity player) {} default void onPlayerLoggedIn(ServerPlayerEntity player) {}
/** /**
* todiddalidoo * 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 com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer; 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 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)); extensions.forEach(e -> e.registerCommands(dispatcher));
//TestCommand.register(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; package carpet.commands;
import carpet.helpers.EntityPlayerActionPack;
import carpet.settings.CarpetSettings; import carpet.settings.CarpetSettings;
import carpet.fakes.ServerPlayerEntityInterface; import carpet.fakes.ServerPlayerEntityInterface;
import carpet.patches.EntityPlayerMPFake; import carpet.patches.EntityPlayerMPFake;
import carpet.utils.Messenger; import carpet.utils.Messenger;
import com.google.common.collect.Sets; 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.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.arguments.DimensionArgumentType; import net.minecraft.command.arguments.DimensionArgumentType;
import net.minecraft.command.arguments.RotationArgumentType; import net.minecraft.command.arguments.RotationArgumentType;
import net.minecraft.command.arguments.Vec3ArgumentType; import net.minecraft.command.arguments.Vec3ArgumentType;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Direction;
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.GameMode; import net.minecraft.world.GameMode;
@@ -24,147 +31,98 @@ import net.minecraft.world.dimension.DimensionType;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Set; 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.argument;
import static net.minecraft.server.command.CommandManager.literal; import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.server.command.CommandSource.suggestMatching; import static net.minecraft.server.command.CommandSource.suggestMatching;
public class PlayerCommand public class PlayerCommand
{ {
// TODO: allow any order like execute
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{ {
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("player"). LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("player")
requires((player) -> CarpetSettings.commandPlayer). .requires((player) -> CarpetSettings.commandPlayer)
then(argument("player", word()). .then(argument("player", StringArgumentType.word())
suggests( (c, b) -> suggestMatching(getPlayers(c.getSource()), b)). .suggests( (c, b) -> suggestMatching(getPlayers(c.getSource()), b))
then(literal("stop"). .then(literal("stop").executes(PlayerCommand::stop))
executes(PlayerCommand::stop)). .then(makeActionCommand("use", EntityPlayerActionPack.ActionType.USE))
then(literal("use"). .then(makeActionCommand("jump", EntityPlayerActionPack.ActionType.JUMP))
executes(PlayerCommand::useOnce). .then(makeActionCommand("attack", EntityPlayerActionPack.ActionType.ATTACK))
then(literal("once"). .then(makeActionCommand("drop", EntityPlayerActionPack.ActionType.DROP_ITEM))
executes( PlayerCommand::useOnce)). .then(makeActionCommand("dropStack", EntityPlayerActionPack.ActionType.DROP_STACK))
then(literal("continuous"). .then(makeActionCommand("swapHands", EntityPlayerActionPack.ActionType.SWAP_HANDS))
executes( PlayerCommand::useContinuous)). .then(literal("kill").executes(PlayerCommand::kill))
then(literal("interval"). .then(literal("shadow"). executes(PlayerCommand::shadow))
then(argument("ticks",integer(2)). .then(literal("mount").executes(manipulation(EntityPlayerActionPack::mount)))
executes( PlayerCommand::useInterval)))). .then(literal("dismount").executes(manipulation(EntityPlayerActionPack::dismount)))
then(literal("jump"). .then(literal("sneak").executes(manipulation(ap -> ap.setSneaking(true))))
executes( PlayerCommand::jumpOnce). .then(literal("unsneak").executes(manipulation(ap -> ap.setSneaking(false))))
then(literal("once"). .then(literal("sprint").executes(manipulation(ap -> ap.setSprinting(true))))
executes( PlayerCommand::jumpOnce)). .then(literal("unsprint").executes(manipulation(ap -> ap.setSprinting(false))))
then(literal("continuous"). .then(literal("look")
executes( PlayerCommand::jumpContinuous)). .then(literal("north").executes(manipulation(ap -> ap.look(Direction.NORTH))))
then(literal("interval"). .then(literal("south").executes(manipulation(ap -> ap.look(Direction.SOUTH))))
then(argument("ticks",integer(2)). .then(literal("east").executes(manipulation(ap -> ap.look(Direction.EAST))))
executes( PlayerCommand::jumpInterval )))). .then(literal("west").executes(manipulation(ap -> ap.look(Direction.WEST))))
then(literal("attack"). .then(literal("up").executes(manipulation(ap -> ap.look(Direction.UP))))
executes(PlayerCommand::attackOnce). .then(literal("down").executes(manipulation(ap -> ap.look(Direction.DOWN))))
then(literal("once"). .then(argument("direction", RotationArgumentType.rotation())
executes(PlayerCommand::attackOnce)). .executes(c -> manipulate(c, ap -> ap.look(RotationArgumentType.getRotation(c, "direction").toAbsoluteRotation(c.getSource())))))
then(literal("continuous"). ).then(literal("turn")
executes(PlayerCommand::attackContinuous)). .then(literal("left").executes(c -> manipulate(c, ap -> ap.turn(-90, 0))))
then(literal("interval"). .then(literal("right").executes(c -> manipulate(c, ap -> ap.turn(90, 0))))
then(argument("ticks",integer(2)). .then(literal("back").executes(c -> manipulate(c, ap -> ap.turn(180, 0))))
executes(PlayerCommand::attackInterval)))). .then(argument("rotation", RotationArgumentType.rotation())
then(literal("drop"). .executes(c -> manipulate(c, ap -> ap.turn(RotationArgumentType.getRotation(c, "rotation").toAbsoluteRotation(c.getSource())))))
executes(PlayerCommand::dropItem)). ).then(literal("move")
then(literal("swapHands"). .then(literal("forward").executes(c -> manipulate(c, ap -> ap.setForward(1))))
executes(PlayerCommand::swapHands)). .then(literal("backward").executes(c -> manipulate(c, ap -> ap.setForward(-1))))
then(literal("kill"). .then(literal("left").executes(c -> manipulate(c, ap -> ap.setStrafing(1))))
executes(PlayerCommand::kill)). .then(literal("right").executes(c -> manipulate(c, ap -> ap.setStrafing(-1))))
then(literal("shadow"). ).then(literal("spawn").executes(PlayerCommand::spawn)
executes(PlayerCommand::shadow)). .then(literal("at").then(argument("position", Vec3ArgumentType.vec3()).executes(PlayerCommand::spawn)
then(literal("mount"). .then(literal("facing").then(argument("direction", RotationArgumentType.rotation()).executes(PlayerCommand::spawn)
executes(PlayerCommand::mount)). .then(literal("in").then(argument("dimension", DimensionArgumentType.dimension()).executes(PlayerCommand::spawn)))
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)))))))));
dispatcher.register(literalargumentbuilder); 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) 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()); players.addAll(source.getPlayerNames());
return players; return players;
} }
private static ServerPlayerEntity getPlayer(CommandContext<ServerCommandSource> context) private static ServerPlayerEntity getPlayer(CommandContext<ServerCommandSource> context)
{ {
String playerName = getString(context,"player"); String playerName = StringArgumentType.getString(context, "player");
MinecraftServer server = context.getSource().getMinecraftServer(); MinecraftServer server = context.getSource().getMinecraftServer();
return server.getPlayerManager().getPlayer(playerName); 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) private static boolean cantManipulate(CommandContext<ServerCommandSource> context)
{ {
PlayerEntity player = getPlayer(context); PlayerEntity player = getPlayer(context);
if (player == null) 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; return true;
} }
PlayerEntity sendingPlayer; PlayerEntity sendingPlayer;
@@ -177,301 +135,145 @@ public class PlayerCommand
return false; 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 true;
} }
} }
return false; 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) private static boolean cantReMove(CommandContext<ServerCommandSource> context)
{ {
if (cantManipulate(context)) if (cantManipulate(context)) return true;
return true;
PlayerEntity player = getPlayer(context); 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 true;
} }
return false; 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) private static int kill(CommandContext<ServerCommandSource> context)
{ {
if (cantReMove(context)) return 0; if (cantReMove(context)) return 0;
getPlayer(context).kill(); getPlayer(context).kill();
return 1; return 1;
} }
private static int shadow(CommandContext<ServerCommandSource> context)
@FunctionalInterface
interface SupplierWithCommandSyntaxException<T>
{ {
if (cantShadow(context)) return 0; T get() throws CommandSyntaxException;
EntityPlayerMPFake.createShadow(
context.getSource().getMinecraftServer(),
getPlayer(context));
return 1;
} }
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; try
((ServerPlayerEntityInterface)getPlayer(context)).getActionPack().mount(); {
return 1; return a.get();
} }
private static int dismount(CommandContext<ServerCommandSource> context) catch (IllegalArgumentException e)
{ {
if (cantManipulate(context)) return 0; return b.get();
((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;
} }
private static int spawn(CommandContext<ServerCommandSource> context) throws CommandSyntaxException private static int spawn(CommandContext<ServerCommandSource> context) throws CommandSyntaxException
{ {
if (cantSpawn(context)) return 0; if (cantSpawn(context)) return 0;
Vec3d pos; ServerCommandSource source = context.getSource();
Vec2f facing; Vec3d pos = tryGetArg(
DimensionType dim; () -> Vec3ArgumentType.getVec3(context, "position"),
try source::getPosition
{ );
pos = Vec3ArgumentType.getVec3(context, "position"); Vec2f facing = tryGetArg(
} () -> RotationArgumentType.getRotation(context, "direction").toAbsoluteRotation(context.getSource()),
catch (IllegalArgumentException e) source::getRotation
{ );
pos = context.getSource().getPosition(); DimensionType dim = tryGetArg(
} () -> DimensionArgumentType.getDimensionArgument(context, "dimension"),
try () -> source.getWorld().dimension.getType()
{ );
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();
}
GameMode mode = GameMode.CREATIVE; GameMode mode = GameMode.CREATIVE;
try try
{ {
ServerPlayerEntity player = context.getSource().getPlayer(); ServerPlayerEntity player = context.getSource().getPlayer();
mode = player.interactionManager.getGameMode(); 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)
{ {
} Messenger.m(context.getSource(), "rb Player " + StringArgumentType.getString(context, "player") + " doesn't exist " +
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 " +
"and cannot spawn in online mode. Turn the server offline to spawn non-existing players"); "and cannot spawn in online mode. Turn the server offline to spawn non-existing players");
} }
return 1; 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; package carpet.helpers;
import net.minecraft.entity.Entity; 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.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RayTraceContext; import net.minecraft.world.RayTraceContext;
import net.minecraft.world.World;
import java.util.Optional;
import java.util.function.Predicate;
public class Tracer public class Tracer
{ {
public static HitResult traceEntityLook(Entity entity, double reach) { public static HitResult rayTrace(Entity source, float partialTicks, double reach, boolean fluids)
Vec3d vec3d_1 = entity.getCameraPosVec(1.0F); {
Vec3d vec3d_2 = entity.getRotationVec(1.0F); BlockHitResult blockHit = rayTraceBlocks(source, partialTicks, reach, fluids);
Vec3d vec3d_3 = vec3d_1.add(vec3d_2.x * reach, vec3d_2.y * reach, vec3d_2.z * reach); double maxSqDist = reach * reach;
return entity.world.rayTrace(new RayTraceContext(vec3d_1, vec3d_3, RayTraceContext.ShapeType.OUTLINE, RayTraceContext.FluidHandling.ANY, entity)); 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 carpet.settings.CarpetSettings;
import net.minecraft.client.network.packet.EntityPositionS2CPacket; import net.minecraft.client.network.packet.EntityPositionS2CPacket;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; 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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(Entity.class) @Mixin(PlayerEntity.class)
public abstract class Entity_horseFixMixin 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")) @Inject(method = "stopRiding", at = @At("HEAD"))
private void resetNavigation(CallbackInfo ci) 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) 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; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ServerPlayNetworkHandler.class) @Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin public class ServerPlayNetworkHandler_tickMixin
{ {
@Shadow public ServerPlayerEntity player; @Shadow public ServerPlayerEntity player;
@@ -78,6 +78,8 @@ public class EntityPlayerMPFake extends ServerPlayerEntity
interactionManagerIn.setGameMode(player.interactionManager.getGameMode()); interactionManagerIn.setGameMode(player.interactionManager.getGameMode());
((ServerPlayerEntityInterface) playerShadow).getActionPack().copyFrom(((ServerPlayerEntityInterface) player).getActionPack()); ((ServerPlayerEntityInterface) playerShadow).getActionPack().copyFrom(((ServerPlayerEntityInterface) player).getActionPack());
playerShadow.stepHeight = 0.6F; 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().sendToDimension(new EntitySetHeadYawS2CPacket(playerShadow, (byte) (player.headYaw * 256 / 360)), playerShadow.dimension);
server.getPlayerManager().sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, playerShadow)); server.getPlayerManager().sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, playerShadow));
+4 -2
View File
@@ -29,7 +29,9 @@
"ServerWorld_fakePlayersMixin", "ServerWorld_fakePlayersMixin",
"ServerPlayNetworkHandlerMixin", "ServerPlayNetworkHandler_coreMixin",
"ServerPlayNetworkHandler_tickMixin",
"MinecraftServer_scarpetMixin", "MinecraftServer_scarpetMixin",
"PlayerListHeaderS2CPacketMixin", "PlayerListHeaderS2CPacketMixin",
"WeightedPickerEntryMixin", "WeightedPickerEntryMixin",
@@ -81,7 +83,7 @@
"Entity_portalSuffocationMixin", "Entity_portalSuffocationMixin",
"PortalForcer_portalSuffocationMixin", "PortalForcer_portalSuffocationMixin",
"Entity_horseFixMixin", "PlayerEntity_horseFixMixin",
"HopperBlockEntityMixin", "HopperBlockEntityMixin",
"ContainerMixin", "ContainerMixin",