Skip to content

Commit

Permalink
refactor island templates
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Oct 2, 2023
1 parent 22de70b commit 97f7ec8
Show file tree
Hide file tree
Showing 20 changed files with 480 additions and 101 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel = true
# Mod Properties
maven_group = ru.pinkgoosik
archives_base_name = skylands
mod_version = 0.3.12+1.20.2
mod_version = 0.4.0+1.20.2

# Dependencies | Check these on https://fabricmc.net/develop
minecraft_version = 1.20.2
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/skylands/command/CreateCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package skylands.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -9,6 +10,7 @@
import skylands.logic.Skylands;
import skylands.util.SkylandsTexts;

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

public class CreateCommand {
Expand All @@ -21,7 +23,30 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
CreateCommand.run(player);
}
return 1;
})));
}).then(argument("template", StringArgumentType.greedyString()).suggests((context, builder) -> {
var player = context.getSource().getPlayer();

if(player != null) {
String remains = builder.getRemaining();

for(var template : Skylands.config.islandTemplates) {
if(template.name.contains(remains)) {
builder.suggest(template.name);
}
}
return builder.buildFuture();
}
return builder.buildFuture();
}).executes(context -> {
var source = context.getSource();
var player = source.getPlayer();
var template = StringArgumentType.getString(context, "template");

if(player != null) {
CreateCommand.run(player, template);
}
return 1;
}))));
}

static void run(ServerPlayerEntity player) {
Expand All @@ -38,4 +63,19 @@ static void run(ServerPlayerEntity player) {
player.sendMessage(SkylandsTexts.prefixed("message.skylands.island_create.success"));
}
}

static void run(ServerPlayerEntity player, String template) {
IslandStuck islands = Skylands.instance.islands;

if(islands.get(player).isPresent()) {
player.sendMessage(SkylandsTexts.prefixed("message.skylands.island_create.fail"));
}
else {
Island island = islands.create(player, template);
if(Skylands.config.teleportAfterIslandCreation) {
island.visitAsMember(player);
}
player.sendMessage(SkylandsTexts.prefixed("message.skylands.island_create.success"));
}
}
}
20 changes: 14 additions & 6 deletions src/main/java/skylands/command/HubCommands.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package skylands.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import skylands.config.PlayerPosition;
import skylands.logic.Skylands;
import skylands.util.SkylandsTexts;

Expand All @@ -30,9 +31,16 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal("force-sl").then(literal("hub").then(literal("set-spawn-pos").requires(Permissions.require("skylands.force.hub.position", 4)).then(argument("position", blockPos()).executes(context -> {
var pos = BlockPosArgumentType.getBlockPos(context, "position");
var source = context.getSource();
HubCommands.setPos(pos, source);
HubCommands.setPos(pos, 0, 0, source);
return 1;
}))).then(literal("toggle-protection").requires(Permissions.require("skylands.force.hub.protection", 4)).executes(context -> {
}).then(argument("yaw", IntegerArgumentType.integer()).then(argument("pitch", IntegerArgumentType.integer()).executes(context -> {
var pos = BlockPosArgumentType.getBlockPos(context, "position");
int yaw = IntegerArgumentType.getInteger(context, "yaw");
int pitch = IntegerArgumentType.getInteger(context, "pitch");
var source = context.getSource();
HubCommands.setPos(pos, yaw, pitch, source);
return 1;
}))))).then(literal("toggle-protection").requires(Permissions.require("skylands.force.hub.protection", 4)).executes(context -> {
HubCommands.toggleProtection(context.getSource());
return 1;
}))));
Expand All @@ -43,9 +51,9 @@ public static void visit(ServerPlayerEntity player) {
Skylands.getHub().visit(player);
}

