Skip to content

Commit

Permalink
Fixed a bug that lead to Points not saving when adding through commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMasrozYTLIVE committed Jul 30, 2024
1 parent f559948 commit f42bc78
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.21+build.9
loader_version=0.15.11

# Mod Properties
mod_version = 1.21-1.2.1
mod_version = 1.21-1.2.2
maven_group = com.just_s
archives_base_name = ctp-mod

Expand Down
33 changes: 30 additions & 3 deletions src/main/java/net/just_s/ctpmod/CTPMod.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.just_s.ctpmod;

import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.ConfigHolder;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
Expand All @@ -13,7 +14,6 @@
import net.minecraft.client.gui.screen.*;
import net.minecraft.client.gui.screen.multiplayer.ConnectScreen;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
import net.minecraft.client.network.CookieStorage;
import net.minecraft.client.network.ServerInfo;
import net.minecraft.client.network.ServerAddress;
import net.minecraft.text.Text;
Expand All @@ -23,6 +23,7 @@
import net.minecraft.client.MinecraftClient;

import java.util.Objects;
import java.util.Optional;

@Environment(EnvType.CLIENT)
public class CTPMod implements ClientModInitializer {
Expand All @@ -34,13 +35,15 @@ public class CTPMod implements ClientModInitializer {
public static CTPMod INSTANCE = new CTPMod();
private static ReconnectThread reconnectThread;
public static ModConfig CONFIG;
private static ConfigHolder<ModConfig> CONFIG_HOLDER;

@Override
public void onInitializeClient() {
AutoConfig.register(ModConfig.class, GsonConfigSerializer::new);
CONFIG = AutoConfig.getConfigHolder(ModConfig.class).getConfig();
CONFIG_HOLDER = AutoConfig.getConfigHolder(ModConfig.class);
CONFIG = CONFIG_HOLDER.getConfig();

CommandRegistry.registerCommands();
CommandRegistry.register();
ClientPlayConnectionEvents.JOIN.register((networkHandler, packetSender, client) -> currentServer = client.getCurrentServerEntry());
}

Expand Down Expand Up @@ -88,4 +91,28 @@ public static Text generateFeedback(String message, Object... args) {
}
return Text.of("§8[§6CatTeleport§8]§2 " + message);
}

public static Point getPoint(String name) {
if(name.isEmpty()) return null;
Optional<Point> point = CONFIG.points.stream().filter(p -> p.getName().equalsIgnoreCase(name)).findFirst();
return point.orElse(null);
}

public static boolean addPoint(Point point) {
if(point == null) return false;
CONFIG.points.add(point);
CONFIG_HOLDER.save();

return true;
}

public static boolean deletePoint(Point point) {
boolean deleted = CONFIG.points.remove(point);
CONFIG_HOLDER.save();
return deleted;
}

public static boolean deletePoint(String name) {
return deletePoint(getPoint(name));
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/just_s/ctpmod/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static int run(CommandContext<FabricClientCommandSource> ctx) {
String waypointName = ctx.getArgument("name", String.class);
float startPeriod = ctx.getArgument("startPeriod", float.class);
float endPeriod = ctx.getArgument("endPeriod", float.class);
CTPMod.CONFIG.points.add(new Point(waypointName, startPeriod, endPeriod));
CTPMod.addPoint(new Point(waypointName, startPeriod, endPeriod));
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"Point §f{0} §aadded§2 with period: §f{1}-{2}§2.",
waypointName, startPeriod, endPeriod
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/net/just_s/ctpmod/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.just_s.ctpmod.CTPMod;
import net.just_s.ctpmod.config.Point;

import java.util.concurrent.CompletableFuture;

Expand All @@ -17,16 +16,7 @@ public static CompletableFuture<Suggestions> suggest(CommandContext<FabricClient
public static int run(CommandContext<FabricClientCommandSource> ctx) {
String pointName = ctx.getArgument("name", String.class);

boolean deleted = false;
for(Point point : CTPMod.CONFIG.points) {
if(point.getName().equals(pointName)) {
deleted = true;
CTPMod.CONFIG.points.remove(point);
break;
}
}

if (deleted) {
if (CTPMod.deletePoint(pointName)) {
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"Point §f{0} §cdeleted§2.",
pointName
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/just_s/ctpmod/commands/TpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.just_s.ctpmod.CTPMod;
import net.just_s.ctpmod.config.Point;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class TpCommand {
Expand All @@ -26,8 +25,8 @@ public static int run(CommandContext<FabricClientCommandSource> ctx) {
return 1;
}
String pointName = ctx.getArgument("name", String.class);
Optional<Point> point = CTPMod.CONFIG.points.stream().filter(p -> p.getName().equals(pointName)).findFirst();
if (point.isEmpty()) {
Point point = CTPMod.getPoint(pointName);
if (point == null) {
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"§cThere is no §fPoint §cwith name \"§f{0}§c\".",
pointName
Expand All @@ -36,7 +35,7 @@ public static int run(CommandContext<FabricClientCommandSource> ctx) {
}

//if everything is okay, only then start reconnect cycle:
CTPMod.startReconnect(point.get());
CTPMod.startReconnect(point);
return 1;
}
}
1 change: 0 additions & 1 deletion src/main/java/net/just_s/ctpmod/config/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import me.shedaniel.autoconfig.annotation.ConfigEntry;

@Getter
@Setter
@AllArgsConstructor
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/just_s/ctpmod/util/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public class CommandRegistry {
public static void registerCommands() {
public static void register() {
ClientCommandRegistrationCallback.EVENT.register((CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) -> {
LiteralCommandNode<FabricClientCommandSource> mainCommand = literal("ctp").build();

Expand Down

0 comments on commit f42bc78

Please sign in to comment.