Skip to content

Commit

Permalink
🚀 Abilities!
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Feb 13, 2022
1 parent eb20ff7 commit 2807645
Show file tree
Hide file tree
Showing 21 changed files with 942 additions and 32 deletions.
50 changes: 50 additions & 0 deletions src/main/java/info/itsthesky/itemcreator/ItemCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import de.leonhard.storage.Config;
import fr.mrmicky.fastinv.FastInvManager;
import info.itsthesky.itemcreator.api.ItemCreatorAPI;
import info.itsthesky.itemcreator.api.abilities.Ability;
import info.itsthesky.itemcreator.api.abilities.DefaultParameters;
import info.itsthesky.itemcreator.api.abilities.RawAbilityParameter;
import info.itsthesky.itemcreator.api.cooldown.CooldownManager;
import info.itsthesky.itemcreator.api.properties.base.ItemProperty;
import info.itsthesky.itemcreator.core.CreatorCommand;
import info.itsthesky.itemcreator.core.ItemCreatorAPIImpl;
import info.itsthesky.itemcreator.core.MainListener;
import info.itsthesky.itemcreator.core.abilities.ItemThrow;
import info.itsthesky.itemcreator.core.langs.LangLoader;
import info.itsthesky.itemcreator.core.properties.*;
import info.itsthesky.itemcreator.core.properties.attributes.AttributeProperty;
Expand All @@ -22,6 +27,7 @@
import info.itsthesky.itemcreator.core.properties.spawners.SpawnerTypeProperty;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.HashMap;
Expand All @@ -30,7 +36,10 @@ public final class ItemCreator extends JavaPlugin {

private static ItemCreator instance;
private static Config config;
private static CooldownManager cooldownManager;
private HashMap<Integer, ItemProperty> registeredProperties;
private HashMap<Integer, Ability> registeredAbilities;
private HashMap<Integer, RawAbilityParameter> registeredParameters;
private ItemCreatorAPI api;
private LangLoader langLoader;

Expand All @@ -44,11 +53,21 @@ public void onEnable() {
config = new Config(configFile);

FastInvManager.register(this);
cooldownManager = new CooldownManager(this);

instance = this;
api = new ItemCreatorAPIImpl(this);
langLoader = new LangLoader(this, config.getOrSetDefault("default_language", "en_US"));

getLogger().info("Registering parameters ...");
registeredParameters = new HashMap<>();
api.registerParameter(DefaultParameters.ABILITY_FORCE);
api.registerParameter(DefaultParameters.ABILITY_COOLDOWN);

getLogger().info("Registering Abilities ...");
registeredAbilities = new HashMap<>();
api.registerAbility(new ItemThrow(this));

getLogger().info("Registering Properties ...");
registeredProperties = new HashMap<>();
api.registerProperty(new ItemEnabled());
Expand All @@ -71,6 +90,7 @@ public void onEnable() {
api.registerProperty(new PotionEffectsProperty());
api.registerProperty(new AttributeProperty());
api.registerProperty(new CommandsProperty(this));
api.registerProperty(new AbilitiesProperty());

api.registerProperty(new CantCraftProperty());
api.registerProperty(new CantEnchantProperty());
Expand Down Expand Up @@ -103,6 +123,10 @@ public HashMap<Integer, ItemProperty> getRegisteredProperties() {
return registeredProperties;
}

public HashMap<Integer, Ability> getRegisteredAbilities() {
return registeredAbilities;
}

@NotNull
public static Config getConfiguration() {
return config;
Expand All @@ -116,4 +140,30 @@ public void onDisable() {
public LangLoader getLangLoader() {
return langLoader;
}

public @Nullable Ability getAbilityFromId(String id) {
return getRegisteredAbilities()
.values()
.stream()
.filter(ab -> ab.getId().equals(id))
.findAny()
.orElse(null);
}

public @Nullable RawAbilityParameter getParameterFromId(String id) {
return getRegisteredParameters()
.values()
.stream()
.filter(ab -> ab.getId().equals(id))
.findAny()
.orElse(null);
}

public static CooldownManager getCooldownManager() {
return cooldownManager;
}

public HashMap<Integer, RawAbilityParameter> getRegisteredParameters() {
return registeredParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import de.leonhard.storage.Config;
import info.itsthesky.itemcreator.ItemCreator;
import info.itsthesky.itemcreator.api.abilities.Ability;
import info.itsthesky.itemcreator.api.abilities.RawAbilityParameter;
import info.itsthesky.itemcreator.api.properties.base.ItemProperty;
import info.itsthesky.itemcreator.core.CustomItem;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -83,6 +85,10 @@ public interface ItemCreatorAPI {
*/
void registerProperty(ItemProperty property);

void registerAbility(Ability ability);

void registerParameter(RawAbilityParameter parameter);

@NotNull File getItemFile(CustomItem item);

@NotNull File getItemsFolder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package info.itsthesky.itemcreator.api.abilities;

import com.cryptomorin.xseries.XMaterial;
import de.leonhard.storage.Config;
import info.itsthesky.itemcreator.ItemCreator;
import info.itsthesky.itemcreator.api.ISnowflake;
import info.itsthesky.itemcreator.api.cooldown.CooldownManager;
import info.itsthesky.itemcreator.core.CustomItem;
import info.itsthesky.itemcreator.core.GUIRepresentation;
import info.itsthesky.itemcreator.core.langs.LangLoader;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public abstract class Ability<E extends Event> implements Listener, ISnowflake {

public void onGenericEvent(@NotNull E event) {
if (!validate(event))
return;
final Player player = getPlayer(event);
final CustomItem item = getItem(event);
if (item == null)
return;
if (ItemCreator.getCooldownManager().isOnCooldown(player.getUniqueId(), "ability_" + getId())) {
player.sendMessage(LangLoader.get().format("messages.ability_on_cooldown", ItemCreator.getCooldownManager().getUserCooldown(player.getUniqueId(), "ability_" + getId()) / 20));
return;
}
ItemCreator.getCooldownManager().setUserCooldown(player.getUniqueId(), "ability_" + getId(), TimeUnit.SECONDS, getCooldown(item));
launch(event, item, Stream.of(item.getAbilities().get(getId()))
.map(para -> new AbilityParameter(para.getParameter().parse(para.getValue().toString(), null), para.getParameter())).toArray(AbilityParameter[]::new));
};

public abstract @Nullable CustomItem getItem(E event);

public abstract @NotNull Player getPlayer(E event);

public abstract boolean validate(E event);

public abstract XMaterial getMaterial();

public abstract @NotNull RawAbilityParameter<?>[] getParameters();

public abstract void launch(@NotNull E event,
@Nullable CustomItem item,
@NotNull AbilityParameter<?>[] parameters);

public boolean hasCooldown() {
return Arrays.stream(getParameters()).anyMatch(para -> para.getId().equals(DefaultParameters.ABILITY_COOLDOWN.getId()));
}

public @NotNull Integer getCooldown(final CustomItem item) {
if (!hasCooldown())
return 0;
return DefaultParameters.ABILITY_COOLDOWN.parse(
Stream.of(item.getAbilityParameters(this))
.filter(para -> para.getParameter().getId().equals(DefaultParameters.ABILITY_COOLDOWN.getId()))
.map(AbilityParameter::getValue)
.findAny()
.orElse("0")
.toString(), null).intValue();
}

public GUIRepresentation getRepresentation() {
return new GUIRepresentation(getMaterial(), "abilities." + getId() + ".name", "abilities." + getId() + ".lore");
}

public <T> AbilityParameter<T> getParameterValue(RawAbilityParameter<T> raw, CustomItem item) {
final Config config = ItemCreator.getInstance().getApi().getItemConfig(item);
final T value = config.getOrSetDefault("abilities." + getId() + "." + raw.getId(), raw.getDefaultValue());
return new AbilityParameter<>(value, raw);
}

public static class AbilityParameter<T> {

private final T value;
private final RawAbilityParameter<T> parameter;

public AbilityParameter(T value, RawAbilityParameter<T> parameter) {
this.value = value;
this.parameter = parameter;
}

public T getValue() {
return value;
}

public RawAbilityParameter<T> getParameter() {
return parameter;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package info.itsthesky.itemcreator.api.abilities;

import com.cryptomorin.xseries.XMaterial;
import info.itsthesky.itemcreator.core.ParameterImpl;
import info.itsthesky.itemcreator.core.langs.LangLoader;
import org.bukkit.entity.Player;

import java.util.Random;
import java.util.function.BiFunction;

/**
* List of default ability parameters
*/
public final class DefaultParameters {

private static final BiFunction<String, Player, Integer> INTEGER_PARSER = (input, player) -> {
try {
return Integer.parseInt(input);
} catch (Exception ex) {
player.sendMessage(LangLoader.get().format("messages.wrong_number"));
return null;
}
};

public static final RawAbilityParameter<Integer> ABILITY_FORCE =
new ParameterImpl<>("ability_force", XMaterial.GOLDEN_SWORD, 2, INTEGER_PARSER);
public static final RawAbilityParameter<Integer> ABILITY_COOLDOWN =
new ParameterImpl<>("ability_cooldown", XMaterial.CLOCK, 0, INTEGER_PARSER);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package info.itsthesky.itemcreator.api.abilities;

import com.cryptomorin.xseries.XMaterial;
import info.itsthesky.itemcreator.api.ISnowflake;
import info.itsthesky.itemcreator.core.GUIRepresentation;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface RawAbilityParameter<T> extends ISnowflake {

XMaterial getMaterial();

default GUIRepresentation getRepresentation() {
return new GUIRepresentation(getMaterial(),
"abilities.parameters." + getId() + "name",
"abilities.parameters." + getId() + "lore");
}

T getDefaultValue();

T parse(@NotNull String input, @Nullable Player player);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package info.itsthesky.itemcreator.api.abilities;

import info.itsthesky.itemcreator.ItemCreator;
import info.itsthesky.itemcreator.core.CustomItem;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class RightClickAbility extends Ability<PlayerInteractEvent> {

// Check https://spigotmc.org/threads/204644/
@EventHandler(priority = EventPriority.HIGHEST)
public void onEvent(@NotNull PlayerInteractEvent event) {
onGenericEvent(event);
}

@Override
public @Nullable CustomItem getItem(PlayerInteractEvent event) {
final ItemStack stack = event.getItem();
if (stack == null || stack.getType().equals(Material.AIR))
return null;
return ItemCreator.getInstance().getApi().convert(stack);
}

@Override
public boolean validate(PlayerInteractEvent event) {
return event.getAction().equals(Action.RIGHT_CLICK_AIR)
|| event.getAction().equals(Action.RIGHT_CLICK_BLOCK);
}

}
Loading

0 comments on commit 2807645

Please sign in to comment.