Skip to content

Commit

Permalink
Pokeodds, debug, configurable interval
Browse files Browse the repository at this point in the history
  • Loading branch information
chasem-dev committed Jan 30, 2024
1 parent 93d9253 commit e2e0e2a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.chasem.cobblemonextras.commands;

import com.cobblemon.mod.common.Cobblemon;
import com.cobblemon.mod.common.api.storage.party.PlayerPartyStore;
import com.cobblemon.mod.common.pokemon.Pokemon;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import dev.chasem.cobblemonextras.CobblemonExtras;
import dev.chasem.cobblemonextras.permissions.CobblemonExtrasPermissions;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import static dev.chasem.cobblemonextras.util.PokemonUtility.getHoverText;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

public class PokeOdds {

public void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// Register /pokeodds to get the current shiny rate no permission required, and /pokeodds setRate <rate> to set the shiny rate, requires permission
dispatcher.register(literal("pokeodds")
.then(literal("setRate")
.requires(source -> CobblemonExtrasPermissions.checkPermission(source, CobblemonExtras.permissions.POKEODDS_PERMISSION))
.then(argument("rate", FloatArgumentType.floatArg(0.0F, 100.0F))
.executes(ctx -> setRate(ctx, FloatArgumentType.getFloat(ctx, "rate")))))
.executes(this::execute));
}

private int setRate(CommandContext<ServerCommandSource> ctx, float rate) {
ctx.getSource().sendMessage(Text.literal("The shiny rate has been set to: ").formatted(Formatting.GOLD)
.append(Text.literal(String.valueOf(rate)).formatted(Formatting.AQUA)));
Cobblemon.config.setShinyRate(rate);
return 1;
}

