generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation for /spin
- Loading branch information
Showing
6 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/net/fill1890/fabsit/command/SpinCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package net.fill1890.fabsit.command; | ||
|
||
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.entity.Pose; | ||
import net.fill1890.fabsit.entity.PoseManagerEntity; | ||
import net.minecraft.block.BlockState; | ||
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 net.minecraft.util.math.BlockPos; | ||
|
||
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 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; | ||
} | ||
|
||
// 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; | ||
} | ||
|
||
// get the block below to check it isn't air | ||
BlockState standingBlock = player.getEntityWorld().getBlockState(new BlockPos(player.getPos()).down()); | ||
// check cancel conditions | ||
if( | ||
player.isFallFlying() | ||
|| player.isSleeping() | ||
|| player.isSwimming() | ||
|| player.isSpectator() | ||
|| standingBlock.isAir() | ||
) { 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.SPINNING, player); | ||
player.getEntityWorld().spawnEntity(chair); | ||
player.startRiding(chair, true); | ||
|
||
return 1; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
public enum Pose { | ||
SITTING, | ||
LAYING; | ||
LAYING, | ||
SPINNING; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/net/fill1890/fabsit/entity/SpinningEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.fill1890.fabsit.entity; | ||
|
||
import com.mojang.authlib.GameProfile; | ||
import net.minecraft.network.packet.s2c.play.EntityS2CPacket; | ||
import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
|
||
import static net.fill1890.fabsit.mixin.LivingEntityAccessor.getLIVING_FLAGS; | ||
|
||
public class SpinningEntity extends PosingEntity { | ||
// pivot the poser to face vertically | ||
EntityS2CPacket pivotPacket; | ||
|
||
public SpinningEntity(ServerPlayerEntity player, GameProfile gameProfile) { | ||
super(player, gameProfile); | ||
|
||
// set spinning state | ||
this.getDataTracker().set(getLIVING_FLAGS(), (byte) 0x04); | ||
// refresh data packet | ||
this.trackerPoserPacket = new EntityTrackerUpdateS2CPacket(this.getId(), this.getDataTracker(), true); | ||
|
||
this.pivotPacket = new EntityS2CPacket.RotateAndMoveRelative(this.getId(), (short) 0, (short) 0, (short) 0, (byte) 0, (byte) (-90.0f * 256.0f / 360.0f), true); | ||
} | ||
|
||
@Override | ||
public void sendUpdates() { | ||
super.sendUpdates(); | ||
|
||
// rotate the poser to be spinning vertically | ||
this.addingPlayers.forEach(p -> p.networkHandler.sendPacket(this.pivotPacket)); | ||
} | ||
|
||
@Override | ||
protected void syncHeadYaw() { | ||
// do nothing; no point since already rotating | ||
// also might mess up angle | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters