Skip to content

Commit

Permalink
cameramode and info commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gnembon committed Oct 1, 2018
1 parent da7b5f4 commit 83d7492
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 50 deletions.
13 changes: 12 additions & 1 deletion patches/net/minecraft/server/management/PlayerList.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@
nethandlerplayserver.sendPacket(new SPacketJoinGame(playerIn.getEntityId(), playerIn.interactionManager.getGameType(), worldinfo.isHardcore(), worldserver.dimension.getType(), worldserver.getDifficulty(), this.getMaxPlayers(), worldinfo.getTerrainType(), worldserver.getGameRules().getBoolean("reducedDebugInfo")));
nethandlerplayserver.sendPacket(new SPacketCustomPayload(SPacketCustomPayload.BRAND, (new PacketBuffer(Unpooled.buffer())).writeString(this.getServer().getServerModName())));
nethandlerplayserver.sendPacket(new SPacketServerDifficulty(worldinfo.getDifficulty(), worldinfo.isDifficultyLocked()));
@@ -460,6 +464,12 @@
@@ -312,6 +316,10 @@
{
nbttagcompound1 = this.playerDataManager.readPlayerData(playerIn);
}
+ if (playerIn instanceof EntityPlayerMPFake)
+ {
+ ((EntityPlayerMPFake) playerIn).fixStartingPosition.run();
+ }

return nbttagcompound1;
}
@@ -460,6 +468,12 @@

