Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CmdrJane committed Dec 18, 2023
1 parent 2ee7167 commit 8f62abb
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 192 deletions.
30 changes: 15 additions & 15 deletions src/main/java/ru/aiefu/timeandwindct/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
import java.util.HashMap;

public class ConfigurationManager {
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static final Gson gson_pretty = new GsonBuilder().setPrettyPrinting().create();
public static void genTimeData(){
try(FileWriter writer = getFileWriter("./config/time-and-wind/time-data.json")) {
gson.toJson(new HashMapOf<>("minecraft:overworld", new TimeDataStorage()), writer);
gson_pretty.toJson(new HashMapOf<>("minecraft:overworld", new TimeDataStorage()), writer);
} catch (IOException e){
e.printStackTrace();
}
}

public static void generateSysTimeCfg(){
try(FileWriter writer = getFileWriter("./config/time-and-wind/system-time-data-global.json")) {
gson.toJson(new SystemTimeConfig("5:00", "20:00", "3:00"), writer);
gson_pretty.toJson(new SystemTimeConfig("5:00", "20:00", "3:00"), writer);
} catch (IOException e){
e.printStackTrace();
}
}

public static void generateMapSysTime(){
try(FileWriter writer = getFileWriter("./config/time-and-wind/system-time-data.json")) {
gson.toJson(new HashMapOf<>("minecraft:overworld", new SystemTimeConfig("5:00", "20:00", "3:00")), writer);
gson_pretty.toJson(new HashMapOf<>("minecraft:overworld", new SystemTimeConfig("5:00", "20:00", "3:00")), writer);
} catch (IOException e){
e.printStackTrace();
}
Expand All @@ -42,7 +42,7 @@ public static void generateMapSysTime(){

public static void updateModConfig(ModConfig config){
try(FileWriter writer = getFileWriter("./config/time-and-wind/config.json")) {
gson.toJson(config, writer);
gson_pretty.toJson(config, writer);
} catch (IOException e){
e.printStackTrace();
}
Expand All @@ -51,7 +51,7 @@ public static void updateModConfig(ModConfig config){
public static void updateMapSysTime(String worldId, String sunrise, String sunset, String timeZone){
TimeAndWindCT.sysTimeMap.put(worldId, new SystemTimeConfig(sunrise, sunset, timeZone));
try(FileWriter writer = getFileWriter("./config/time-and-wind/system-time-data.json")) {
gson.toJson(TimeAndWindCT.sysTimeMap, writer);
gson_pretty.toJson(TimeAndWindCT.sysTimeMap, writer);
} catch (IOException e){
e.printStackTrace();
}
Expand All @@ -60,7 +60,7 @@ public static void updateMapSysTime(String worldId, String sunrise, String sunse
public static void updateGlobalSysTimeCfg(String sunrise, String sunset, String timezone){
TimeAndWindCT.systemTimeConfig = new SystemTimeConfig(sunrise, sunset, timezone);
try(FileWriter writer = getFileWriter("./config/time-and-wind/system-time-data-global.json")) {
gson.toJson(TimeAndWindCT.systemTimeConfig, writer);
gson_pretty.toJson(TimeAndWindCT.systemTimeConfig, writer);
} catch (IOException e){
e.printStackTrace();
}
Expand All @@ -69,7 +69,7 @@ public static void updateGlobalSysTimeCfg(String sunrise, String sunset, String
public static SystemTimeConfig readGlobalSysTimeCfg(){
SystemTimeConfig config;
try {
config = gson.fromJson(new FileReader("./config/time-and-wind/system-time-data-global.json"), SystemTimeConfig.class);
config = gson_pretty.fromJson(new FileReader("./config/time-and-wind/system-time-data-global.json"), SystemTimeConfig.class);
} catch (IOException e){
e.printStackTrace();
config = new SystemTimeConfig("7:00", "19:00", "local");
Expand All @@ -80,7 +80,7 @@ public static SystemTimeConfig readGlobalSysTimeCfg(){
public static HashMap<String, SystemTimeConfig> readSysTimeCfg(){
HashMap<String, SystemTimeConfig> map;
try {
map = gson.fromJson(new FileReader("./config/time-and-wind/system-time-data.json"), new TypeToken<HashMap<String, SystemTimeConfig>>(){}.getType());
map = gson_pretty.fromJson(new FileReader("./config/time-and-wind/system-time-data.json"), new TypeToken<HashMap<String, SystemTimeConfig>>(){}.getType());
} catch (IOException e){
e.printStackTrace();
map = new HashMapOf<>("minecraft:overworld", new SystemTimeConfig("5:00", "20:00", "3:00"));
Expand All @@ -90,7 +90,7 @@ public static HashMap<String, SystemTimeConfig> readSysTimeCfg(){

public static void generateModConfig(){
try(FileWriter writer = getFileWriter("./config/time-and-wind/config.json")){
gson.toJson(new ModConfig(false, false, false,
gson_pretty.toJson(new ModConfig(false, false, false,
false, 30, true, 50), writer);
} catch (IOException e){
e.printStackTrace();
Expand All @@ -100,11 +100,11 @@ public static void generateModConfig(){
public static ModConfig readModConfig(){
ModConfig config;
try {
config = gson.fromJson(new FileReader("./config/time-and-wind/config.json"), ModConfig.class);
config = gson_pretty.fromJson(new FileReader("./config/time-and-wind/config.json"), ModConfig.class);
if(config.config_ver < 3){
config = patchModConfigV1(config);
try(FileWriter writer = getFileWriter("./config/time-and-wind/config.json")) {
gson.toJson(config, writer);
gson_pretty.toJson(config, writer);
} catch (IOException e){
e.printStackTrace();
}
Expand All @@ -124,15 +124,15 @@ public static ModConfig patchModConfigV1(ModConfig config){
public static void updateTimeData(String id, int dayD, int nightD){
TimeAndWindCT.timeDataMap.put(id, new TimeDataStorage(dayD, nightD));
try(FileWriter writer = getFileWriter("./config/time-and-wind/time-data.json")) {
gson.toJson(TimeAndWindCT.timeDataMap, writer);
gson_pretty.toJson(TimeAndWindCT.timeDataMap, writer);
} catch (IOException e){
e.printStackTrace();
}
}

public static void updateTimeData() {
try(FileWriter writer = getFileWriter("./config/time-and-wind/time-data.json")) {
gson.toJson(TimeAndWindCT.timeDataMap, writer);
gson_pretty.toJson(TimeAndWindCT.timeDataMap, writer);
} catch (IOException e){
e.printStackTrace();
}
Expand All @@ -142,7 +142,7 @@ public static int readTimeData(){
HashMap<String, TimeDataStorage> timeDataMap;
int result;
try {
timeDataMap = gson.fromJson(new FileReader("./config/time-and-wind/time-data.json"), new TypeToken<HashMap<String, TimeDataStorage>>(){}.getType());
timeDataMap = gson_pretty.fromJson(new FileReader("./config/time-and-wind/time-data.json"), new TypeToken<HashMap<String, TimeDataStorage>>(){}.getType());
result = 1;
} catch (IOException e){
e.printStackTrace();
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/ru/aiefu/timeandwindct/HashMapOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,4 @@ public class HashMapOf<K, V> extends HashMap<K, V> {
public HashMapOf(K k1, V v1){
putIfAbsent(k1, v1);
}
public HashMapOf(K k1, V v1, K k2, V v2){
putIfAbsent(k1, v1);
putIfAbsent(k2, v2);
}
public HashMapOf(K k1, V v1, K k2, V v2, K k3, V v3){
putIfAbsent(k1, v1);
putIfAbsent(k2, v2);
putIfAbsent(k3, v3);
}
public HashMapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4){
putIfAbsent(k1, v1);
putIfAbsent(k2, v2);
putIfAbsent(k3, v3);
putIfAbsent(k4, v4);
}
public HashMapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5){
putIfAbsent(k1, v1);
putIfAbsent(k2, v2);
putIfAbsent(k3, v3);
putIfAbsent(k4, v4);
putIfAbsent(k5, v5);
}
}
19 changes: 9 additions & 10 deletions src/main/java/ru/aiefu/timeandwindct/ITimeOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import ru.aiefu.timeandwindct.tickers.Ticker;

public interface ITimeOperations {
Ticker getTimeTicker();
void setTimeTicker(Ticker timeTicker);
void setTimeOfDayTAW(long time);
long getTimeTAW();
long getTimeOfDayTAW();
boolean isClient();
void setSkipState(boolean bl);
void setSpeed(int speed);
float getPrevSkyAngle();
void wakeUpAllPlayersTAW();
Ticker time_and_wind_custom_ticker$getTimeTicker();
void time_and_wind_custom_ticker$setTimeTicker(Ticker timeTicker);
void time_and_wind_custom_ticker$setTimeOfDayTAW(long time);
long time_and_wind_custom_ticker$getTimeTAW();
long time_and_wind_custom_ticker$getTimeOfDayTAW();
boolean time_and_wind_custom_ticker$isClient();
void time_and_wind_custom_ticker$setSkipState(boolean bl);
void time_and_wind_custom_ticker$setSpeed(int speed);
void time_and_wind_custom_ticker$wakeUpAllPlayersTAW();
}
12 changes: 6 additions & 6 deletions src/main/java/ru/aiefu/timeandwindct/TAWCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ private static int reloadCfg(CommandSourceStack source) throws CommandSyntaxExce
String id = serverWorld.dimension().location().toString();
if(TimeAndWindCT.CONFIG.syncWithSystemTime){
if(TimeAndWindCT.CONFIG.systemTimePerDimensions && TimeAndWindCT.sysTimeMap.containsKey(id))
((ITimeOperations) serverWorld).setTimeTicker(new SystemTimeTicker((ITimeOperations) serverWorld, TimeAndWindCT.sysTimeMap.get(id)));
else ((ITimeOperations) serverWorld).setTimeTicker(new SystemTimeTicker((ITimeOperations) serverWorld, TimeAndWindCT.systemTimeConfig));
((ITimeOperations) serverWorld).time_and_wind_custom_ticker$setTimeTicker(new SystemTimeTicker((ITimeOperations) serverWorld, TimeAndWindCT.sysTimeMap.get(id)));
else ((ITimeOperations) serverWorld).time_and_wind_custom_ticker$setTimeTicker(new SystemTimeTicker((ITimeOperations) serverWorld, TimeAndWindCT.systemTimeConfig));
}
else if (TimeAndWindCT.timeDataMap.containsKey(id)) {
TimeDataStorage storage = TimeAndWindCT.timeDataMap.get(id);
((ITimeOperations) serverWorld).setTimeTicker(new TimeTicker(storage.dayDuration, storage.nightDuration, serverWorld));
} else ((ITimeOperations) serverWorld).setTimeTicker(new DefaultTicker());
((ITimeOperations) serverWorld).time_and_wind_custom_ticker$setTimeTicker(new TimeTicker(storage.dayDuration, storage.nightDuration, serverWorld));
} else ((ITimeOperations) serverWorld).time_and_wind_custom_ticker$setTimeTicker(new DefaultTicker());
}
for(ServerPlayer player : server.getPlayerList().getPlayers()){
TimeAndWindCT.sendConfigSyncPacket(player);
Expand Down Expand Up @@ -210,7 +210,7 @@ private static int parseWorldsIds(CommandSourceStack source) throws CommandSynta
List<String> ids = new ArrayList<>();
source.getServer().getAllLevels().forEach(serverWorld -> ids.add(serverWorld.dimension().location().toString()));
try(FileWriter writer = ConfigurationManager.getFileWriter("./taw-worlds-ids.json")) {
ConfigurationManager.gson.toJson(ids, writer);
ConfigurationManager.gson_pretty.toJson(ids, writer);
} catch (IOException e){
e.printStackTrace();
}
Expand Down Expand Up @@ -245,7 +245,7 @@ private static int getTimeConfig(CommandSourceStack source) throws CommandSyntax

private static int getTimeChecker(CommandSourceStack source) throws CommandSyntaxException {
if(TimeAndWindCT.debugMode || source.hasPermission(4) || source.getServer().isSingleplayerOwner(source.getPlayerOrException().getGameProfile())) {
Ticker t = ((ITimeOperations) source.getLevel()).getTimeTicker();
Ticker t = ((ITimeOperations) source.getLevel()).time_and_wind_custom_ticker$getTimeTicker();
if(t instanceof TimeTicker ticker) {
source.sendSuccess(() -> Component.literal("Day: " + ticker.getDayD() + " Night: " + ticker.getNightD()), false);
source.sendSuccess(() -> Component.literal("Day Mod: " + ticker.getDayMod() + " Night Mod: " + ticker.getNightMod()), false);
Expand Down
41 changes: 16 additions & 25 deletions src/main/java/ru/aiefu/timeandwindct/TimeAndWindCT.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.aiefu.timeandwindct;

import com.google.gson.Gson;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
Expand All @@ -15,9 +14,8 @@
import ru.aiefu.timeandwindct.config.SystemTimeConfig;
import ru.aiefu.timeandwindct.config.TimeDataStorage;

import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Objects;
Expand Down Expand Up @@ -47,28 +45,21 @@ public void onInitialize() {
}

public void craftPaths(){
try{
Path path = Paths.get("./config/time-and-wind");
if(!Files.isDirectory(path)){
Files.createDirectories(path);
}
if(!Files.exists(Paths.get("./config/time-and-wind/time-data.json"))){
ConfigurationManager.genTimeData();
}
if(!Files.exists(Paths.get("./config/time-and-wind/config.json"))){
ConfigurationManager.generateModConfig();
}
if(!Files.exists(Paths.get("./config/time-and-wind/system-time-data-global.json"))){
ConfigurationManager.generateSysTimeCfg();
}
if(!Files.exists(Paths.get("./config/time-and-wind/system-time-data.json"))){
ConfigurationManager.generateMapSysTime();
}
CONFIG = ConfigurationManager.readModConfig();
File file = new File("./config/time-and-wind");
file.mkdirs();
if(!Files.exists(Paths.get("./config/time-and-wind/time-data.json"))){
ConfigurationManager.genTimeData();
}
catch (IOException e){
e.printStackTrace();
if(!Files.exists(Paths.get("./config/time-and-wind/config.json"))){
ConfigurationManager.generateModConfig();
}
if(!Files.exists(Paths.get("./config/time-and-wind/system-time-data-global.json"))){
ConfigurationManager.generateSysTimeCfg();
}
if(!Files.exists(Paths.get("./config/time-and-wind/system-time-data.json"))){
ConfigurationManager.generateMapSysTime();
}
CONFIG = ConfigurationManager.readModConfig();
}

public static String getFormattedTime(long ms){
Expand All @@ -84,8 +75,8 @@ public static void sendConfigSyncPacket(ServerPlayer player){
if(!Objects.requireNonNull(player.getServer()).isSingleplayerOwner(player.getGameProfile())) {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());

String cfgJson = new Gson().toJson(TimeAndWindCT.CONFIG);
String cfgsJson = new Gson().toJson(TimeAndWindCT.systemTimeConfig);
String cfgJson = ConfigurationManager.gson_pretty.toJson(TimeAndWindCT.CONFIG);
String cfgsJson = ConfigurationManager.gson_pretty.toJson(TimeAndWindCT.systemTimeConfig);

buf.writeUtf(cfgJson);
buf.writeUtf(cfgsJson);
Expand Down
Loading

0 comments on commit 8f62abb

Please sign in to comment.