private int execute(CommandContext<ServerCommandSource> ctx) {
ctx.getSource().sendMessage(Text.literal("The current shiny rate is: ").formatted(Formatting.GOLD)
.append(Text.literal(String.valueOf(Cobblemon.config.getShinyRate())).formatted(Formatting.AQUA)));
return 1;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public class PermissionLevels {
@SerializedName("command.pokeivs") public int COMMAND_POKEIVS_PERMISSION_LEVEL = 2;
@SerializedName("command.emptybox") public int COMMAND_EMPTYBOX_PERMISSION_LEVEL = 2;
@SerializedName("command.pokeshoutall") public int COMMAND_POKESHOUT_ALL_PERMISSION_LEVEL = 2;
@SerializedName("command.pokeodds") public int COMMAND_POKEODDS_PERMISSION_LEVEL = 2;
}

public class ShowcaseConfig {
public boolean isShowcaseEnabled = true;
public String apiSecret = "To start using showcase, please goto https://cobblemonextras.com/showcase";

public int syncIntervalMinutes = 5;
public boolean debug = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CobblemonExtrasPermissions {
public final CobblemonPermission EMPTYBOX_PERMISSION;
public final CobblemonPermission POKESHOUT_ALL_PERMISSION;
public final CobblemonPermission ITEMSHOUT_PERMISSION;
public final CobblemonPermission POKEODDS_PERMISSION;

public CobblemonExtrasPermissions() {
this.COMPSEE_PERMISSION = new CobblemonPermission("cobblemonextras.command.compsee", toPermLevel(CobblemonExtras.config.permissionLevels.COMMAND_COMPSEE_PERMISSION_LEVEL));
Expand All @@ -38,6 +39,7 @@ public CobblemonExtrasPermissions() {
this.EMPTYBOX_PERMISSION = new CobblemonPermission("cobblemonextras.command.emptybox", toPermLevel(CobblemonExtras.config.permissionLevels.COMMAND_EMPTYBOX_PERMISSION_LEVEL));
this.POKESHOUT_ALL_PERMISSION = new CobblemonPermission("cobblemonextras.command.pokeshoutall", toPermLevel(CobblemonExtras.config.permissionLevels.COMMAND_POKESHOUT_ALL_PERMISSION_LEVEL));
this.ITEMSHOUT_PERMISSION = new CobblemonPermission("cobblemonextras.command.itemshout", toPermLevel(CobblemonExtras.config.permissionLevels.COMMAND_ITEMSHOUT_PERMISSION_LEVEL));
this.POKEODDS_PERMISSION = new CobblemonPermission("cobblemonextras.command.pokeodds", toPermLevel(CobblemonExtras.config.permissionLevels.COMMAND_POKEODDS_PERMISSION_LEVEL));
}

public PermissionLevel toPermLevel(int permLevel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public void init() {
CobblemonExtras.INSTANCE.getLogger().info("Failed to enable Showcase");
CobblemonExtras.INSTANCE.getLogger().error("Invalid API Secret, please goto https://cobblemonextras.com/showcase to get your API Secret");
} else {

CobblemonExtras.INSTANCE.getLogger().info("[CobblemonExtras] Showcase will sync every " + CobblemonExtras.config.showcase.syncIntervalMinutes + " minutes.");
// async task to sync players every 5 minutes
showcaseThread = new Thread(() -> {
while (showcaseThread == Thread.currentThread()) {
try {
// Thread.sleep(1000 * 60 * 5);
Thread.sleep(1000 * 30);
Thread.sleep(1000L * 60 * CobblemonExtras.config.showcase.syncIntervalMinutes);
// Get Minecraft Server instance
MinecraftServer server = Cobblemon.INSTANCE.getImplementation().server();
// Sync all players
Expand Down Expand Up @@ -101,18 +102,22 @@ public JsonObject getPokemonJson(Pokemon pokemon) {
JsonElement moveSet3 = moveJson.get("MoveSet3");
if (moveSet0 != null && !moveSet0.isJsonNull()) {
moveSet0.getAsJsonObject().addProperty("MoveName", pokemon.getMoveSet().get(0).getDisplayName().getString());
moveSet0.getAsJsonObject().addProperty("MoveType", pokemon.getMoveSet().get(0).getType().getName());
moveJson.add("MoveSet0", moveSet0);
}
if (moveSet1 != null && !moveSet1.isJsonNull()) {
moveSet1.getAsJsonObject().addProperty("MoveName", pokemon.getMoveSet().get(1).getDisplayName().getString());
moveSet1.getAsJsonObject().addProperty("MoveType", pokemon.getMoveSet().get(1).getType().getName());
moveJson.add("MoveSet1", moveSet1);
}
if (moveSet2 != null && !moveSet2.isJsonNull()) {
moveSet2.getAsJsonObject().addProperty("MoveName", pokemon.getMoveSet().get(2).getDisplayName().getString());
moveSet2.getAsJsonObject().addProperty("MoveType", pokemon.getMoveSet().get(2).getType().getName());
moveJson.add("MoveSet2", moveSet2);
}
if (moveSet3 != null && !moveSet3.isJsonNull()) {
moveSet3.getAsJsonObject().addProperty("MoveName", pokemon.getMoveSet().get(3).getDisplayName().getString());
moveSet3.getAsJsonObject().addProperty("MoveType", pokemon.getMoveSet().get(3).getType().getName());
moveJson.add("MoveSet3", moveSet3);
}
pokemonJson.add("MoveSet", moveJson);
Expand Down Expand Up @@ -180,7 +185,9 @@ public void syncPlayers(ServerPlayerEntity[] player) {
CobblemonExtras.INSTANCE.getLogger().error("Invalid API Secret, please goto https://cobblemonextras.com/showcase to get your API Secret");
return;
}
CobblemonExtras.INSTANCE.getLogger().info("Syncing " + player.length + " players...");
if (CobblemonExtras.config.showcase.debug) {
CobblemonExtras.INSTANCE.getLogger().info("Syncing " + player.length + " players...");
}
// Build JSonArray of player data

JsonObject request = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ object CobblemonExtras {
PokeShoutAll().register(dispatcher)
Showcase().register(dispatcher)
ItemShout().register(dispatcher)
PokeOdds().register(dispatcher)
}

}

0 comments on commit e2e0e2a

Please sign in to comment.