-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
ab3a866
commit 26e3f5d
Showing
6 changed files
with
271 additions
and
12 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
src/main/java/pink/zak/minestom/towerdefence/command/TowerPresetCommand.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,115 @@ | ||
package pink.zak.minestom.towerdefence.command; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.stream.JsonWriter; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.minestom.server.command.CommandSender; | ||
import net.minestom.server.command.builder.Command; | ||
import net.minestom.server.command.builder.CommandContext; | ||
import net.minestom.server.command.builder.arguments.ArgumentLiteral; | ||
import net.minestom.server.command.builder.arguments.ArgumentWord; | ||
import net.minestom.server.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import pink.zak.minestom.towerdefence.game.GameHandler; | ||
import pink.zak.minestom.towerdefence.model.preset.TowerPreset; | ||
import pink.zak.minestom.towerdefence.model.tower.TowerManager; | ||
import pink.zak.minestom.towerdefence.model.tower.placed.PlacedTower; | ||
import pink.zak.minestom.towerdefence.model.user.GameUser; | ||
import pink.zak.minestom.towerdefence.storage.TowerStorage; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
|
||
public class TowerPresetCommand extends Command { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TowerPresetCommand.class); | ||
private static final Gson GSON = new Gson(); | ||
|
||
private final @NotNull GameHandler gameHandler; | ||
private final @NotNull TowerManager towerManager; | ||
private final @NotNull TowerStorage towerStorage; | ||
|
||
public TowerPresetCommand(@NotNull GameHandler gameHandler, @NotNull TowerStorage towerStorage) { | ||
super("towerpreset"); | ||
this.gameHandler = gameHandler; | ||
this.towerManager = gameHandler.getTowerManager(); | ||
this.towerStorage = towerStorage; | ||
|
||
ArgumentLiteral saveArg = new ArgumentLiteral("save"); | ||
ArgumentLiteral loadArg = new ArgumentLiteral("load"); | ||
ArgumentWord presetIdArg = new ArgumentWord("presetId"); | ||
|
||
this.addSyntax(this::executeSave, saveArg, presetIdArg); | ||
this.addSyntax(this::executeLoad, loadArg, presetIdArg); | ||
} | ||
|
||
private void executeSave(CommandSender sender, CommandContext context) { | ||
if (!(sender instanceof Player player)) { | ||
sender.sendMessage("You must be a player to use this command."); | ||
return; | ||
} | ||
|
||
String fileName = context.get("presetId") + ".json"; | ||
Path path = Path.of("towerPresets", fileName); | ||
if (Files.exists(path)) { | ||
sender.sendMessage("Overwriting existing tower preset..."); | ||
} else { | ||
try { | ||
Files.createDirectories(path.getParent()); | ||
Files.createFile(path); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to create tower preset file", e); | ||
sender.sendMessage("Failed to create tower preset file."); | ||
return; | ||
} | ||
} | ||
|
||
GameUser user = this.gameHandler.getGameUser(player); | ||
Set<PlacedTower<?>> towers = this.gameHandler.getTowerManager().getTowers(user.getTeam()); | ||
TowerPreset preset = new TowerPreset(towers); | ||
|
||
// Save the tower preset | ||
try (JsonWriter writer = GSON.newJsonWriter(Files.newBufferedWriter(path))) { | ||
GSON.toJson(preset.toJson(), writer); | ||
sender.sendMessage("Tower preset saved."); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to save tower preset", e); | ||
sender.sendMessage("Failed to save tower preset."); | ||
} | ||
} | ||
|
||
private void executeLoad(CommandSender sender, CommandContext context) { | ||
if (!(sender instanceof Player player)) { | ||
sender.sendMessage("You must be a player to use this command."); | ||
return; | ||
} | ||
|
||
String presetName = context.get("presetId"); | ||
String fileName = presetName + ".json"; | ||
Path path = Path.of("towerPresets", fileName); | ||
if (!Files.exists(path)) { | ||
sender.sendMessage("Tower preset does not exist."); | ||
return; | ||
} | ||
|
||
JsonArray json; | ||
try { | ||
json = GSON.fromJson(Files.newBufferedReader(path), JsonArray.class); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to load tower preset", e); | ||
sender.sendMessage("Failed to load tower preset."); | ||
return; | ||
} | ||
|
||
TowerPreset preset = new TowerPreset(json); | ||
GameUser user = this.gameHandler.getGameUser(player); | ||
|
||
this.towerManager.removeAllTowers(user.getTeam()); | ||
preset.placeTowers(this.towerStorage, this.towerManager, user); | ||
sender.sendMessage(Component.text("Loaded preset %s with %s towers.".formatted(presetName, preset.getTowers().size()), NamedTextColor.GREEN)); | ||
} | ||
} |
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
119 changes: 119 additions & 0 deletions
119
src/main/java/pink/zak/minestom/towerdefence/model/preset/TowerPreset.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,119 @@ | ||
package pink.zak.minestom.towerdefence.model.preset; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import net.minestom.server.coordinate.Vec; | ||
import org.apache.commons.collections4.set.UnmodifiableSet; | ||
import org.jetbrains.annotations.NotNull; | ||
import pink.zak.minestom.towerdefence.enums.TowerType; | ||
import pink.zak.minestom.towerdefence.model.tower.TowerManager; | ||
import pink.zak.minestom.towerdefence.model.tower.TowerPlaceResult; | ||
import pink.zak.minestom.towerdefence.model.tower.config.TowerLevel; | ||
import pink.zak.minestom.towerdefence.model.tower.placed.PlacedTower; | ||
import pink.zak.minestom.towerdefence.model.user.GameUser; | ||
import pink.zak.minestom.towerdefence.storage.TowerStorage; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class TowerPreset { | ||
private final Set<@NotNull Tower> towers; | ||
|
||
public TowerPreset(@NotNull JsonArray json) { | ||
Set<Tower> towers = new HashSet<>(); | ||
for (int i = 0; i < json.size(); i++) { | ||
JsonObject jsonObject = json.get(i).getAsJsonObject(); | ||
towers.add(new Tower(jsonObject)); | ||
} | ||
|
||
this.towers = UnmodifiableSet.unmodifiableSet(towers); | ||
} | ||
|
||
public TowerPreset(@NotNull Set<PlacedTower<? extends TowerLevel>> placedTowers) { | ||
Set<Tower> towers = new HashSet<>(); | ||
for (PlacedTower<? extends TowerLevel> placedTower : placedTowers) { | ||
towers.add(new Tower(placedTower)); | ||
} | ||
|
||
this.towers = UnmodifiableSet.unmodifiableSet(towers); | ||
} | ||
|
||
public void placeTowers(@NotNull TowerStorage storage, @NotNull TowerManager manager, @NotNull GameUser user) { | ||
for (Tower tower : this.towers) { | ||
tower.place(storage, manager, user); | ||
} | ||
} | ||
|
||
public Set<Tower> getTowers() { | ||
return this.towers; | ||
} | ||
|
||
public @NotNull JsonArray toJson() { | ||
JsonArray jsonArray = new JsonArray(); | ||
for (Tower tower : this.towers) { | ||
jsonArray.add(tower.toJson()); | ||
} | ||
|
||
return jsonArray; | ||
} | ||
|
||
public static class Tower { | ||
private final @NotNull TowerType towerType; | ||
private final int level; | ||
private final @NotNull Vec position; | ||
|
||
public Tower(@NotNull JsonObject jsonObject) { | ||
this.towerType = TowerType.valueOf(jsonObject.get("type").getAsString()); | ||
this.level = jsonObject.get("level").getAsInt(); | ||
this.position = new Vec(jsonObject.get("x").getAsDouble(), jsonObject.get("y").getAsDouble(), jsonObject.get("z").getAsDouble()); | ||
} | ||
|
||
public Tower(@NotNull TowerType towerType, int level, @NotNull Vec position) { | ||
this.towerType = towerType; | ||
this.level = level; | ||
this.position = position; | ||
} | ||
|
||
public Tower(@NotNull PlacedTower<? extends TowerLevel> placedTower) { | ||
this( | ||
placedTower.getConfiguration().getType(), | ||
placedTower.getLevel().asInteger(), | ||
Vec.fromPoint(placedTower.getBasePoint()) | ||
); | ||
} | ||
|
||
public void place(@NotNull TowerStorage storage, @NotNull TowerManager manager, @NotNull GameUser user) { | ||
TowerPlaceResult result = manager.placeTower(storage.getTower(this.towerType), this.position, user, true); | ||
|
||
if (!result.isSuccessful()) { | ||
throw new IllegalStateException("Failed to place tower: " + result.failureReason()); | ||
} | ||
|
||
if (this.level > 1) { | ||
result.tower().upgrade(this.level, null); | ||
} | ||
} | ||
|
||
public @NotNull JsonObject toJson() { | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("type", this.towerType.name()); | ||
jsonObject.addProperty("level", this.level); | ||
jsonObject.addProperty("x", this.position.x()); | ||
jsonObject.addProperty("y", this.position.y()); | ||
jsonObject.addProperty("z", this.position.z()); | ||
return jsonObject; | ||
} | ||
|
||
public @NotNull TowerType getTowerType() { | ||
return this.towerType; | ||
} | ||
|
||
public int getLevel() { | ||
return this.level; | ||
} | ||
|
||
public @NotNull Vec getPosition() { | ||
return this.position; | ||
} | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/main/java/pink/zak/minestom/towerdefence/model/tower/TowerPlaceResult.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,14 @@ | ||
package pink.zak.minestom.towerdefence.model.tower; | ||
|
||
import org.jetbrains.annotations.UnknownNullability; | ||
import pink.zak.minestom.towerdefence.model.tower.config.TowerLevel; | ||
import pink.zak.minestom.towerdefence.model.tower.placed.PlacedTower; | ||
|
||
public record TowerPlaceResult(@UnknownNullability TowerPlaceFailureReason failureReason, | ||
@UnknownNullability PlacedTower<? extends TowerLevel> tower) { | ||
|
||
public boolean isSuccessful() { | ||
return this.failureReason == null; | ||
} | ||
|
||
} |
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