static void setPos(BlockPos pos, ServerCommandSource source) {
Skylands.getHub().pos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ();
static void setPos(BlockPos pos, int yaw, int pitch, ServerCommandSource source) {
Skylands.getHub().spawnPos = new PlayerPosition(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, yaw, pitch);
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ() + " " + yaw + " " + pitch;
source.sendFeedback(() -> SkylandsTexts.prefixed("message.skylands.hub_pos_change", map -> map.put("%pos%", posText)), true);
}

Expand Down
39 changes: 28 additions & 11 deletions src/main/java/skylands/command/SettingCommands.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package skylands.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import skylands.config.PlayerPosition;
import skylands.logic.Skylands;
import skylands.util.SkylandsTexts;

Expand All @@ -27,17 +28,33 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
var player = context.getSource().getPlayer();
var pos = BlockPosArgumentType.getBlockPos(context, "position");
if(player != null) {
SettingCommands.setSpawnPos(player, pos);
SettingCommands.setSpawnPos(player, pos, 0, 0);
}
return 1;
}))).then(literal("set-visits-pos").requires(Permissions.require("skylands.settings.visits.position", true)).then(argument("position", blockPos()).executes(context -> {
}).then(argument("yaw", IntegerArgumentType.integer()).then(argument("pitch", IntegerArgumentType.integer()).executes(context -> {
var player = context.getSource().getPlayer();
var pos = BlockPosArgumentType.getBlockPos(context, "position");
int yaw = IntegerArgumentType.getInteger(context, "yaw");
int pitch = IntegerArgumentType.getInteger(context, "pitch");

SettingCommands.setSpawnPos(player, pos, yaw, pitch);
return 1;
}))))).then(literal("set-visits-pos").requires(Permissions.require("skylands.settings.visits.position", true)).then(argument("position", blockPos()).executes(context -> {
var player = context.getSource().getPlayer();
var pos = BlockPosArgumentType.getBlockPos(context, "position");
if(player != null) {
SettingCommands.setVisitsPos(player, pos);
SettingCommands.setVisitsPos(player, pos, 0, 0);
}
return 1;
})))));
}).then(argument("yaw", IntegerArgumentType.integer()).then(argument("pitch", IntegerArgumentType.integer()).executes(context -> {
var player = context.getSource().getPlayer();
var pos = BlockPosArgumentType.getBlockPos(context, "position");
int yaw = IntegerArgumentType.getInteger(context, "yaw");
int pitch = IntegerArgumentType.getInteger(context, "pitch");

SettingCommands.setVisitsPos(player, pos, yaw, pitch);
return 1;
})))))));
}

static void toggleVisits(ServerPlayerEntity player) {
Expand All @@ -54,19 +71,19 @@ static void toggleVisits(ServerPlayerEntity player) {
}, () -> player.sendMessage(SkylandsTexts.prefixed("message.skylands.settings.no_island")));
}

