Skip to content

Commit 8ba8ca4

Browse files
committed
Introduce a GeneralUtils class in the common module and move code to it
1 parent 5408510 commit 8ba8ca4

File tree

18 files changed

+160
-314
lines changed

18 files changed

+160
-314
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.communityradargg.forgemod;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public interface ModSpecific {
6+
String MOD_ID = "communityradar";
7+
8+
/**
9+
* Gets the version.
10+
*
11+
* @return Returns the version.
12+
*/
13+
String getVersion();
14+
15+
void sendMessage(final @NotNull String message);
16+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.github.communityradargg.forgemod.util;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
import java.net.InetSocketAddress;
6+
import java.net.SocketAddress;
7+
import java.time.LocalDateTime;
8+
import java.time.format.DateTimeFormatter;
9+
import java.util.Locale;
10+
import java.util.Optional;
11+
12+
/**
13+
* A class with some general util methods.
14+
*/
15+
public class GeneralUtils {
16+
private static final DateTimeFormatter readableDateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
17+
private static boolean onGrieferGames = false;
18+
19+
/**
20+
* Checks if a given hostname is a hostname of GrieferGames.
21+
* <br><br>
22+
* Following domains are taken into account:
23+
* <br>
24+
* - griefergames.net
25+
* <br>
26+
* - griefergames.de
27+
* <br>
28+
* - griefergames.live
29+
*
30+
* @param hostName The hostname to check.
31+
* @return Returns, whether the given hostname is one of the GrieferGames hostnames.
32+
*/
33+
public static boolean isGrieferGamesHostName(final @NotNull String hostName) {
34+
final String filteredHostName = Optional.of(hostName)
35+
.filter(host -> host.endsWith("."))
36+
.map(host -> host.substring(0, host.length() - 1).toLowerCase(Locale.ENGLISH))
37+
.orElse(hostName.toLowerCase(Locale.ENGLISH));
38+
return filteredHostName.endsWith("griefergames.net") || filteredHostName.endsWith("griefergames.de") || filteredHostName.endsWith("griefergames.live");
39+
}
40+
41+
/**
42+
* Formats a given date time in a human-readable form.
43+
*
44+
* @param localDateTime The local date time to format.
45+
* @return Returns the formatted date time.
46+
*/
47+
public static @NotNull String formatDateTime(final @NotNull LocalDateTime localDateTime) {
48+
return localDateTime.format(readableDateTimeFormatter);
49+
}
50+
51+
/**
52+
* Gets the GrieferGames connection state.
53+
*
54+
* @return Returns the GrieferGames connection state.
55+
*/
56+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
57+
public static boolean isOnGrieferGames() {
58+
return onGrieferGames;
59+
}
60+
61+
/**
62+
* Sets the GrieferGames connection state.
63+
*
64+
* @param onGrieferGames The GrieferGames connection state to set.
65+
*/
66+
public static void setOnGrieferGames(final boolean onGrieferGames) {
67+
GeneralUtils.onGrieferGames = onGrieferGames;
68+
}
69+
70+
public static void setOnGrieferGames(final boolean isLocal, final @Nullable SocketAddress socketAddress) {
71+
if (isLocal) {
72+
return;
73+
}
74+
75+
if (!(socketAddress instanceof InetSocketAddress)) {
76+
return;
77+
}
78+
79+
final String hostname = ((InetSocketAddress) socketAddress).getHostName();
80+
if (GeneralUtils.isGrieferGamesHostName(hostname)) {
81+
GeneralUtils.setOnGrieferGames(true);
82+
return;
83+
}
84+
GeneralUtils.setOnGrieferGames(false);
85+
}
86+
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/util/Messages.java renamed to common/src/main/java/io/github/communityradargg/forgemod/util/Messages.java

File renamed without changes.

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import io.github.communityradargg.forgemod.event.KeyInputListener;
2222
import io.github.communityradargg.forgemod.event.PlayerNameFormatListener;
2323
import io.github.communityradargg.forgemod.radarlistmanager.RadarListManager;
24+
import net.minecraft.client.Minecraft;
25+
import net.minecraft.util.text.TextComponentString;
2426
import net.minecraftforge.client.ClientCommandHandler;
2527
import net.minecraftforge.common.MinecraftForge;
2628
import net.minecraftforge.fml.common.Loader;
@@ -39,12 +41,10 @@
3941
* This class represents the main class of the mod.
4042
*/
4143
@Mod(modid = CommunityRadarMod.MOD_ID)
42-
public class CommunityRadarMod {
43-
public static final String MOD_ID = "communityradar";
44+
public class CommunityRadarMod implements ModSpecific {
4445
private static final Logger LOGGER = LogManager.getLogger(CommunityRadarMod.class);
4546
private RadarListManager listManager;
4647
private String version;
47-
private boolean onGrieferGames = false;
4848

4949
/**
5050
* The listener for the {@link FMLInitializationEvent} event.
@@ -59,7 +59,7 @@ public void init(FMLInitializationEvent event) {
5959
version = modContainer == null ? "UNKNOWN" : modContainer.getVersion();
6060

6161
final File directoryPath = Paths.get(new File("")
62-
.getAbsolutePath(),"communityradar", "lists")
62+
.getAbsolutePath(), "communityradar", "lists")
6363
.toFile();
6464
if (!directoryPath.exists() && !directoryPath.mkdirs()) {
6565
LOGGER.error("Could not create directory: {}", directoryPath);
@@ -81,7 +81,7 @@ private void registerEvents() {
8181
MinecraftForge.EVENT_BUS.register(new ClientChatReceivedListener(this));
8282
MinecraftForge.EVENT_BUS.register(new PlayerNameFormatListener(this));
8383
MinecraftForge.EVENT_BUS.register(new KeyInputListener(this));
84-
MinecraftForge.EVENT_BUS.register(new ClientConnectionDisconnectListener(this));
84+
MinecraftForge.EVENT_BUS.register(new ClientConnectionDisconnectListener());
8585
}
8686

8787
/**
@@ -113,31 +113,18 @@ private void registerPublicLists() {
113113
return listManager;
114114
}
115115

116-
/**
117-
* Gets the GrieferGames connection state.
118-
*
119-
* @return Returns the GrieferGames connection state.
120-
*/
121-
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
122-
public boolean isOnGrieferGames() {
123-
return onGrieferGames;
116+
@Override
117+
public String getVersion() {
118+
return version;
124119
}
125120

126-
/**
127-
* Sets the GrieferGames connection state.
128-
*
129-
* @param onGrieferGames The GrieferGames connection state to set.
130-
*/
131-
public void setOnGrieferGames(final boolean onGrieferGames) {
132-
this.onGrieferGames = onGrieferGames;
133-
}
121+
@Override
122+
public void sendMessage(final @NotNull String message) {
123+
if (Minecraft.getMinecraft().player == null) {
124+
LOGGER.warn("Could not add message to chat. Player is null. The message is following: {}", message);
125+
return;
126+
}
134127

135-
/**
136-
* Gets the version.
137-
*
138-
* @return Returns the version.
139-
*/
140-
public String getVersion() {
141-
return version;
128+
Minecraft.getMinecraft().player.sendMessage(new TextComponentString(message));
142129
}
143130
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/command/CheckSubcommand.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.github.communityradargg.forgemod.CommunityRadarMod;
1919
import io.github.communityradargg.forgemod.radarlistmanager.RadarListEntry;
20+
import io.github.communityradargg.forgemod.util.GeneralUtils;
2021
import io.github.communityradargg.forgemod.util.Messages;
2122
import io.github.communityradargg.forgemod.util.RadarMessage;
2223
import io.github.communityradargg.forgemod.util.Utils;
@@ -95,8 +96,8 @@ private void handleCheckPlayerSubcommand(final @NotNull EntityPlayer player, fin
9596
.replaceWithColorCodes("{prefix}", communityRadarMod.getListManager().getPrefix(entry.uuid()))
9697
.replace("{name}", entry.name())
9798
.replace("{cause}", entry.cause())
98-
.replace("{entryCreationDate}", Utils.formatDateTime(entry.entryCreationDate()))
99-
.replace("{entryUpdateDate}", Utils.formatDateTime(entry.entryUpdateDate()))
99+
.replace("{entryCreationDate}", GeneralUtils.formatDateTime(entry.entryCreationDate()))
100+
.replace("{entryUpdateDate}", GeneralUtils.formatDateTime(entry.entryUpdateDate()))
100101
.build().toChatComponentText());
101102
});
102103
}
@@ -133,8 +134,8 @@ private void handleCheckAllSubcommand(final @NotNull EntityPlayer player) {
133134
.replaceWithColorCodes("{prefix}", communityRadarMod.getListManager().getPrefix(entry.uuid()))
134135
.replace("{name}", entry.name())
135136
.replace("{cause}", entry.cause())
136-
.replace("{entryCreationDate}", Utils.formatDateTime(entry.entryCreationDate()))
137-
.replace("{entryUpdateDate}", Utils.formatDateTime(entry.entryUpdateDate()))
137+
.replace("{entryCreationDate}", GeneralUtils.formatDateTime(entry.entryCreationDate()))
138+
.replace("{entryUpdateDate}", GeneralUtils.formatDateTime(entry.entryUpdateDate()))
138139
.build().toChatComponentText());
139140
}
140141
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.github.communityradargg.forgemod.event;
1717

1818
import io.github.communityradargg.forgemod.CommunityRadarMod;
19+
import io.github.communityradargg.forgemod.util.GeneralUtils;
1920
import io.github.communityradargg.forgemod.util.Utils;
2021
import net.minecraft.util.text.TextComponentString;
2122
import net.minecraftforge.client.event.ClientChatReceivedEvent;
@@ -51,7 +52,7 @@ public ClientChatReceivedListener(final @NotNull CommunityRadarMod communityRada
5152
*/
5253
@SubscribeEvent
5354
public void onClientChatReceived(final ClientChatReceivedEvent event) {
54-
if (!communityRadarMod.isOnGrieferGames()) {
55+
if (!GeneralUtils.isOnGrieferGames()) {
5556
return;
5657
}
5758

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/event/ClientConnectionDisconnectListener.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,22 @@
1515
*/
1616
package io.github.communityradargg.forgemod.event;
1717

18-
import io.github.communityradargg.forgemod.CommunityRadarMod;
19-
import io.github.communityradargg.forgemod.util.Utils;
18+
import io.github.communityradargg.forgemod.util.GeneralUtils;
2019
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
2120
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
22-
import org.jetbrains.annotations.NotNull;
23-
24-
import java.net.InetSocketAddress;
25-
import java.net.SocketAddress;
2621

2722
/**
2823
* A class containing listeners for player connect and disconnect from and to servers.
2924
*/
3025
public class ClientConnectionDisconnectListener {
31-
private final CommunityRadarMod communityRadarMod;
32-
33-
/**
34-
* Constructs the class {@link ClientConnectionDisconnectListener}.
35-
*
36-
* @param communityRadarMod An instance of the {@link CommunityRadarMod} class.
37-
*/
38-
public ClientConnectionDisconnectListener(final @NotNull CommunityRadarMod communityRadarMod) {
39-
this.communityRadarMod = communityRadarMod;
40-
}
41-
4226
/**
4327
* The listener for the {@link FMLNetworkEvent.ClientConnectedToServerEvent} event.
4428
*
4529
* @param event The event.
4630
*/
4731
@SubscribeEvent
4832
public void onFMLNetworkClientConnectedToServer(final FMLNetworkEvent.ClientConnectedToServerEvent event) {
49-
if (event.isLocal()) {
50-
return;
51-
}
52-
53-
final SocketAddress socketAddress = event.getManager().getRemoteAddress();
54-
if (!(socketAddress instanceof InetSocketAddress)) {
55-
return;
56-
}
57-
58-
final String hostname = ((InetSocketAddress) socketAddress).getHostName();
59-
if (Utils.isGrieferGamesHostName(hostname)) {
60-
communityRadarMod.setOnGrieferGames(true);
61-
return;
62-
}
63-
communityRadarMod.setOnGrieferGames(false);
33+
GeneralUtils.setOnGrieferGames(event.isLocal(), event.getManager().getRemoteAddress());
6434
}
6535

6636
/**
@@ -70,6 +40,6 @@ public void onFMLNetworkClientConnectedToServer(final FMLNetworkEvent.ClientConn
7040
*/
7141
@SubscribeEvent
7242
public void onFMLNetworkClientDisconnectionFromServer(final FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
73-
communityRadarMod.setOnGrieferGames(false);
43+
GeneralUtils.setOnGrieferGames(false);
7444
}
7545
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.github.communityradargg.forgemod.event;
1717

1818
import io.github.communityradargg.forgemod.CommunityRadarMod;
19+
import io.github.communityradargg.forgemod.util.GeneralUtils;
1920
import io.github.communityradargg.forgemod.util.Utils;
2021
import net.minecraft.client.Minecraft;
2122
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@@ -44,7 +45,7 @@ public KeyInputListener(final @NotNull CommunityRadarMod communityRadarMod) {
4445
*/
4546
@SubscribeEvent
4647
public void onKeyInput(final InputEvent.KeyInputEvent event) {
47-
if (!communityRadarMod.isOnGrieferGames()) {
48+
if (!GeneralUtils.isOnGrieferGames()) {
4849
return;
4950
}
5051

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.github.communityradargg.forgemod.event;
1717

1818
import io.github.communityradargg.forgemod.CommunityRadarMod;
19+
import io.github.communityradargg.forgemod.util.GeneralUtils;
1920
import io.github.communityradargg.forgemod.util.Utils;
2021
import net.minecraftforge.event.entity.player.PlayerEvent;
2122
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@@ -43,7 +44,7 @@ public PlayerNameFormatListener(final @NotNull CommunityRadarMod communityRadarM
4344
*/
4445
@SubscribeEvent
4546
public void onPlayerNameFormat(final PlayerEvent.NameFormat event) {
46-
if (!communityRadarMod.isOnGrieferGames()) {
47+
if (!GeneralUtils.isOnGrieferGames()) {
4748
return;
4849
}
4950
Utils.updatePlayerNameTag(communityRadarMod, event.getEntityPlayer(), communityRadarMod.getListManager().getExistingPrefixes());

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/util/Utils.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
import java.io.InputStreamReader;
3434
import java.net.HttpURLConnection;
3535
import java.net.URL;
36-
import java.time.LocalDateTime;
37-
import java.time.format.DateTimeFormatter;
3836
import java.util.HashMap;
39-
import java.util.Locale;
4037
import java.util.Map;
4138
import java.util.Optional;
4239
import java.util.Set;
@@ -51,7 +48,6 @@ public class Utils {
5148
private static final Logger LOGGER = LogManager.getLogger(Utils.class);
5249
private static final String MOJANG_API_NAME_TO_UUID = "https://api.mojang.com/users/profiles/minecraft/";
5350
private static final Pattern UUID_MOJANG_API_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
54-
private static final DateTimeFormatter readableDateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
5551
private static final Map<String, UUID> uuidNameCache = new HashMap<>();
5652

5753
/**
@@ -139,38 +135,6 @@ public class Utils {
139135
});
140136
}
141137

142-
/**
143-
* Formats a given date time in a human-readable form.
144-
*
145-
* @param localDateTime The local date time to format.
146-
* @return Returns the formatted date time.
147-
*/
148-
public static @NotNull String formatDateTime(final @NotNull LocalDateTime localDateTime) {
149-
return localDateTime.format(readableDateTimeFormatter);
150-
}
151-
152-
/**
153-
* Checks if a given hostname is a hostname of GrieferGames.
154-
* <br><br>
155-
* Following domains are taken into account:
156-
* <br>
157-
* - griefergames.net
158-
* <br>
159-
* - griefergames.de
160-
* <br>
161-
* - griefergames.live
162-
*
163-
* @param hostName The hostname to check.
164-
* @return Returns, whether the given hostname is one of the GrieferGames hostnames.
165-
*/
166-
public static boolean isGrieferGamesHostName(final @NotNull String hostName) {
167-
final String filteredHostName = Optional.of(hostName)
168-
.filter(host -> host.endsWith("."))
169-
.map(host -> host.substring(0, host.length() - 1).toLowerCase(Locale.ENGLISH))
170-
.orElse(hostName.toLowerCase(Locale.ENGLISH));
171-
return filteredHostName.endsWith("griefergames.net") || filteredHostName.endsWith("griefergames.de") || filteredHostName.endsWith("griefergames.live");
172-
}
173-
174138
/**
175139
* Gets a {@link NetworkPlayerInfo} by the uuid of a player.
176140
*

0 commit comments

Comments
 (0)