Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Polymeta committed Apr 8, 2023
0 parents commit 0d2051f
Show file tree
Hide file tree
Showing 24 changed files with 1,074 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
build/
*.ipr
run/
*.iws
out/
*.iml
.gradle/
output/
bin/
libs/

.classpath
.project
.idea/
classes/
.metadata
.vscode
.settings
*.launch
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Polymeta

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Pokegift

Gift your Pokémon to other players! Works on both fabric and forge

## Commands & Permissions

| Command | Permission |
|-----------------------------------------------------|--------------------------------|
| `/pokegift <slot> <player> [--confirm]` OR `/pgift` | `pokegift.command.gift.base` |
| Bypass pgift cooldown | `pokegift.command.gift.bypass` |

## Config explanation

### General

- `cooldownEnabled` - Whether to enable cool-downs on the pokegift command
- `cooldown` - Cool-down in **MINUTES**. Only used if above value is set to `true`
- `blacklist` - A list of Pokémon properties that can not be gifted, an example entry would be "cobblemon:charmander", but you can even get more complex as we use the Pokémon properties under the hood.

### Message Config

As a preface, this plugin uses [MiniMessage](https://docs.advntr.dev/minimessage/format.html) to parse these messages.
It's a powerful api allowing for various formatting options for you as user.
Refer to the default messages to see what placeholders are allowed where.

- `pokegiftFeedback` - The confirmation question that gets sent to the player when they do /pokegift without confirmation
- be sure to leave the `<giftconfirm>` tag in as everything in that allows the player to click it to confirm the gift
- `cooldownFeedback` - message that gets sent when the player is on cooldown
- `pokemonNotAllowed` - message that gets sent when a player attempts to gift a forbidden Pokémon
- `successFeedback` - message that get sent on successful gift
- `errorCantGiftYourself` - error message that gets displayed when a player attempts to gift to themselves
- `errorCouldntTakePokemon` - generic error message indicating something went wrong whilst taking the Pokémon away from the gifter
- `errorCouldntGivePokemon` - generic error message indicating something went wrong whilst giving the Pokémon to the target player
26 changes: 26 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id("java")
id("java-library")
kotlin("jvm") version("1.8.0")

id("dev.architectury.loom") version("1.1-SNAPSHOT") apply false
id("architectury-plugin") version("3.4-SNAPSHOT") apply false
}

allprojects {
apply(plugin = "java")
apply(plugin = "org.jetbrains.kotlin.jvm")

version = project.properties["mod_version"]!!
group = project.properties["maven_group"]!!

repositories {
mavenCentral()
maven(url = "https://dl.cloudsmith.io/public/geckolib3/geckolib/maven/")
maven("https://maven.impactdev.net/repository/development/")
}

java {
withSourcesJar()
}
}
27 changes: 27 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id("dev.architectury.loom")
id("architectury-plugin")
}


architectury {
common("forge", "fabric")
}

loom {
silentMojangMappingsLicense()
}

dependencies {
minecraft("com.mojang:minecraft:${property("minecraft_version")}")
// The following line declares the mojmap mappings, you may use other mappings as well
mappings(loom.officialMojangMappings())
// We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies
// Do NOT use other classes from fabric loader
modImplementation("net.fabricmc:fabric-loader:${property("fabric_loader_version")}")
implementation("net.kyori:adventure-text-minimessage:${property("minimessage_version")}")
implementation("net.kyori:adventure-text-serializer-gson:${property("minimessage_version")}")
// Remove the next line if you don't want to depend on the API
modApi("dev.architectury:architectury:${property("architectury_version")}") { isTransitive = false }
modImplementation("com.cobblemon:mod:1.3.1+1.19.2-SNAPSHOT") { isTransitive = false }
}
105 changes: 105 additions & 0 deletions common/src/main/java/io/github/polymeta/pokegift/Pokegift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.github.polymeta.pokegift;

import dev.architectury.event.events.common.CommandRegistrationEvent;
import io.github.polymeta.pokegift.commands.Gift;
import io.github.polymeta.pokegift.configuration.BaseConfig;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;


