Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
- Generic run function for sit-based poses
- Class abstraction to prevent instantiation
  • Loading branch information
fill1890 committed Jun 23, 2022
1 parent 97bb442 commit 3929e67
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 191 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/fill1890/fabsit/FabSitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.network.PacketByteBuf;

public class FabSitClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(FabSit.fabsitChannel, FabSitClient::checkPacketReply);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/fill1890/fabsit/FabSitServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.server.network.ServerPlayerEntity;

public class FabSitServer implements DedicatedServerModInitializer {

@Override
public void onInitializeServer() {
ServerPlayNetworking.registerGlobalReceiver(FabSit.fabsitChannel, FabSitServer::handleCheckResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.fill1890.fabsit.command;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fill1890.fabsit.config.ConfigManager;
import net.fill1890.fabsit.entity.Pose;
import net.fill1890.fabsit.entity.PoseManagerEntity;
import net.fill1890.fabsit.error.PoseException;
import net.fill1890.fabsit.util.Messages;
import net.fill1890.fabsit.util.PoseTest;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

/**
* Generic sit-based command class
* <br>
* Implementation details taken from <a href="https://github.com/Gecolay/GSit">GSit</a>
*/
public abstract class GenericSitBasedCommand {
public static int run(CommandContext<ServerCommandSource> context, Pose pose) {
final ServerCommandSource source = context.getSource();
ServerPlayerEntity player;

try {
player = source.getPlayerOrThrow();
} catch (CommandSyntaxException e) {
source.sendError(Text.of("You must be a player to run this command!"));
return -1;
}

// check the pose is config-enabled
try {
PoseTest.confirmEnabled(pose);
} catch(PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, pose, e);
return -1;
}


// toggle sitting if the player was sat down
if(player.hasVehicle()) {
player.dismountVehicle();
// TODO: should be able to add to manager?
player.teleport(player.getX(), player.getY() + 0.6, player.getZ());
return 1;
}

// confirm player can pose right now
try {
PoseTest.confirmPosable(player);
} catch (PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, pose, e);
return -1;
}

// create a new pose manager for laying and sit the player down
// (player is then invisible and an npc lays down)
PoseManagerEntity chair = new PoseManagerEntity(player.getPos(), pose, player);
player.getEntityWorld().spawnEntity(chair);
player.startRiding(chair, true);

return 1;
}
}
69 changes: 5 additions & 64 deletions src/main/java/net/fill1890/fabsit/command/LayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,25 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fill1890.fabsit.config.ConfigManager;
import net.fill1890.fabsit.entity.Pose;
import net.fill1890.fabsit.entity.PoseManagerEntity;
import net.fill1890.fabsit.error.PoseException;
import net.fill1890.fabsit.util.Messages;
import net.fill1890.fabsit.util.PoseTest;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import static net.minecraft.server.command.CommandManager.literal;

/**
* /lay command implementation
* <br>
* Requires <code>fabsit.commands.lay</code> permission node, granted to all players by default
* <br>
* Implementation details taken from <a href="https://github.com/Gecolay/GSit">GSit</a>
*/
// TODO: implement generic pose class?
public class LayCommand {
protected static final Pose POSE = Pose.LAYING;
public abstract class LayCommand {
private static final Pose POSE = Pose.LAYING;

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
dispatcher.register(literal("lay")
.requires(Permissions.require("fabsit.commands.lay", true))
.executes(LayCommand::run));
}

public static int run(CommandContext<ServerCommandSource> context) {
final ServerCommandSource source = context.getSource();
ServerPlayerEntity player;

try {
player = source.getPlayerOrThrow();
} catch (CommandSyntaxException e) {
source.sendError(Text.of("You must be a player to run this command!"));
return -1;
}

// check the pose is config-enabled
try {
PoseTest.confirmEnabled(POSE);
} catch(PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, POSE, e);
return -1;
}


// toggle sitting if the player was sat down
if(player.hasVehicle()) {
player.dismountVehicle();
// TODO: should be able to add to manager?
player.teleport(player.getX(), player.getY() + 0.6, player.getZ());
return 1;
}

// confirm player can pose right now
try {
PoseTest.confirmPosable(player);
} catch (PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, POSE, e);
return -1;
}

// create a new pose manager for laying and sit the player down
// (player is then invisible and an npc lays down)
PoseManagerEntity chair = new PoseManagerEntity(player.getEntityWorld(), player.getPos(), POSE, player);
player.getEntityWorld().spawnEntity(chair);
player.startRiding(chair, true);

return 1;

dispatcher.register(literal("lay")
.requires(Permissions.require("fabsit.commands.lay", true))
.executes((CommandContext<ServerCommandSource> context) -> GenericSitBasedCommand.run(context, POSE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import static net.minecraft.server.command.CommandManager.literal;

public class ReloadConfigCommand {
public abstract class ReloadConfigCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
dispatcher.register(literal("fabsit")
.then(literal("reload")
Expand Down
69 changes: 7 additions & 62 deletions src/main/java/net/fill1890/fabsit/command/SitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,25 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fill1890.fabsit.config.ConfigManager;
import net.fill1890.fabsit.entity.Pose;
import net.fill1890.fabsit.entity.PoseManagerEntity;
import net.fill1890.fabsit.error.PoseException;
import net.fill1890.fabsit.util.Messages;
import net.fill1890.fabsit.util.PoseTest;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import static net.minecraft.server.command.CommandManager.literal;

/**
* /sit command implementation
* <br>
* Requires <code>fabsit.commands.sit</code> permission node; granted to all players by default
* <br>
* Inspiration taken from <a href="https://github.com/BradBot1/FabricSit">Fabric Sit</a>
*/
public class SitCommand {
public abstract class SitCommand {
protected static final Pose POSE = Pose.SITTING;

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
dispatcher.register(
CommandManager.literal("sit")
.requires(Permissions.require("fabsit.commands.sit", true))
.executes(SitCommand::run));
}

public static int run(CommandContext<ServerCommandSource> context) {
final ServerCommandSource source = context.getSource();
ServerPlayerEntity player;

try {
player = source.getPlayerOrThrow();
} catch (CommandSyntaxException e) {
source.sendError(Text.of("You must be a player to run this command!"));
return -1;
}

// check the pose is config-enabled
try {
PoseTest.confirmEnabled(POSE);
} catch (PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, POSE, e);
return -1;
}

// toggle sitting if already sat down
if(player.hasVehicle()) {
player.dismountVehicle();
// tp bump up so the player doesn't end up inside the block
player.teleport(player.getX(), player.getY() + 0.5, player.getZ());
return 1;
}

// confirm player can pose right now
try {
PoseTest.confirmPosable(player);
} catch(PoseException e) {
System.out.println("messages: " + ConfigManager.getConfig().enable_messages.pose_errors);
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, POSE, e);
return -1;
}

// Create a new pose manager for sitting and sit the player down
PoseManagerEntity chair = new PoseManagerEntity(player.getEntityWorld(), player.getPos(), POSE, player);
player.getEntityWorld().spawnEntity(chair);
player.startRiding(chair, true);

return 1;
dispatcher.register(literal("sit")
.requires(Permissions.require("fabsit.commands.sit", true))
.executes((CommandContext<ServerCommandSource> context) -> GenericSitBasedCommand.run(context, POSE)));
}
}
}
66 changes: 5 additions & 61 deletions src/main/java/net/fill1890/fabsit/command/SpinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,25 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fill1890.fabsit.config.ConfigManager;
import net.fill1890.fabsit.entity.Pose;
import net.fill1890.fabsit.entity.PoseManagerEntity;
import net.fill1890.fabsit.error.PoseException;
import net.fill1890.fabsit.util.Messages;
import net.fill1890.fabsit.util.PoseTest;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import static net.minecraft.server.command.CommandManager.literal;

/**
* /spin command implementation
* <br>
* Requires <code>fabsit.commands.spin</code> permission node, granted to all players by default
* <br>
* Implementation details taken from <a href="https://github.com/Gecolay/GSit">GSit</a>
*/
public class SpinCommand {
public abstract class SpinCommand {
protected static final Pose POSE = Pose.SPINNING;

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
dispatcher.register(literal("spin")
.requires(Permissions.require("fabsit.commands.spin", true))
.executes(SpinCommand::run));
}

public static int run(CommandContext<ServerCommandSource> context) {
final ServerCommandSource source = context.getSource();
ServerPlayerEntity player;

try {
player = source.getPlayerOrThrow();
} catch (CommandSyntaxException e) {
source.sendError(Text.of("You must be a player to run this command!"));
return -1;
}

// check the pose is config-enabled
try {
PoseTest.confirmEnabled(POSE);
} catch(PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, POSE, e);
return -1;
}

// toggle sitting if the player was sat down
if(player.hasVehicle()) {
player.dismountVehicle();
player.teleport(player.getX(), player.getY() + 0.6, player.getZ());
return 1;
}

// confirm player can pose right now
try {
PoseTest.confirmPosable(player);
} catch(PoseException e) {
if(ConfigManager.getConfig().enable_messages.pose_errors)
Messages.sendByException(player, POSE, e);
return -1;
}

// create a new pose manager for spinning and sit the player down
// (player is then invisible and an npc spins)
PoseManagerEntity chair = new PoseManagerEntity(player.getEntityWorld(), player.getPos(), POSE, player);
player.getEntityWorld().spawnEntity(chair);
player.startRiding(chair, true);

return 1;

dispatcher.register(literal("spin")
.requires(Permissions.require("fabsit.commands.spin", true))
.executes((CommandContext<ServerCommandSource> context) -> GenericSitBasedCommand.run(context, POSE)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Config manager
*/
public class ConfigManager {
public abstract class ConfigManager {
// GSON config
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().setLenient().create();
// config data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public class PoseManagerEntity extends ArmorStandEntity {

protected boolean killing;

public PoseManagerEntity(World world, Vec3d pos, Pose pose, ServerPlayerEntity player) {
public PoseManagerEntity(Vec3d pos, Pose pose, ServerPlayerEntity player) {
// create a new armour stand at the appropriate height
// TODO: no magic numbers
super(world, pos.x, pos.y - 1.6, pos.z);
super(player.getWorld(), pos.x, pos.y - 1.6, pos.z);

this.setInvisible(true);
this.setInvulnerable(true);
Expand Down

0 comments on commit 3929e67

Please sign in to comment.