Skip to content

Commit 3f86faf

Browse files
committed
improved ascend and descend
1 parent fb1c496 commit 3f86faf

7 files changed

Lines changed: 134 additions & 136 deletions

File tree

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ loom_version = 1.13.0-bta
1919
java_version = 8
2020
##########################################################################
2121
# Mod Properties
22-
mod_version = 1.1.4+7.3_04
22+
mod_version = 1.1.5+7.3_04
2323
mod_group = redart15
24-
mod_name = Commandly
24+
mod_name = commandly
2525
##########################################################################
2626
# Mod Dependencies
2727
# Check this on https://github.com/Turnip-Labs/ModMenu/releases/latest/

src/main/java/redart15/commandly/command/CommandAscend.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main/java/redart15/commandly/command/CommandDescend.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main/java/redart15/commandly/command/CommandlyCommands.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import net.fabricmc.api.EnvType;
44
import net.fabricmc.api.Environment;
55
import net.minecraft.core.net.command.CommandManager;
6+
import redart15.commandly.command.cend.CommandAscend;
7+
import redart15.commandly.command.cend.CommandDescend;
68

79
public class CommandlyCommands {
810

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package redart15.commandly.command.cend;
2+
3+
import com.mojang.brigadier.context.CommandContext;
4+
import net.minecraft.core.block.material.MaterialDecoration;
5+
import net.minecraft.core.entity.player.Player;
6+
import net.minecraft.core.net.command.CommandSource;
7+
import net.minecraft.core.world.World;
8+
import net.minecraft.core.world.chunk.ChunkCoordinates;
9+
10+
import java.util.function.BiFunction;
11+
12+
public class CendUtil {
13+
private CendUtil() {}
14+
15+
public static ChunkCoordinates canPlacePlayer(World world, double dx, double dy, double dz) {
16+
int y = (int) Math.ceil(dy);
17+
int minX = (int) Math.floor(dx);
18+
int maxX = (int) Math.ceil(dx);
19+
int minZ = (int) Math.floor(dz);
20+
int maxZ = (int) Math.ceil(dz);
21+
22+
for (; minX < maxX; minX++) {
23+
for (; minZ < maxZ; minZ++) {
24+
if (world.isAirBlock(minX, y, minZ) && world.isAirBlock(minX, y - 1, minZ) && canTeleportTo(world, minX, y, minZ)) {
25+
return new ChunkCoordinates(minX, y, minZ);
26+
}
27+
}
28+
}
29+
return null;
30+
}
31+
32+
private static boolean canTeleportTo(World world, int x, int y, int z) {
33+
if (world.getBlockMaterial(x, y - 2, z).isSolid()) {
34+
return true;
35+
}
36+
return world.getBlockMaterial(x, y - 2, z) instanceof MaterialDecoration && world.getBlockMaterial(x, y - 3, z).isSolid();
37+
}
38+
39+
protected static int cend(CommandContext<Object> context, BiFunction<CommandSource, Player, Integer> cend) {
40+
CommandSource source = (CommandSource) context.getSource();
41+
Player player;
42+
try {
43+
player = context.getArgument("player", Player.class);
44+
} catch (IllegalArgumentException e) {
45+
player = source.getSender();
46+
}
47+
return cend.apply(source, player);
48+
}
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package redart15.commandly.command.cend;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.builder.ArgumentBuilderLiteral;
5+
import com.mojang.brigadier.builder.ArgumentBuilderRequired;
6+
import net.minecraft.core.entity.player.Player;
7+
import net.minecraft.core.net.command.CommandManager;
8+
import net.minecraft.core.net.command.CommandSource;
9+
import net.minecraft.core.net.command.arguments.ArgumentTypeEntity;
10+
import net.minecraft.core.world.World;
11+
import net.minecraft.core.world.chunk.ChunkCoordinates;
12+
13+
import static redart15.commandly.command.CommandlyCommands.ReturnValues.*;
14+
15+
@SuppressWarnings("ALL") //cause this drives me nuts
16+
public class CommandAscend implements CommandManager.CommandRegistry {
17+
18+
@Override
19+
public void register(CommandDispatcher<CommandSource> dispatcher) {
20+
dispatcher.register((ArgumentBuilderLiteral) ((ArgumentBuilderLiteral) ArgumentBuilderLiteral.literal("ascend")
21+
.requires((t) -> ((CommandSource) t).hasAdmin())
22+
.executes(ctx -> CendUtil.cend(ctx, CommandAscend::ascend))
23+
.then(ArgumentBuilderRequired.argument("player", ArgumentTypeEntity.username())
24+
.executes(ctx -> CendUtil.cend(ctx, CommandAscend::ascend)))));
25+
}
26+
27+
private static int ascend(CommandSource source, Player player) {
28+
World world = player.world;
29+
for (double y = player.y; y <= world.getHeightBlocks(); y++) {
30+
ChunkCoordinates telePos = CendUtil.canPlacePlayer(world, player.x, y, player.z);
31+
if (telePos != null) {
32+
source.teleportPlayerToPos(player, telePos.x, telePos.y + 1.0f, telePos.z);
33+
source.sendTranslatableMessage("commandly.ascend.up");
34+
return code(OK);
35+
}
36+
}
37+
source.sendTranslatableMessage("commandly.ascend.fail");
38+
return code(FAIL);
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package redart15.commandly.command.cend;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.builder.ArgumentBuilderLiteral;
5+
import com.mojang.brigadier.builder.ArgumentBuilderRequired;
6+
import net.minecraft.core.entity.player.Player;
7+
import net.minecraft.core.net.command.CommandManager;
8+
import net.minecraft.core.net.command.CommandSource;
9+
import net.minecraft.core.net.command.arguments.ArgumentTypeEntity;
10+
import net.minecraft.core.world.World;
11+
import net.minecraft.core.world.chunk.ChunkCoordinates;
12+
13+
import static redart15.commandly.command.CommandlyCommands.ReturnValues.*;
14+
15+
@SuppressWarnings("ALL") //cause this drives me nuts
16+
public class CommandDescend implements CommandManager.CommandRegistry {
17+
18+
@Override
19+
public void register(CommandDispatcher<CommandSource> dispatcher) {
20+
dispatcher.register((ArgumentBuilderLiteral) ((ArgumentBuilderLiteral) ArgumentBuilderLiteral.literal("descend")
21+
.requires((t) -> ((CommandSource) t).hasAdmin())
22+
.executes(ctx -> CendUtil.cend(ctx, CommandDescend::descend))
23+
.then(ArgumentBuilderRequired.argument("player", ArgumentTypeEntity.username())
24+
.executes(ctx -> CendUtil.cend(ctx, CommandDescend::descend)))));
25+
}
26+
27+
private static int descend(CommandSource source, Player player) {
28+
World world = player.world;
29+
for (double y = player.y - player.bbHeight; y > 0; y--) {
30+
ChunkCoordinates telePos = CendUtil.canPlacePlayer(world, player.x, y, player.z);
31+
if (telePos != null) {
32+
source.teleportPlayerToPos(player, telePos.x, telePos.y + 1.0f, telePos.z);
33+
source.sendTranslatableMessage("commandly.descend.down");
34+
return code(OK);
35+
}
36+
}
37+
source.sendTranslatableMessage("commandly.descend.fail");
38+
return code(FAIL);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)