Added configs

This commit is contained in:
KäseToatz
2024-07-15 22:58:11 +02:00
parent 450268d399
commit ce1d46971e
6 changed files with 288 additions and 81 deletions

View File

@ -1,90 +1,13 @@
package com.kasetoatz.weathervote;
import com.kasetoatz.weathervote.enums.WeatherType;
import com.kasetoatz.weathervote.commands.Commands;
import com.kasetoatz.weathervote.config.Config;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.HashSet;
import java.util.logging.Logger;
public class WeatherVote implements ModInitializer {
private final HashSet<ServerPlayerEntity> clearVotes = new HashSet<>();
private final HashSet<ServerPlayerEntity> rainVotes = new HashSet<>();
private final HashSet<ServerPlayerEntity> thunderVotes = new HashSet<>();
private static final Logger LOGGER = Logger.getLogger("WeatherVote");
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("weathervote")
.then(CommandManager.literal("clear").executes(context -> {
this.update(context.getSource(), WeatherType.CLEAR);
return 1;
}))
.then(CommandManager.literal("rain").executes(context -> {
this.update(context.getSource(), WeatherType.RAIN);
return 1;
}))
.then(CommandManager.literal("thunder").executes(context -> {
this.update(context.getSource(), WeatherType.THUNDER);
return 1;
}))
));
}
private void update(ServerCommandSource source, WeatherType weather)
{
clearVotes.remove(source.getPlayer());
rainVotes.remove(source.getPlayer());
thunderVotes.remove(source.getPlayer());
switch (weather)
{
case CLEAR:
clearVotes.add(source.getPlayer());
if (clearVotes.size() >= source.getServer().getCurrentPlayerCount() / 2)
{
source.getServer().sendMessage(Text.literal("Weather vote complete. Changing weather to clear."));
source.getWorld().setWeather(6000, 0, false, false);
this.resetVotes();
return;
}
source.sendFeedback(() -> Text.literal("You voted for clear weather. (" + clearVotes.size() + " / " + Math.round((float)source.getServer().getCurrentPlayerCount() / 2) + " required votes."), false);
LOGGER.info(source.getName() + " voted for clear weather.");
break;
case RAIN:
rainVotes.add(source.getPlayer());
if (rainVotes.size() >= source.getServer().getCurrentPlayerCount() / 2)
{
source.getServer().sendMessage(Text.literal("Weather vote complete. Changing weather to rain."));
source.getWorld().setWeather(0, 6000, true, false);
this.resetVotes();
return;
}
source.sendFeedback(() -> Text.literal("You voted for rain. (" + rainVotes.size() + " / " + Math.round((float)source.getServer().getCurrentPlayerCount() / 2) + " required votes."), false);
LOGGER.info(source.getName() + " voted for rain.");
break;
case THUNDER:
thunderVotes.add(source.getPlayer());
if (thunderVotes.size() >= source.getServer().getCurrentPlayerCount() / 2)
{
source.getServer().sendMessage(Text.literal("Weather vote complete. Changing weather to thunder."));
source.getWorld().setWeather(0, 6000, true, true);
this.resetVotes();
return;
}
source.sendFeedback(() -> Text.literal("You voted for thunder. (" + thunderVotes.size() + " / " + Math.round((float)source.getServer().getCurrentPlayerCount() / 2) + " required votes."), false);
LOGGER.info(source.getName() + " voted for thunder.");
break;
}
}
private void resetVotes()
{
clearVotes.clear();
rainVotes.clear();
thunderVotes.clear();
Config.load();
Commands.registerCommands();
}
}

View File

@ -0,0 +1,26 @@
package com.kasetoatz.weathervote.commands;
import com.kasetoatz.weathervote.votes.Votes;
import com.kasetoatz.weathervote.weather.Weather;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
public class Commands {
public static void registerCommands()
{
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("weathervote")
.then(CommandManager.literal("clear").executes(context -> {
Weather.update(Votes.voteClear(context.getSource()));
return 1;
}))
.then(CommandManager.literal("rain").executes(context -> {
Weather.update(Votes.voteRain(context.getSource()));
return 1;
}))
.then(CommandManager.literal("thunder").executes(context -> {
Weather.update(Votes.voteThunder(context.getSource()));
return 1;
}))
));
}
}

View File

@ -0,0 +1,80 @@
package com.kasetoatz.weathervote.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.crash.CrashException;
import net.minecraft.util.crash.CrashReport;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Config {
public static boolean enabled = true;
public static int votingPercentage = 50;
public static String completedVoteMessage = "§2[WeatherVote]§r Voting passed. Changing weather to $weather";
public static String voteMessage = "§2[WeatherVote]§r $name voted for $weather. ($current/$required) required votes.";
public static String disabledMessage = "§cVoting is currently disabled.";
private static final File config = new File(MinecraftClient.getInstance().runDirectory, "config/weathervote.json");
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static void load()
{
if (!config.exists())
{
save();
return;
}
try (FileReader reader = new FileReader(config))
{
JsonObject json = gson.fromJson(reader, JsonObject.class);
if (json.has("enabled"))
{
enabled = json.get("enabled").getAsBoolean();
}
if (json.has("votingPercentage"))
{
votingPercentage = json.get("votingPercentage").getAsInt();
}
if (json.has("completedVoteMessage"))
{
completedVoteMessage = json.get("completedVoteMessage").getAsString();
}
if (json.has("voteMessage"))
{
voteMessage = json.get("voteMessage").getAsString();
}
if (json.has("disabledMessage"))
{
disabledMessage = json.get("disabledMessage").getAsString();
}
save();
}
catch (IOException exc)
{
throw new CrashException(CrashReport.create(exc, "Loading config file."));
}
}
public static void save()
{
JsonObject json = new JsonObject();
json.addProperty("enabled", enabled);
json.addProperty("votingPercentage", votingPercentage);
json.addProperty("completedVoteMessage", completedVoteMessage);
json.addProperty("voteMessage", voteMessage);
json.addProperty("disabledMessage", disabledMessage);
try (FileWriter writer = new FileWriter(config))
{
gson.toJson(json, writer);
}
catch (IOException exc)
{
throw new CrashException(CrashReport.create(exc, "Saving config file."));
}
}
}

