Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
rework + upd
Browse files Browse the repository at this point in the history
  • Loading branch information
JustS-js committed Feb 3, 2023
1 parent 5a64078 commit 568aeb2
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 188 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# CatTeleportMod-Fabric
Little mod to help you count seconds on cat-powered teleport
A little mod to help you count seconds on cat-powered teleports
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.19
yarn_mappings=1.19+build.1
loader_version=0.14.6
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.13

# Mod Properties
mod_version = 1.19-1.0.0
maven_group = com.just_s
archives_base_name = ctp-mod

# Dependencies
fabric_version=0.55.1+1.19
fabric_version=0.73.2+1.19.2
29 changes: 0 additions & 29 deletions src/main/java/net/just_s/ctpmod/CMDHandler.java

This file was deleted.

122 changes: 18 additions & 104 deletions src/main/java/net/just_s/ctpmod/CTPMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.just_s.ctpmod.config.ConfigParser;
import net.just_s.ctpmod.config.Point;
import net.just_s.ctpmod.util.CommandManager;
import net.just_s.ctpmod.util.ReconnectThread;
import net.minecraft.client.gui.screen.*;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
Expand All @@ -22,130 +24,40 @@
@Environment(EnvType.CLIENT)
public class CTPMod implements ClientModInitializer {
public static final String MOD_ID = "ctpmod";
public static final String MOD_CMD = "ctp";
public static final MinecraftClient MC = MinecraftClient.getInstance();
public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
public static Point[] points = new Point[0];
public static int delta = 0;
public static final ConfigParser config = ConfigParser.INSTANCE;
public static ServerInfo server = null;
public static ServerInfo currentServer = null;
public static CTPMod INSTANCE = new CTPMod();
private ReconnectThread reconnectThread;
private static ReconnectThread reconnectThread;


@Override
public void onInitializeClient() {
CommandManager.registerCommands();
ClientPlayConnectionEvents.JOIN.register((networkHandler, packetSender, client) -> currentServer = client.getCurrentServerEntry());
}

public void listPoints() {
String[] pointRepr = new String[points.length];
for (int i = 0; i < points.length; i++) {pointRepr[i] = points[i].toString();}
String message = "list of loaded Points:\n" + String.join("\n", pointRepr);
printInGame(message);
}

public void addPoint(String[] args) {
String message;
try {
if (args.length != 3) {throw new ArrayIndexOutOfBoundsException("Too much or not enough arguments.");}
String waypointName = args[1];
String[] numbers = args[2].split("-");
int startPeriod = Integer.parseInt(numbers[0]);
int endPeriod = Integer.parseInt(numbers[1]);
message = "Point §f" + waypointName + " §aadded§2 with period: §f" + startPeriod + "-" + endPeriod + "§2.";

config.addPoint(new Point(waypointName, startPeriod, endPeriod));
}
catch (NumberFormatException e) { message = "bad Period argument. §c" + e;}
catch (ArrayIndexOutOfBoundsException e) { message = "wrong number of arguments. §c" + e;}
catch (ClassCastException e) { message = "wrong type of argument. §c" + e;}
catch (IllegalArgumentException e) { message = "bad writing. §c" + e;}


printInGame(message);
}

public void delPoint(String[] args) {
String message;
try {
if (args.length != 2) {throw new ArrayIndexOutOfBoundsException("Too much or not enough arguments.");}
String waypointName = args[1];
if (config.deletePoint(waypointName)) {
message = "Point §f" + waypointName + " §cdeleted§2.";
} else {
message = "Point §f" + waypointName + " §cwas not deleted due to some issues§2.";
}
}
catch (NumberFormatException e) { message = "bad Period argument. §c" + e;}
catch (ArrayIndexOutOfBoundsException e) { message = "wrong number of arguments. §c" + e;}
catch (ClassCastException e) { message = "wrong type of argument. §c" + e;}
catch (IllegalArgumentException e) { message = "bad writing. §c" + e;}

printInGame(message);
}

public void teleport(String[] args) {
//Doesn't work in singleplayer, getCurrentServerEntry() returns null
if (!MC.isInSingleplayer()) {
if (args.length != 2) {throw new IllegalArgumentException("Too much or not enough arguments.");}
Point curPoint = null;
for (Point point : points) {
if (Objects.equals(point.getName(), args[1])) {
curPoint = point;
break;
}
}
if (curPoint == null) {
printInGame("§cThere is no §fPoint §cwith name \"§f" + args[1] + "§c\".");
return;
}
if (curPoint.getStartPeriod() < 0) {
printInGame("§cCan't teleport to §fPoint §cwith negative §fstartPeriodTime§c.");
return;
}

//if everything is okay, only then start reconnect cycle:
startReconnect(curPoint);
} else {
printInGame("§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining.");
}
}

public void rawTp(String[] args) {
if (!MC.isInSingleplayer()) {
if (args.length != 2) {throw new IllegalArgumentException("Too much or not enough arguments.");}
int time = Integer.parseInt(args[1]);

if (time < 0) {
printInGame("§cCan't reconnect with negative §fstartPeriodTime§c.");
return;
}

//if everything is okay, only then start reconnect cycle:
startReconnect(new Point("null", time, time));
} else {
printInGame("§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining.");
}
}

public void startReconnect(Point point) {
public static void startReconnect(Point point) {
Screen newScr = new DisconnectedScreen(
new MultiplayerScreen(new TitleScreen()),
Text.of("§8[§6CatTeleport§8]"),
Text.of("startReconnect"));

server = MC.getCurrentServerEntry();
ClientConnection con = Objects.requireNonNull(MC.getNetworkHandler()).getConnection();
//con.disconnect(new LiteralText("§8[§6CatTeleport§8]"));
Objects.requireNonNull(CTPMod.MC.getNetworkHandler()).getConnection().disconnect(Text.translatable("reconnecting"));
MC.disconnect();

reconnectThread = new ReconnectThread(server, point.getStartPeriod(), point.getEndPeriod());
reconnectThread = new ReconnectThread(point.getStartPeriod(), point.getEndPeriod());
reconnectThread.start();

newScr.init(MC, 0, 0);
MC.disconnect(newScr);
MC.setScreen(newScr);
}

public void finishReconnect() {
connectToServer(server);
connectToServer(currentServer);
}

public void cancelReconnect() {
Expand All @@ -161,15 +73,17 @@ public void connectToServer(ServerInfo targetInfo) {
ConnectScreen.connect(new MultiplayerScreen(new TitleScreen()), MC, ServerAddress.parse(targetInfo.address), targetInfo);
}

public static void printInGame(String... msgargs) {
public static Text generateFeedback(String message, Object... args) {
//Send message in chat that only user can see
//§0 black §8 dark_gray §g minecoin_gold
//§1 dark_blue §9 blue §f white
//§2 dark_green §a green §7 gray
//§3 dark_aqua §b aqua §e yellow
//§4 dark_red §c red §6 gold
//§5 dark_purple §d light_purple
String msg = String.join(", ", msgargs);
MC.inGameHud.getChatHud().addMessage(Text.of("§8[§6CatTeleport§8]§2 " + msg));
for (int i = 0; i < args.length; i++) {
message = message.replace("{" + i + "}", args[i].toString());
}
return Text.of("§8[§6CatTeleport§8]§2 " + message);
}
}
43 changes: 43 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.just_s.ctpmod.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.just_s.ctpmod.CTPMod;
import net.just_s.ctpmod.config.Point;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.text.Text;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public class AddCommand {
public static void register(CommandDispatcher<FabricClientCommandSource> fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) {
fabricClientCommandSourceCommandDispatcher.register(
literal(CTPMod.MOD_CMD).then(
literal("add").then(
argument("name", StringArgumentType.word()).then(
argument("startPeriod", IntegerArgumentType.integer(0)).then(
argument("endPeriod", IntegerArgumentType.integer(1)).
executes(AddCommand::run)
)
)
)
)
);
}

private static int run(CommandContext<FabricClientCommandSource> ctx) {
String waypointName = ctx.getArgument("name", String.class);
int startPeriod = ctx.getArgument("startPeriod", int.class);
int endPeriod = ctx.getArgument("endPeriod", int.class);
CTPMod.config.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
));
return 1;
}
}
40 changes: 40 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/DelCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.just_s.ctpmod.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.just_s.ctpmod.CTPMod;
import net.minecraft.command.CommandRegistryAccess;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public class DelCommand {
public static void register(CommandDispatcher<FabricClientCommandSource> fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) {
fabricClientCommandSourceCommandDispatcher.register(
literal(CTPMod.MOD_CMD).then(
literal("delete").then(
argument("name", StringArgumentType.word()).
executes(DelCommand::run)
)
)
);
}

private static int run(CommandContext<FabricClientCommandSource> ctx) {
String waypointName = ctx.getArgument("name", String.class);
if (CTPMod.config.deletePoint(waypointName)) {
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"Point §f{0} §cdeleted§2.",
waypointName
));
} else {
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"Point §f{0} §cwas not deleted due to some issues§2.",
waypointName
));
}
return 1;
}
}
28 changes: 28 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.just_s.ctpmod.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.just_s.ctpmod.CTPMod;
import net.minecraft.command.CommandRegistryAccess;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public class ListCommand {
public static void register(CommandDispatcher<FabricClientCommandSource> fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) {
fabricClientCommandSourceCommandDispatcher.register(
literal(CTPMod.MOD_CMD).then(
literal("list").executes(ListCommand::run)
)
);
}

private static int run(CommandContext<FabricClientCommandSource> ctx) {
String[] pointRepr = new String[CTPMod.points.length];
for (int i = 0; i < CTPMod.points.length; i++) {pointRepr[i] = CTPMod.points[i].toString();}
String message = "list of loaded Points:\n" + String.join("\n", pointRepr);
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
message
));
return 1;
}
}
38 changes: 38 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/RcCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.just_s.ctpmod.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.just_s.ctpmod.CTPMod;
import net.just_s.ctpmod.config.Point;
import net.minecraft.command.CommandRegistryAccess;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public class RcCommand {
public static void register(CommandDispatcher<FabricClientCommandSource> fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) {
fabricClientCommandSourceCommandDispatcher.register(
literal(CTPMod.MOD_CMD).then(
literal("rc").then(
argument("seconds", IntegerArgumentType.integer(1)).
executes(RcCommand::run)
)
)
);
}

private static int run(CommandContext<FabricClientCommandSource> ctx) {
int seconds = ctx.getArgument("seconds", int.class);
if (CTPMod.MC.isInSingleplayer()) {
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining."
));
return 1;
}
CTPMod.startReconnect(new Point(null, seconds, seconds));
return 1;
}
}
Loading

0 comments on commit 568aeb2

Please sign in to comment.