public class Pokegift {
public static final String MOD_ID = "pokegift";
public static final MiniMessage miniMessage = MiniMessage.miniMessage();

public static BaseConfig config;
public static ScheduledThreadPoolExecutor scheduler;
public static ForkJoinPool worker;

private static final Logger logger = LogManager.getLogger();

public static void init() {
logger.info("Pokegift by Polymeta starting up!");
scheduler = new ScheduledThreadPoolExecutor(1, r -> {
Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setName("Pokegift Thread");
return thread;
});
scheduler.setRemoveOnCancelPolicy(true);
scheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
worker = new ForkJoinPool(16, new WorkerThreadFactory(), new ExceptionHandler(), false);
//load config and pool and message configuration
loadConfig();

CommandRegistrationEvent.EVENT.register((dispatcher, registry, selection) -> {
Gift.register(dispatcher);
});
}

private static void loadConfig() {
var configFile = new File("config/pokegift/main.json");
configFile.getParentFile().mkdirs();

// Check config existence and load if it exists, otherwise create default.
if (configFile.exists()) {
try {
var fileReader = new FileReader(configFile);
config = BaseConfig.GSON.fromJson(fileReader, BaseConfig.class);
fileReader.close();
} catch (Exception e) {
logger.error("Failed to load the config! Using default config as fallback");
e.printStackTrace();
config = new BaseConfig();
}

} else {
config = new BaseConfig();
}

saveConfig();
}

private static void saveConfig() {
try {
var configFile = new File("config/pokegift/main.json");
var fileWriter = new FileWriter(configFile);
BaseConfig.GSON.toJson(config, fileWriter);
fileWriter.flush();
fileWriter.close();
} catch (Exception e) {
logger.error("Failed to save the config!");
e.printStackTrace();
}
}

private static final class WorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
private static final AtomicInteger COUNT = new AtomicInteger(0);

@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
thread.setDaemon(true);
thread.setName("Pokegift Worker - " + COUNT.getAndIncrement());
thread.setContextClassLoader(Pokegift.class.getClassLoader());
return thread;
}
}

private static final class ExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.error("Thread " + t.getName() + " threw an uncaught exception");
e.printStackTrace();
}
}
}
121 changes: 121 additions & 0 deletions common/src/main/java/io/github/polymeta/pokegift/commands/Gift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package io.github.polymeta.pokegift.commands;

import com.cobblemon.mod.common.Cobblemon;
import com.cobblemon.mod.common.api.permission.CobblemonPermission;
import com.cobblemon.mod.common.api.permission.PermissionLevel;
import com.cobblemon.mod.common.api.pokemon.PokemonProperties;
import com.cobblemon.mod.common.api.pokemon.PokemonPropertyExtractor;
import com.cobblemon.mod.common.api.storage.party.PartyPosition;
import com.cobblemon.mod.common.command.argument.PartySlotArgumentType;
import com.cobblemon.mod.common.pokemon.Pokemon;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.polymeta.pokegift.Pokegift;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;

import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;

public class Gift {

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
var giftCommand = dispatcher.register(
LiteralArgumentBuilder.<CommandSourceStack>literal("pokegift")
.requires(req -> Cobblemon.INSTANCE.getPermissionValidator().hasPermission(req,
new CobblemonPermission("pokegift.command.gift.base", PermissionLevel.NONE)))
.then(Commands.argument("slot", PartySlotArgumentType.Companion.partySlot())
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("confirmation", StringArgumentType.greedyString()).executes(ExecuteWithConfirm))
.executes(Execute)))
);
dispatcher.register(LiteralArgumentBuilder.<CommandSourceStack>literal("pgift").redirect(giftCommand));
}

private static final ConcurrentSkipListSet<UUID> playersOnCooldown = new ConcurrentSkipListSet<>();