static void setSpawnPos(ServerPlayerEntity player, BlockPos pos) {
static void setSpawnPos(ServerPlayerEntity player, BlockPos pos, int yaw, int pitch) {
Skylands.instance.islands.get(player).ifPresentOrElse(island -> {
island.spawnPos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ();
island.spawnPos = new PlayerPosition(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, yaw, pitch);
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ() + " " + yaw + " " + pitch;
player.sendMessage(SkylandsTexts.prefixed("message.skylands.settings.spawn_pos_change", map -> map.put("%pos%", posText)));

}, () -> player.sendMessage(SkylandsTexts.prefixed("message.skylands.settings.no_island")));
}

static void setVisitsPos(ServerPlayerEntity player, BlockPos pos) {
static void setVisitsPos(ServerPlayerEntity player, BlockPos pos, int yaw, int pitch) {
Skylands.instance.islands.get(player).ifPresentOrElse(island -> {
island.visitsPos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ();
island.visitsPos = new PlayerPosition(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, yaw, pitch);
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ() + " " + yaw + " " + pitch;
player.sendMessage(SkylandsTexts.prefixed("message.skylands.settings.visits_pos_change", map -> map.put("%pos%", posText)));

}, () -> player.sendMessage(SkylandsTexts.prefixed("message.skylands.settings.no_island")));
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/skylands/config/BlockPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package skylands.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;

import java.io.IOException;

public class BlockPosition {
public int x;
public int y;
public int z;

public BlockPosition(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public Vec3i toVec() {
return new Vec3i(this.x, this.y, this.z);
}

public BlockPos toBlockPos() {
return new BlockPos(x, y, z);
}

public static class JsonAdapter extends TypeAdapter<BlockPosition> {

public static final Gson GSON = new GsonBuilder().setLenient().create();

@Override
public void write(JsonWriter out, BlockPosition value) throws IOException {
out.jsonValue(GSON.toJson(value).replaceAll(",", ", ")
.replaceAll("\\{", "{ ")
.replaceAll("}", " }").replaceAll(":", ": "));
}

@Override
public BlockPosition read(JsonReader in) {
return GSON.fromJson(in, BlockPosition.class);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/skylands/config/HubTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package skylands.config;

public class HubTemplate extends Template {

public HubTemplate(String type, Metadata metadata) {
super(null, type, metadata, null);
}
}
11 changes: 11 additions & 0 deletions src/main/java/skylands/config/IslandTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package skylands.config;

public class IslandTemplate extends Template {
public String netherTemplate;

public IslandTemplate(String name, String type, Metadata metadata, PlayerPosition playerSpawnPosition, String netherTemplate) {
super(name, type, metadata, playerSpawnPosition);
this.netherTemplate = netherTemplate;
}

}
50 changes: 50 additions & 0 deletions src/main/java/skylands/config/Metadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package skylands.config;

import com.google.gson.annotations.JsonAdapter;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

public class Metadata {

public String structure;
@JsonAdapter(BlockPosition.JsonAdapter.class)
public BlockPosition position;
@Nullable
public BlockPosition pivot;
@Nullable
public String rotation;

public String path;

public Metadata(String structureId, BlockPosition position) {
this.structure = structureId;
this.position = position;
}

public Metadata(String path) {
this.path = path;
}

public BlockRotation getRotation() {
BlockRotation rot = BlockRotation.NONE;

if(rotation != null) {
switch (rotation.toLowerCase()) {
case "clockwise_90" -> rot = BlockRotation.CLOCKWISE_90;
case "180" -> rot = BlockRotation.CLOCKWISE_180;
case "counterclockwise_90" -> rot = BlockRotation.COUNTERCLOCKWISE_90;
}
}

return rot;
}

public BlockPos getPivot() {
if(pivot != null) {
return pivot.toBlockPos();
}
return new BlockPos(0, 0, 0);
}

}
59 changes: 59 additions & 0 deletions src/main/java/skylands/config/PlayerPosition.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package skylands.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.Vec3d;

import java.io.IOException;

public class PlayerPosition {
public double x;
public double y;
Expand All @@ -27,4 +35,55 @@ public PlayerPosition(double x, double y, double z, float yaw, float pitch) {
public Vec3d toVec() {
return new Vec3d(this.x, this.y, this.z);
}

public NbtCompound toNbt() {
NbtCompound nbt = new NbtCompound();
nbt.putDouble("x", this.x);
nbt.putDouble("y", this.y);
nbt.putDouble("z", this.z);
nbt.putFloat("yaw", this.yaw);
nbt.putFloat("pitch", this.pitch);
return nbt;
}

public static PlayerPosition fromNbt(NbtCompound nbt) {
PlayerPosition pos = new PlayerPosition(nbt.getDouble("x"), nbt.getDouble("y"), nbt.getDouble("z"));
if (nbt.contains("yaw")) pos.yaw = nbt.getFloat("yaw");
if (nbt.contains("pitch")) pos.yaw = nbt.getFloat("pitch");
return pos;
}

public static PlayerPosition fromNbt(NbtCompound nbt, PlayerPosition backup) {
PlayerPosition pos = new PlayerPosition(0, 0, 0);

if(nbt.contains("x")) pos.x = nbt.getDouble("x");
else pos.x = backup.x;

if(nbt.contains("y")) pos.y = nbt.getDouble("y");
else pos.y = backup.y;

if(nbt.contains("z")) pos.z = nbt.getDouble("z");
else pos.z = backup.z;

if (nbt.contains("yaw")) pos.yaw = nbt.getFloat("yaw");
if (nbt.contains("pitch")) pos.yaw = nbt.getFloat("pitch");
return pos;
}

public static class JsonAdapter extends TypeAdapter<PlayerPosition> {

public static final Gson GSON = new GsonBuilder().setLenient().create();

@Override
public void write(JsonWriter out, PlayerPosition value) throws IOException {
out.jsonValue(GSON.toJson(value).replaceAll(",", ", ")
.replaceAll("\\{", "{ ")
.replaceAll("}", " }").replaceAll(":", ": "));
}

@Override
public PlayerPosition read(JsonReader in) {
return GSON.fromJson(in, PlayerPosition.class);
}
}
}
Loading

0 comments on commit 97f7ec8

Please sign in to comment.