for (EntityPlayerMP entityplayermp1 : list)
{
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/carpet/CarpetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public static void registerCarpetCommands(CommandDispatcher<CommandSource> dispa
LogCommand.register(dispatcher);
SpawnCommand.register(dispatcher);
PlayerCommand.register(dispatcher);
CameraModeCommand.register(dispatcher);
InfoCommand.register(dispatcher);

TestCommand.register(dispatcher);
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/carpet/CarpetSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,16 @@ private static void set_defaults()
rule("commandLog", "commands", "Enables /log command to monitor events in the game via chat and overlays").defaultTrue(),
//rule("commandDistance", "commands", "Enables /distance command to measure in game distance between points").defaultTrue()
// .extraInfo("Also enables brown carpet placement action if 'carpets' rule is turned on as well"),
rule("commandInfo", "commands", "Enables /info command for blocks and entities").defaultTrue()
.extraInfo("Also enables gray carpet placement action if 'carpets' rule is turned on as well"),

//rule("commandBlockInfo", "commands", "Enables /blockinfo command").defaultTrue()
// .extraInfo("Also enables gray carpet placement action if 'carpets' rule is turned on as well"),
//rule("commandEntityInfo", "commands", "Enables /entityinfo command").defaultTrue()
// .extraInfo("Also enables yellow carpet placement action if 'carpets' rule is turned on as well"),
//rule("commandUnload", "commands", "Enables /unload command to control game speed").defaultTrue(),
//rule("commandCameramode", "commands", "Enables /c and /s commands to quickly switch between camera and survival modes").defaultTrue()
// .extraInfo("/c and /s commands are available to all players regardless of their permission levels"),
rule("commandCameramode", "commands", "Enables /c and /s commands to quickly switch between camera and survival modes").defaultTrue()
.extraInfo("/c and /s commands are available to all players regardless of their permission levels"),
//rule("commandPerimeterInfo", "commands", "Enables /perimeterinfo command that scans the area around the block for potential spawnable spots").defaultTrue(),
rule("commandPlayer", "commands", "Enables /player command to control/spawn players").defaultTrue(),
//rule("commandRNG", "commands", "Enables /rng command to manipulate and query rng").defaultTrue(),
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/carpet/commands/CameraModeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package carpet.commands;

import carpet.CarpetSettings;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandSource;
import net.minecraft.command.ISuggestionProvider;
import net.minecraft.command.arguments.EntityArgument;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.potion.PotionEffect;
import net.minecraft.world.GameType;

import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.command.Commands.argument;
import static net.minecraft.command.Commands.literal;

public class CameraModeCommand
{
public static void register(CommandDispatcher<CommandSource> dispatcher)
{
LiteralArgumentBuilder<CommandSource> camera = literal("c").
requires((player) -> CarpetSettings.getBool("commandCameramode")).
executes((c) -> cameraMode(c.getSource(), c.getSource().asPlayer())).
then(argument("player", EntityArgument.singlePlayer()).
executes( (c) -> cameraMode(c.getSource(), EntityArgument.getOnePlayer(c, "player"))));

LiteralArgumentBuilder<CommandSource> survival = literal("s").
requires((player) -> CarpetSettings.getBool("commandCameramode")).
executes((c) -> survivalMode(
c.getSource(),
c.getSource().asPlayer())).
then(argument("player", EntityArgument.singlePlayer()).
executes( (c) -> survivalMode(c.getSource(), EntityArgument.getOnePlayer(c, "player"))));

dispatcher.register(camera);
dispatcher.register(survival);
}
private static boolean iCanHasPermissions(CommandSource source, EntityPlayer player)
{
try
{
return source.hasPermissionLevel(2) || source.asPlayer() == player;
}
catch (CommandSyntaxException e)
{
return true; // shoudn't happen because server has all permissions anyways
}
}
private static int cameraMode(CommandSource source, EntityPlayer player)
{
if (!(iCanHasPermissions(source, player))) return 0;
player.setGameType(GameType.SPECTATOR);
player.addPotionEffect(new PotionEffect(MobEffects.NIGHT_VISION, 999999, 0, false, false));
player.addPotionEffect(new PotionEffect(MobEffects.CONDUIT_POWER, 999999, 0, false, false));
return 1;
}
private static int survivalMode(CommandSource source, EntityPlayer player)
{
if (!(iCanHasPermissions(source, player))) return 0;
player.setGameType(GameType.SURVIVAL);
player.removePotionEffect(MobEffects.NIGHT_VISION);
player.removePotionEffect(MobEffects.CONDUIT_POWER);
return 1;
}

}
132 changes: 132 additions & 0 deletions src/main/java/carpet/commands/InfoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package carpet.commands;

import carpet.CarpetSettings;
import carpet.utils.EntityInfo;
import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.command.ISuggestionProvider;
import net.minecraft.command.arguments.BlockPosArgument;
import net.minecraft.command.arguments.EntityArgument;
import net.minecraft.command.arguments.EntitySelector;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.command.Commands.argument;
import static net.minecraft.command.Commands.literal;

public class InfoCommand
{
public static void register(CommandDispatcher<CommandSource> dispatcher)
{
LiteralArgumentBuilder<CommandSource> command = literal("info").
requires((player) -> CarpetSettings.getBool("commandCameramode")).
then(literal("block").
then(argument("block position", BlockPosArgument.blockPos()).
executes( (c) -> infoBlock(
c.getSource(),
BlockPosArgument.getBlockPos(c, "block position"), null)).
then(literal("grep").
then(argument("regexp",greedyString()).
executes( (c) -> infoBlock(
c.getSource(),
BlockPosArgument.getBlockPos(c, "block position"),
getString(c, "regexp"))))))).
then(literal("entity").
then(argument("entity selector", EntityArgument.multipleEntities()).
executes( (c) -> infoEntities(
c.getSource(), EntityArgument.getEntities(c,"entity selector"), null)).
then(literal("grep").
then(argument("regexp",greedyString()).
executes( (c) -> infoEntities(
c.getSource(),
EntityArgument.getEntities(c,"entity selector"),
getString(c, "regexp")))))));

dispatcher.register(command);
}

public static void printEntity(List<ITextComponent> messages, CommandSource source, String grep)
{
List<ITextComponent> actual = new ArrayList<>();
if (grep != null)
{
Pattern p = Pattern.compile(grep);
actual.add(messages.get(0));
boolean empty = true;
for (int i = 1; i<messages.size(); i++)
{
ITextComponent line = messages.get(i);
Matcher m = p.matcher(line.getString());
if (m.find())
{
empty = false;
actual.add(line);
}
}
if (empty)
{
return;
}
}
else
{
actual = messages;
}
Messenger.m(source, "");
Messenger.send(source, actual);
}

public static void printBlock(List<ITextComponent> messages, CommandSource source, String grep)
{
Messenger.m(source, "");
if (grep != null)
{
Pattern p = Pattern.compile(grep);
Messenger.m(source, messages.get(0));
for (int i = 1; i<messages.size(); i++)
{
ITextComponent line = messages.get(i);
Matcher m = p.matcher(line.getString());
if (m.find())
{
Messenger.m(source, line);
}
}
}
else
{
Messenger.send(source, messages);
}
}



private static int infoEntities(CommandSource source, Collection<? extends Entity> entities, String grep)
{
for (Entity e: entities)
{
List<ITextComponent> report = EntityInfo.entityInfo(e, source.getWorld());
printEntity(report, source, grep);
}
return 1;
}
private static int infoBlock(CommandSource source, BlockPos pos, String grep)
{
Messenger.m(source, "r Not done yet: check.... tomorrow");
return 1;
}

}
8 changes: 7 additions & 1 deletion src/main/java/carpet/patches/EntityPlayerMPFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.mojang.authlib.GameProfile;
import net.minecraft.tileentity.TileEntitySkull;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.WorldServer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand All @@ -16,8 +18,12 @@
import net.minecraft.world.GameType;
import net.minecraft.world.dimension.DimensionType;

import java.util.function.BiConsumer;

public class EntityPlayerMPFake extends EntityPlayerMP
{
public Runnable fixStartingPosition = () -> {};

public static EntityPlayerMPFake createFake(String username, MinecraftServer server, double d0, double d1, double d2, double yaw, double pitch, DimensionType dimension, GameType gamemode)
{
//prolly half of that crap is not necessary, but it works
Expand All @@ -29,7 +35,7 @@ public static EntityPlayerMPFake createFake(String username, MinecraftServer ser
gameprofile = TileEntitySkull.updateGameProfile(gameprofile);
}
EntityPlayerMPFake instance = new EntityPlayerMPFake(server, worldIn, gameprofile, interactionManagerIn);
instance.setLocationAndAngles(d0, d1, d2, (float)yaw, (float)pitch);
instance.fixStartingPosition = () -> instance.setLocationAndAngles(d0, d1, d2, (float)yaw, (float)pitch);
server.getPlayerList().initializeConnectionToPlayer(new NetworkManagerFake(EnumPacketDirection.CLIENTBOUND), instance);
if (instance.dimension != dimension) //player was logged in in a different dimension
{
Expand Down
Loading

0 comments on commit 83d7492

Please sign in to comment.