View File

@ -0,0 +1,30 @@
package com.kasetoatz.weathervote.votes;
import com.kasetoatz.weathervote.WeatherVote;
import com.kasetoatz.weathervote.enums.WeatherType;
import net.minecraft.server.command.ServerCommandSource;
public class VoteResponse {
private final ServerCommandSource source;
private final WeatherType weather;
private final boolean votePassed;
public VoteResponse(ServerCommandSource source, WeatherType weather, boolean votePassed)
{
this.source = source;
this.weather = weather;
this.votePassed = votePassed;
}
public ServerCommandSource getSource() {
return source;
}
public WeatherType getWeather() {
return weather;
}
public boolean hasVotePassed() {
return votePassed;
}
}

View File

@ -0,0 +1,69 @@
package com.kasetoatz.weathervote.votes;
import com.kasetoatz.weathervote.config.Config;
import com.kasetoatz.weathervote.enums.WeatherType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import java.util.HashSet;
public class Votes {
private static final HashSet<ServerPlayerEntity> clearVotes = new HashSet<>();
private static final HashSet<ServerPlayerEntity> rainVotes = new HashSet<>();
private static final HashSet<ServerPlayerEntity> thunderVotes = new HashSet<>();
private static void removeVote(ServerPlayerEntity player)
{
clearVotes.remove(player);
rainVotes.remove(player);
thunderVotes.remove(player);
}
public static VoteResponse voteClear(ServerCommandSource source)
{
removeVote(source.getPlayer());
clearVotes.add(source.getPlayer());
return new VoteResponse(source, WeatherType.CLEAR, clearVotes.size() >= requiredVotes(source));
}
public static int clearSize()
{
return clearVotes.size();
}
public static VoteResponse voteRain(ServerCommandSource source)
{
removeVote(source.getPlayer());
rainVotes.add(source.getPlayer());
return new VoteResponse(source, WeatherType.RAIN, rainVotes.size() >= requiredVotes(source));
}
public static int rainSize()
{
return rainVotes.size();
}
public static VoteResponse voteThunder(ServerCommandSource source)
{
removeVote(source.getPlayer());
thunderVotes.add(source.getPlayer());
return new VoteResponse(source, WeatherType.THUNDER, thunderVotes.size() >= requiredVotes(source));
}
public static int thunderSize()
{
return thunderVotes.size();
}
public static void removeVotes()
{
clearVotes.clear();
rainVotes.clear();
thunderVotes.clear();
}
public static int requiredVotes(ServerCommandSource source)
{
return Math.round((float)source.getServer().getCurrentPlayerCount() / (100.f / Config.votingPercentage));
}
}

View File

@ -0,0 +1,79 @@
package com.kasetoatz.weathervote.weather;
import com.kasetoatz.weathervote.config.Config;
import com.kasetoatz.weathervote.enums.WeatherType;
import com.kasetoatz.weathervote.votes.VoteResponse;
import com.kasetoatz.weathervote.votes.Votes;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
public class Weather {
public static void update(VoteResponse vote)
{
if (!Config.enabled)
{
vote.getSource().sendFeedback(() -> Text.of(Config.disabledMessage), false);
}
else if (vote.hasVotePassed())
{
Votes.removeVotes();
setWeather(vote.getSource(), vote.getWeather());
}
else
{
incompleteVote(vote.getSource(), vote.getWeather());
}
}
private static void setWeather(ServerCommandSource source, WeatherType weather)
{
switch (weather)
{
case CLEAR:
broadcastVote(source, "clear", -1);
source.getWorld().setWeather(6000, 0, false, false);
break;
case RAIN:
broadcastVote(source, "rain", -1);
source.getWorld().setWeather(0, 6000, true, false);
break;
case THUNDER:
broadcastVote(source, "thunder", -1);
source.getWorld().setWeather(0, 6000, true, true);
break;
}
}
private static void incompleteVote(ServerCommandSource source, WeatherType weather)
{
switch (weather)
{
case CLEAR:
broadcastVote(source, "clear weather", Votes.clearSize());
break;
case RAIN:
broadcastVote(source, "rain", Votes.rainSize());
break;
case THUNDER:
broadcastVote(source, "thunder", Votes.thunderSize());
break;
}
}
private static void broadcastVote(ServerCommandSource source, String weather, int currentVotes)
{
if (source.getPlayer() != null)
{
for (ServerPlayerEntity player : source.getServer().getPlayerManager().getPlayerList())
{
String message = (currentVotes == -1) ? Config.completedVoteMessage : Config.voteMessage;
message = message.replace("$name", source.getPlayer().getName().getString());
message = message.replace("$weather", weather);
message = message.replace("$current", String.valueOf(currentVotes));
message = message.replace("$required", String.valueOf(Votes.requiredVotes(source)));
player.sendMessage(Text.of(message));
}
}
}
}