private static final Command<CommandSourceStack> Execute = context -> {
var slot = PartySlotArgumentType.Companion.getPokemon(context, "slot");
var player = context.getSource().getPlayerOrException();
var targetPlayer = EntityArgument.getPlayer(context, "player");
if(player.getUUID().equals(targetPlayer.getUUID())) {
player.sendSystemMessage(Pokegift.config.messages.errorCantGiftYourself());
return Command.SINGLE_SUCCESS;
}
var slotNo = ((PartyPosition)slot.getStoreCoordinates().get().getPosition()).getSlot();
var canBypass = Cobblemon.INSTANCE.getPermissionValidator().hasPermission(player,
new CobblemonPermission("pokegift.command.gift.bypass",
PermissionLevel.CHEAT_COMMANDS_AND_COMMAND_BLOCKS));
if(playersOnCooldown.contains(player.getUUID()) && !canBypass && Pokegift.config.cooldownEnabled) {
player.sendSystemMessage(Pokegift.config.messages.cooldownFeedback());
return Command.SINGLE_SUCCESS;
}
if(isPokemonForbidden(slot) && !canBypass) {
player.sendSystemMessage(Pokegift.config.messages.pokemonNotAllowed());
return Command.SINGLE_SUCCESS;
}
player.sendSystemMessage(Pokegift.config.messages.pokegiftFeedback(slot, slotNo, targetPlayer));

return Command.SINGLE_SUCCESS;
};

private static final Command<CommandSourceStack> ExecuteWithConfirm = context -> {
var slot = PartySlotArgumentType.Companion.getPokemon(context, "slot");
var confirmation = StringArgumentType.getString(context, "confirmation");
if(!confirmation.trim().equals("--confirm")){
return Execute.run(context);
}
var player = context.getSource().getPlayerOrException();
var targetPlayer = EntityArgument.getPlayer(context, "player");
if(player.getUUID().equals(targetPlayer.getUUID())) {
player.sendSystemMessage(Pokegift.config.messages.errorCantGiftYourself());
return Command.SINGLE_SUCCESS;
}
var canBypass = Cobblemon.INSTANCE.getPermissionValidator().hasPermission(player,
new CobblemonPermission("pokegift.command.gift.bypass",
PermissionLevel.CHEAT_COMMANDS_AND_COMMAND_BLOCKS));
if(playersOnCooldown.contains(player.getUUID()) && !canBypass && Pokegift.config.cooldownEnabled) {
player.sendSystemMessage(Pokegift.config.messages.cooldownFeedback());
return Command.SINGLE_SUCCESS;
}
if(isPokemonForbidden(slot) && !canBypass) {
player.sendSystemMessage(Pokegift.config.messages.pokemonNotAllowed());
return Command.SINGLE_SUCCESS;
}
var playerParty = Cobblemon.INSTANCE.getStorage().getParty(player);
var targetParty = Cobblemon.INSTANCE.getStorage().getParty(targetPlayer);
if(playerParty.remove(slot)) {
if(!targetParty.add(slot)) {
player.sendSystemMessage(Pokegift.config.messages.errorCouldntGivePokemon(targetPlayer));
playerParty.add(slot); //give pokemon back if something went wrong
return Command.SINGLE_SUCCESS;
}
}
else {
player.sendSystemMessage(Pokegift.config.messages.errorCouldntTakePokemon());
return Command.SINGLE_SUCCESS;
}

if(Pokegift.config.cooldownEnabled && !canBypass) {
playersOnCooldown.add(player.getUUID());
Pokegift.scheduler.schedule(() -> {playersOnCooldown.remove(player.getUUID());}, Pokegift.config.cooldown, TimeUnit.MINUTES);
}
player.sendSystemMessage(Pokegift.config.messages.successFeedback());
return Command.SINGLE_SUCCESS;
};

private static boolean isPokemonForbidden(Pokemon pokemon) {
for (String property : Pokegift.config.blacklist) {
if(PokemonProperties.Companion.parse(property, " ", "=").matches(pokemon)) {
return true;
}
}
return false;
}

}
Loading

0 comments on commit 0d2051f

Please sign in to comment.