Expansion

The expansions folder in the fishing plugin is vital for extending the functionalities of the plugin. It is structured into three sub-folders, each designated for a specific type of expansion: game extensions, requirement extensions, and action extensions. You can install the expansion jar like PlaceholderAPI expansions

Create a game expansion

Here's an easy example to introduce how to create a basic expansion.

import net.momirealms.customfishing.api.mechanic.game.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;

import java.util.concurrent.ThreadLocalRandom;

public class MyGameExpansion extends GameExpansion {

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public String getAuthor() {
        return "author";
    }

    @Override
    public String getGameType() {
        return "chat_game";
    }

    @Override
    public GameFactory getGameFactory() {
        return new MyGameFactory();
    }

    public static class MyGameFactory implements GameFactory {

        @Override
        public GameInstance setArgs(ConfigurationSection section) {
            var myBarTitles = section.getStringList("title").toArray(new String[0]);
            var myBarSubTitles = section.getStringList("subtitle").toArray(new String[0]);
            var words = section.getStringList("words").toArray(new String[0]);
            return new MyGameInstance(myBarTitles, myBarSubTitles, words);
        }
    }

    public static class MyGameInstance implements GameInstance {

        private final String[] myBarTitles;
        private final String[] myBarSubTitles;
        private final String[] words;

        public MyGameInstance(String[] myBarTitles, String[] myBarSubTitles, String[] words) {
            this.myBarTitles = myBarTitles;
            this.myBarSubTitles = myBarSubTitles;
            this.words = words;
        }

        @Override
        public GamingPlayer start(Player player, FishHook hook, GameSettings settings) {

            return new AbstractGamingPlayer(player, hook, settings) {

                private final String title = myBarTitles[ThreadLocalRandom.current().nextInt(myBarTitles.length)];
                private final String subTitle = myBarSubTitles[ThreadLocalRandom.current().nextInt(myBarSubTitles.length)];
                private final String word = words[ThreadLocalRandom.current().nextInt(words.length)];

                @Override
                public void onTick() {
                    player.sendTitle(title.replace("{word}", word), subTitle, 0, 100, 0);
                }
                
                @Override
                public boolean onChat(String message) {
                    if (message.equals(word)) {
                        setGameResult(true);
                        endGame();
                    }
                    return true;
                }
            };
        }
    }
}
chat_game_1:
  game-type: chat_game
  title:
    - 'Type in {word} to fish'
  subtitle:
    - 'This is subtitle'
  words:
    - 'Apple'
    - 'Zombie'
    - 'Diamond'

chat_game_2:
  game-type: chat_game
  title:
    - 'Type in {word} to fish'
  subtitle:
    - 'This is another game''s subtitle'
  words:
    - 'Banana'
    - 'Skeleton'
    - 'Emerald'

GameExpansion

This is the primary expansion class

  • getVersion(): Returns the version of this expansion.

  • getAuthor(): Returns the name of the expansion's author.

  • getGameType(): Specifies the type of game, in this case, "chat_game".

  • getGameFactory(): Produces a new instance of MyGameFactory.

GameFactory

This class is responsible for creating a new game instance.

  • setArgs(ConfigurationSection section): This method extracts segments from the configuration file and supplies the data for a new MyGameInstance.

GameInstance

This represents the core instance of the game and is responsible for all game logic.

  • Constructor: Initializes the titles, subtitles, and words.

  • start(Player player, FishHook hook, GameSettings settings): This method initiates the game for the given player. Upon starting the game for a player, it randomizes and displays titles, subtitles, and a word on their screen. If the player correctly types the word in chat, the game concludes.

default games

Create a requirement expansion

import net.momirealms.customfishing.api.mechanic.action.Action;
import net.momirealms.customfishing.api.mechanic.condition.Condition;
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
import net.momirealms.customfishing.api.mechanic.requirement.RequirementExpansion;
import net.momirealms.customfishing.api.mechanic.requirement.RequirementFactory;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;

import java.util.List;

public class MyRequirementExpansion extends RequirementExpansion {

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public String getAuthor() {
        return "author";
    }

    @Override
    public String getRequirementType() {
        return "xz_range";
    }

    @Override
    public RequirementFactory getRequirementFactory() {
        return new MyRequirementFactory();
    }

    public static class MyRequirementFactory implements RequirementFactory {

        @Override
        public Requirement build(Object args, List<Action> notMetActions, boolean checkAction) {
            if (args instanceof ConfigurationSection section) {

                return new Requirement() {
                    private final int xMin = section.getInt("xMin");
                    private final int xMax = section.getInt("xMax");
                    private final int zMin = section.getInt("zMin");
                    private final int zMax = section.getInt("zMax");

                    @Override
                    public boolean isConditionMet(Condition condition) {
                        Location location = condition.getLocation();
                        if (location.getX() <= xMax
                            && location.getX() >= xMin
                            && location.getZ() <= zMax
                            && location.getZ() >= zMin) {
                            return true;
                        }
                        if (checkAction && notMetActions != null) {
                            for (Action action : notMetActions) {
                                action.trigger(condition);
                            }
                        }
                        return false;
                    }
                };
            }
            throw new RuntimeException("Illegal arguments");
        }
    }
}
xz_range:
  xMin: -100
  xMax: 100
  zMin: -100
  zMax: 100

requirement_1:
  type: xz_range
  value:
    xMin: -100
    xMax: 100
    zMin: -100
    zMax: 100
  not-met-actions:
    ...

Create an action expansion

import net.momirealms.customfishing.api.mechanic.action.Action;
import net.momirealms.customfishing.api.mechanic.action.ActionExpansion;
import net.momirealms.customfishing.api.mechanic.action.ActionFactory;
import net.momirealms.customfishing.api.mechanic.condition.Condition;
import org.bukkit.Bukkit;

import java.util.Optional;

public class MyActionExpansion extends ActionExpansion {
    
    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public String getAuthor() {
        return "author";
    }

    @Override
    public String getActionType() {
        return "kill_player";
    }

    @Override
    public ActionFactory getActionFactory() {
        return new ActionFactory() {
            
            private String player;
            
            @Override
            public Action build(Object args, double chance) {
                
                this.player = (String) args;
                
                return new Action() {
                    @Override
                    public void trigger(Condition condition) {
                        if (Math.random() > chance) {
                            return;
                        }
                        if (player.equals("@a")) {
                            Bukkit.getOnlinePlayers().forEach(it -> it.setHealth(0));
                        } else if (player.equals("@s")) {
                            condition.getPlayer().setHealth(0);
                        } else  {
                            Optional.ofNullable(Bukkit.getPlayer(player)).ifPresent(it -> it.setHealth(0));
                        }
                    }
                };
            }
        };
    }
}
action_1:
  type: kill_player
  value: '@a'

Last updated