1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-21 12:15:50 +00:00
Slimefun4/src/main/java/me/mrCookieSlime/Slimefun/Objects/Research.java

296 lines
10 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.Objects;
2020-04-13 18:08:05 +00:00
import java.util.ArrayList;
import java.util.LinkedList;
2016-04-14 16:24:03 +00:00
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
2019-12-12 01:52:24 +00:00
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
2020-03-19 12:14:29 +00:00
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.core.guide.options.SlimefunGuideSettings;
import io.github.thebusybiscuit.slimefun4.core.services.localization.Language;
2020-02-02 17:27:32 +00:00
import io.github.thebusybiscuit.slimefun4.implementation.setup.ResearchSetup;
2020-01-09 23:13:01 +00:00
import io.github.thebusybiscuit.slimefun4.utils.FireworkUtils;
2019-08-31 09:36:45 +00:00
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
2017-06-23 11:06:08 +00:00
import me.mrCookieSlime.Slimefun.api.Slimefun;
2016-04-14 16:24:03 +00:00
2017-06-23 11:06:08 +00:00
/**
* Represents a research, which is bound to one
* {@link SlimefunItem} or more and requires XP levels to unlock said item(s).
2020-02-11 21:22:33 +00:00
*
2017-06-23 11:06:08 +00:00
* @author TheBusyBiscuit
2020-03-07 10:35:29 +00:00
*
* @see ResearchSetup
* @see ResearchUnlockEvent
*
2017-06-23 11:06:08 +00:00
*/
public class Research implements Keyed {
2019-03-27 19:38:30 +00:00
private static final int[] RESEARCH_PROGRESS = { 23, 44, 57, 92 };
private final NamespacedKey key;
private final int id;
private String name;
2020-03-12 02:32:04 +00:00
private boolean enabled = true;
private int cost;
private final List<SlimefunItem> items = new LinkedList<>();
/**
* The constructor for a {@link Research}.
*
* Create a new research, then bind this research to the Slimefun items you want by calling
* {@link #addItems(SlimefunItem...)}. Once you're finished, call {@link #register()}
* to register it.
*
* To speed up, directly setup the research by calling
* {@link Slimefun#registerResearch(Research, org.bukkit.inventory.ItemStack...)}.
*
* @param key
* A unique identifier for this {@link Research}
* @param id
* old way of identifying researches
* @param name
* The displayed name of this {@link Research}
* @param defaultCost
* The Cost in XP levels to unlock this {@link Research}
*
*/
2020-04-25 20:57:46 +00:00
public Research(NamespacedKey key, int id, String name, int defaultCost) {
this.key = key;
this.id = id;
this.name = name;
this.cost = defaultCost;
}
@Override
public NamespacedKey getKey() {
return key;
}
2020-03-07 10:35:29 +00:00
/**
* This method returns whether this {@link Research} is enabled.
* {@code false} can mean that this particular {@link Research} was disabled or that
* researches alltogether have been disabled.
*
* @return Whether this {@link Research} is enabled or not
*/
public boolean isEnabled() {
2020-03-15 16:43:51 +00:00
return SlimefunPlugin.getRegistry().isResearchingEnabled() && enabled;
}
/**
* Gets the ID of this {@link Research}.
* This is the old way of identifying Researches, use a {@link NamespacedKey} in the future.
*
* @return The ID of this {@link Research}
*/
public int getID() {
return id;
}
/**
* This method gives you a localized name for this {@link Research}.
* The name is automatically taken from the currently selected {@link Language} of
* the specified {@link Player}.
*
* @param p
* The {@link Player} to translate this name for.
* @return The localized Name of this {@link Research}.
*/
public String getName(Player p) {
String localized = SlimefunPlugin.getLocal().getResearchName(p, key);
return localized != null ? localized : name;
}
/**
* Gets the cost in XP levels to unlock this {@link Research}.
*
* @return The cost in XP levels for this {@link Research}
*/
public int getCost() {
return cost;
}
/**
* Sets the cost in XP levels to unlock this {@link Research}.
*
* @param cost
* The cost in XP levels
*/
public void setCost(int cost) {
this.cost = cost;
}
/**
* Bind the specified Slimefun items to this {@link Research}.
*
* @param items
* Instances of {@link SlimefunItem} to bind to this {@link Research}
*/
2020-04-25 21:13:00 +00:00
public void addItems(SlimefunItem... items) {
for (SlimefunItem item : items) {
if (item != null) {
2020-04-06 23:01:13 +00:00
item.setResearch(this);
}
}
}
/**
* Lists every {@link SlimefunItem} that is bound to this {@link Research}.
*
* @return The Slimefun items bound to this {@link Research}.
*/
public List<SlimefunItem> getAffectedItems() {
return items;
}
/**
* Checks if the {@link Player} can unlock this {@link Research}.
*
* @param p
* The {@link Player} to check
* @return Whether that {@link Player} can unlock this {@link Research}
*/
public boolean canUnlock(Player p) {
if (!isEnabled()) {
return true;
}
2020-04-13 18:08:05 +00:00
boolean creativeResearch = p.getGameMode() == GameMode.CREATIVE && SlimefunPlugin.getRegistry().isFreeCreativeResearchingEnabled();
2020-04-04 00:45:51 +00:00
return creativeResearch || p.getLevel() >= cost;
}
/**
* Unlocks this {@link Research} for the specified {@link Player}.
*
* @param p
* The {@link Player} for which to unlock this {@link Research}
* @param instant
* Whether to unlock the research instantly
*/
2020-03-10 21:09:19 +00:00
public void unlock(Player p, boolean instant) {
if (!instant) {
Slimefun.runSync(() -> {
p.playSound(p.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 1F);
SlimefunPlugin.getLocal().sendMessage(p, "messages.research.progress", true, msg -> msg.replace("%research%", getName(p)).replace("%progress%", "0%"));
}, 10L);
}
PlayerProfile.get(p, profile -> {
if (!profile.hasUnlocked(this)) {
Runnable runnable = () -> {
profile.setResearched(this, true);
SlimefunPlugin.getLocal().sendMessage(p, "messages.unlocked", true, msg -> msg.replace("%research%", getName(p)));
if (SlimefunPlugin.getRegistry().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) {
FireworkUtils.launchRandom(p, 1);
}
};
Slimefun.runSync(() -> {
ResearchUnlockEvent event = new ResearchUnlockEvent(p, this);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
if (instant) {
runnable.run();
}
else if (SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().add(p.getUniqueId())) {
SlimefunPlugin.getLocal().sendMessage(p, "messages.research.start", true, msg -> msg.replace("%research%", getName(p)));
playResearchAnimation(p);
Slimefun.runSync(() -> {
runnable.run();
SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().remove(p.getUniqueId());
}, (RESEARCH_PROGRESS.length + 1) * 20L);
}
}
});
}
});
}
private void playResearchAnimation(Player p) {
for (int i = 1; i < RESEARCH_PROGRESS.length + 1; i++) {
int j = i;
Slimefun.runSync(() -> {
p.playSound(p.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 1F);
SlimefunPlugin.getLocal().sendMessage(p, "messages.research.progress", true, msg -> msg.replace("%research%", getName(p)).replace("%progress%", RESEARCH_PROGRESS[j - 1] + "%"));
}, i * 20L);
}
}
/**
* Registers this {@link Research}.
*/
public void register() {
SlimefunPlugin.getResearchCfg().setDefaultValue("enable-researching", true);
String path = key.getNamespace() + '.' + key.getKey();
migrate(id, path);
if (SlimefunPlugin.getResearchCfg().contains(path + ".enabled") && !SlimefunPlugin.getResearchCfg().getBoolean(path + ".enabled")) {
2020-04-13 18:08:05 +00:00
for (SlimefunItem item : new ArrayList<>(items)) {
if (item != null) {
item.setResearch(null);
}
}
2020-04-13 18:08:05 +00:00
return;
}
SlimefunPlugin.getResearchCfg().setDefaultValue(path + ".cost", this.getCost());
SlimefunPlugin.getResearchCfg().setDefaultValue(path + ".enabled", true);
this.cost = SlimefunPlugin.getResearchCfg().getInt(path + ".cost");
this.enabled = SlimefunPlugin.getResearchCfg().getBoolean(path + ".enabled");
SlimefunPlugin.getRegistry().getResearches().add(this);
SlimefunPlugin.getRegistry().getResearchIds().add(this);
}
// Temporary migration method from ids to Namespaced Keys.
private void migrate(int id, String path) {
if (SlimefunPlugin.getResearchCfg().contains(id + ".enabled")) {
SlimefunPlugin.getResearchCfg().setValue(path + ".enabled", SlimefunPlugin.getResearchCfg().getBoolean(id + ".enabled"));
}
if (SlimefunPlugin.getResearchCfg().contains(id + ".cost")) {
SlimefunPlugin.getResearchCfg().setValue(path + ".cost", SlimefunPlugin.getResearchCfg().getInt(id + ".cost"));
}
SlimefunPlugin.getResearchCfg().setValue(String.valueOf(id), null);
}
/**
* Attempts to get a {@link Research} with the given ID.
*
* We will use {@link NamespacedKey} for this in the future.
*
* @param id
* ID of the research to get
* @return {@link Research} if found, or null
*/
public static Research getByID(int id) {
for (Research research : SlimefunPlugin.getRegistry().getResearches()) {
2020-03-12 02:32:04 +00:00
if (research.getID() == id) {
return research;
}
}
return null;
}
@Override
public String toString() {
return "Research (" + getKey() + ')';
}
2020-04-25 21:13:00 +00:00
}