-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
856 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ loom { | |
mods { | ||
"teleport_commands" { | ||
sourceSet sourceSets.main | ||
sourceSet sourceSets.client | ||
} | ||
} | ||
|
||
|
10 changes: 0 additions & 10 deletions
10
src/client/java/dev/mrsnowy/teleport_commands/TeleportCommandsClient.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/client/java/dev/mrsnowy/teleport_commands/mixin/client/ExampleClientMixin.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
61 changes: 60 additions & 1 deletion
61
src/main/java/dev/mrsnowy/teleport_commands/TeleportCommands.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 |
---|---|---|
@@ -1,22 +1,81 @@ | ||
package dev.mrsnowy.teleport_commands; | ||
|
||
import dev.mrsnowy.teleport_commands.storage.StorageManager; | ||
import dev.mrsnowy.teleport_commands.utils.commands; | ||
import net.fabricmc.api.ModInitializer; | ||
|
||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.WorldSavePath; | ||
import net.minecraft.world.World; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
import static dev.mrsnowy.teleport_commands.utils.tools.DeathLocationUpdater; | ||
|
||
public class TeleportCommands implements ModInitializer { | ||
// This logger is used to write text to the console and the log file. | ||
// It is considered best practice to use your mod id as the logger's name. | ||
// That way, it's clear which mod wrote info, warnings, and errors. | ||
public static final String MOD_ID = "teleport_commands"; | ||
public static final Logger LOGGER = LoggerFactory.getLogger("teleport_commands"); | ||
|
||
public static Path SAVE_DIR; | ||
public static Path CONFIG_DIR; | ||
public static StorageManager Storage; | ||
public static MinecraftServer Server; | ||
|
||
@Override | ||
public void onInitialize() { | ||
// This code runs as soon as Minecraft is in a mod-load-ready state. | ||
// However, some things (like resources) may still be uninitialized. | ||
// Proceed with mild caution. | ||
ServerWorldEvents.LOAD.register(this::onWorldLoad); | ||
|
||
ServerEntityEvents.ENTITY_UNLOAD.register(this::onPlayerUnload); | ||
|
||
// todo: /back /tpa /tpahere /home /homes /sethome /delhome /renamehome /defaulthome /spawn /worldspawn | ||
commands.registerCommands(); | ||
|
||
} | ||
|
||
|
||
|
||
private void onPlayerUnload(Entity entity, ServerWorld world) { | ||
if (entity instanceof ServerPlayerEntity player) { | ||
// LOGGER.info(String.valueOf(entity.getRemovalReason())); | ||
if (player.getRemovalReason() != null && Objects.equals(player.getRemovalReason().toString(), "KILLED") || Objects.equals(player.getRemovalReason().toString(), "DISCARDED")) { | ||
try { | ||
// /back command position | ||
LOGGER.info(player.getPos().toString()); | ||
DeathLocationUpdater(player.getPos(), player.getServerWorld(), player.getUuidAsString()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void onWorldLoad(MinecraftServer minecraftServer, ServerWorld serverWorld) { | ||
// make it run only once | ||
if (serverWorld.getRegistryKey() == World.OVERWORLD) { | ||
// initialize da variables | ||
SAVE_DIR = Path.of(String.valueOf(minecraftServer.getSavePath(WorldSavePath.ROOT))); | ||
CONFIG_DIR = FabricLoader.getInstance().getConfigDir(); | ||
|
||
LOGGER.error(String.valueOf(SAVE_DIR)); | ||
LOGGER.error(String.valueOf(CONFIG_DIR)); | ||
|
||
LOGGER.info("Hello Fabric world!"); | ||
Server = minecraftServer; | ||
StorageManager.StorageInit(); | ||
} | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/java/dev/mrsnowy/teleport_commands/mixin/ExampleMixin.java
This file was deleted.
Oops, something went wrong.
158 changes: 158 additions & 0 deletions
158
src/main/java/dev/mrsnowy/teleport_commands/storage/StorageManager.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,158 @@ | ||
package dev.mrsnowy.teleport_commands.storage; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import dev.mrsnowy.teleport_commands.TeleportCommands; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class StorageManager { | ||
|
||
public static Path STORAGE_FOLDER; | ||
public static Path STORAGE_FILE; | ||
|
||
public static void StorageInit() { | ||
STORAGE_FOLDER = TeleportCommands.SAVE_DIR.resolve("TeleportCommands/"); | ||
STORAGE_FILE = STORAGE_FOLDER.resolve("storage.json"); | ||
|
||
try { | ||
if (!Files.exists(STORAGE_FOLDER)) { | ||
Files.createDirectories(STORAGE_FOLDER); | ||
} | ||
|
||
if (!Files.exists(STORAGE_FILE)) { | ||
Files.createFile(STORAGE_FILE); | ||
} | ||
|
||
// create the storage | ||
if (new File(String.valueOf(STORAGE_FILE)).length() == 0) { | ||
StorageClass root = new StorageClass(); | ||
root.Players = new ArrayList<>(); | ||
StorageSaver(root); | ||
} | ||
|
||
} catch (Exception e) { | ||
TeleportCommands.LOGGER.error(e.getMessage()); | ||
// crashing is probably better here, otherwise the whole mod will be broken | ||
System.exit(1); | ||
} | ||
} | ||
|
||
public static void StorageAdd(String UUID) throws Exception { | ||
StorageClass storage = StorageRetriever(); | ||
// Optional<StorageClass.Player> playerStorage = GetPlayerStorage(UUID, storage); | ||
Optional<StorageClass.Player> playerStorage = storage.Players.stream() | ||
.filter(player -> Objects.equals(UUID, player.Player_UUID)) | ||
.findFirst(); | ||
|
||
if (playerStorage.isEmpty()) { | ||
StorageClass.Player newPlayer = new StorageClass.Player(); | ||
|
||
newPlayer.Player_UUID = UUID; | ||
newPlayer.DefaultHome = "home"; | ||
newPlayer.deathLocation = new StorageClass.Player.Location(); | ||
newPlayer.deathLocation.x = new StorageClass.Player.Location().x; | ||
newPlayer.deathLocation.y = new StorageClass.Player.Location().y; | ||
newPlayer.deathLocation.z = new StorageClass.Player.Location().z; | ||
newPlayer.deathLocation.world = ""; | ||
|
||
newPlayer.Homes = new ArrayList<>(); | ||
|
||
List<StorageClass.Player> playerList = storage.Players; | ||
playerList.add(newPlayer); | ||
|
||
StorageSaver(storage); | ||
TeleportCommands.LOGGER.info("Player '" + UUID + "' added successfully in storage!"); | ||
} else { | ||
TeleportCommands.LOGGER.info("Player '" + UUID + "' already exists!"); | ||
} | ||
} | ||
|
||
public static void StorageSaver(StorageClass storage) throws Exception { | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
byte[] json = gson.toJson(storage).getBytes(); | ||
Files.write(STORAGE_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); | ||
} | ||
|
||
private static StorageClass StorageRetriever() throws Exception { | ||
// double check that the storage file is intact | ||
if (new File(String.valueOf(STORAGE_FILE)).length() == 0) { | ||
StorageInit(); | ||
} | ||
String jsonContent = Files.readString(STORAGE_FILE); | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
return gson.fromJson(jsonContent, StorageClass.class); | ||
} | ||
|
||
public static PlayerStorageResult GetPlayerStorage(String UUID) { | ||
try { | ||
StorageClass storage = StorageRetriever(); | ||
|
||
Optional<StorageClass.Player> playerStorage = storage.Players.stream() | ||
.filter(player -> Objects.equals(UUID, player.Player_UUID)) | ||
.findFirst(); | ||
|
||
if (playerStorage.isEmpty()) { | ||
StorageAdd(UUID); | ||
|
||
storage = StorageRetriever(); | ||
|
||
playerStorage = storage.Players.stream() | ||
.filter(player -> Objects.equals(UUID, player.Player_UUID)) | ||
.findFirst(); | ||
|
||
if (playerStorage.isEmpty()) { | ||
throw new Exception("No Player found?!"); | ||
} | ||
} | ||
|
||
return new PlayerStorageResult(storage, playerStorage.get()); | ||
|
||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static class PlayerStorageResult { | ||
public StorageClass storage; | ||
public StorageClass.Player playerStorage; | ||
|
||
public PlayerStorageResult(StorageClass storage, StorageClass.Player playerStorage) { | ||
this.storage = storage; | ||
this.playerStorage = playerStorage; | ||
} | ||
} | ||
|
||
public static class StorageClass { | ||
public List<Player> Players; | ||
|
||
public static class Player { | ||
public String Player_UUID; | ||
public String DefaultHome; | ||
public Location deathLocation; | ||
public List<Home> Homes; | ||
|
||
public static class Location { | ||
public double x; | ||
public double y; | ||
public double z; | ||
public String world; | ||
} | ||
|
||
public static class Home { | ||
public String name; | ||
public double x; | ||
public double y; | ||
public double z; | ||
public String world; | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/dev/mrsnowy/teleport_commands/storage/configManager.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,8 @@ | ||
package dev.mrsnowy.teleport_commands.storage; | ||
|
||
//import java.nio.file.Path; | ||
//import net.fabricmc.loader.api.FabricLoader; | ||
|
||
public class configManager { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/dev/mrsnowy/teleport_commands/suggestions/HomesuggestionProvider.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,32 @@ | ||
package dev.mrsnowy.teleport_commands.suggestions; | ||
|
||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.suggestion.SuggestionProvider; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import dev.mrsnowy.teleport_commands.storage.StorageManager; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.Vec3d; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static dev.mrsnowy.teleport_commands.storage.StorageManager.GetPlayerStorage; | ||
|
||
public class HomesuggestionProvider implements SuggestionProvider<ServerCommandSource> { | ||
@Override | ||
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) { | ||
|
||
ServerPlayerEntity player = Objects.requireNonNull(context.getSource().getPlayer()); | ||
StorageManager.StorageClass.Player playerStorage = GetPlayerStorage(player.getUuidAsString()).playerStorage; | ||
|
||
for (StorageManager.StorageClass.Player.Home currenthome : playerStorage.Homes) { | ||
builder.suggest(currenthome.name); | ||
} | ||
|
||
// Build and return the suggestions | ||
return builder.buildFuture(); | ||
} | ||
} |
Oops, something went wrong.