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

410 lines
11 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.Objects;
import java.util.ArrayList;
import java.util.Collection;
2016-04-14 16:24:03 +00:00
import java.util.Iterator;
import java.util.LinkedList;
2016-04-14 16:24:03 +00:00
import java.util.List;
import java.util.UUID;
2019-08-31 15:52:20 +00:00
import java.util.logging.Level;
2016-04-14 16:24:03 +00:00
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
2019-12-12 01:52:24 +00:00
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
import io.github.thebusybiscuit.slimefun4.core.SlimefunRegistry;
import io.github.thebusybiscuit.slimefun4.core.guide.GuideSettings;
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;
import me.mrCookieSlime.Slimefun.api.PlayerProfile;
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
/**
2017-06-23 16:01:44 +00:00
* Statically handles researches. Represents a research, which is bound to one
* {@link SlimefunItem} or more and require XP levels to unlock this/these item(s).
2017-06-23 11:06:08 +00:00
* <p>
* See {@link #Research(int, String, int)} to create a research.
2017-06-23 16:01:44 +00:00
* <p>
* See {@link ResearchSetup} for the built-in researches.
2017-06-23 11:06:08 +00:00
*
* @author TheBusyBiscuit
* @since 4.0
*/
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;
2019-12-26 17:36:21 +00:00
private final int id;
2017-06-23 11:06:08 +00:00
private String name;
private int cost;
private final List<SlimefunItem> items = new LinkedList<>();
private boolean enabled = true;
2017-06-23 11:06:08 +00:00
/**
* The constructor for a Research.
* <p>
* Create a new research by calling {@link #Research(int, String, int)}, 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.
* <p>
* To speed up, directly setup the research by calling
* {@link Slimefun#registerResearch(Research, org.bukkit.inventory.ItemStack...)}.
*
* @deprecated Use the constuctor with {@link NamespacedKey} instead
*
2017-06-23 11:06:08 +00:00
* @param id Unique integer ID for this research, used for {@link #getByID(int)} and to
* register it in Researches.yml
2017-06-23 11:06:08 +00:00
* @param name Display name of the research
* @param cost Cost in XP levels to unlock the research
*
* @since 4.0
*/
@Deprecated
2017-06-23 11:06:08 +00:00
public Research(int id, String name, int cost) {
2016-04-14 16:24:03 +00:00
this.id = id;
this.name = name;
this.cost = cost;
this.key = new NamespacedKey(SlimefunPlugin.instance, name.replace(' ', '_'));
}
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;
}
public boolean isEnabled() {
2019-08-31 09:36:45 +00:00
return SlimefunPlugin.getSettings().researchesEnabled && enabled;
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the ID of the research.
2017-06-23 11:06:08 +00:00
*
* @return ID of the research
*
* @since 4.0
*/
2016-04-14 16:24:03 +00:00
public int getID() {
2017-06-23 11:06:08 +00:00
return id;
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the display name of the research.
2017-06-23 11:06:08 +00:00
*
* @deprecated Use {@link Research#getName(Player)} instead. It is localized.
*
2017-06-23 11:06:08 +00:00
* @return The display name of the research
*
* @since 4.0
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public String getName() {
2017-06-23 11:06:08 +00:00
return name;
2016-04-14 16:24:03 +00:00
}
public String getName(Player p) {
String localized = SlimefunPlugin.getLocal().getResearchName(p, key);
return localized != null ? localized: name;
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the cost in XP levels to unlock the research.
*
* @return The cost in XP levels of the research
2017-06-23 11:06:08 +00:00
* @since 4.1.10
*/
public int getCost() {
return cost;
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Sets the cost in XP levels to unlock the research.
*
* @param cost Cost in XP levels
*
2017-06-23 11:06:08 +00:00
* @since 4.1.10
*/
public void setCost(int cost) {
this.cost = cost;
2017-06-23 11:06:08 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Bind the specified Slimefun items to the research.
2017-06-23 11:06:08 +00:00
*
* @param items {@link SlimefunItem} to bind to the research
*
* @since 4.0
*/
2016-04-14 16:24:03 +00:00
public void addItems(SlimefunItem... items) {
for (SlimefunItem item : items) {
2016-04-14 16:24:03 +00:00
if (item != null) item.bindToResearch(this);
}
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the list of the Slimefun items bound to the research.
2017-06-23 11:06:08 +00:00
*
* @return the Slimefun items bound to the research
*
* @since 4.0
*/
public List<SlimefunItem> getAffectedItems() {
2017-06-23 11:06:08 +00:00
return items;
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Convenience method to check if the player unlocked this research.
2017-06-23 11:06:08 +00:00
*
* @param p Player to check
* @return true if he unlocked the research, otherwise false
*
* @since 4.0
* @see #hasUnlocked(UUID)
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public boolean hasUnlocked(Player p) {
return hasUnlocked(p.getUniqueId());
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Checks if the player unlocked this research.
2017-06-23 11:06:08 +00:00
*
* @param uuid UUID of the player to check
* @return true if he unlocked the research, otherwise false
*
* @since 4.0
* @see #hasUnlocked(Player)
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public boolean hasUnlocked(UUID uuid) {
return PlayerProfile.fromUUID(uuid).hasUnlocked(this);
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Checks if the player can unlock this research.
2017-06-23 11:06:08 +00:00
*
* @param p Player to check
* @return true if he can unlock the research, otherwise false
*
* @since 4.1.10
*/
public boolean canUnlock(Player p) {
if (!isEnabled()) return true;
2019-08-31 09:36:45 +00:00
return (p.getGameMode() == GameMode.CREATIVE && SlimefunPlugin.getSettings().researchesFreeInCreative) || p.getLevel() >= this.cost;
2017-06-23 11:06:08 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Locks the research for the specified player.
2017-06-23 11:06:08 +00:00
*
* @param p Player to lock the research
*
* @since 4.0
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public void lock(Player p) {
PlayerProfile.get(p).setResearched(this, false);
2019-09-21 11:59:15 +00:00
SlimefunPlugin.getLocal().sendMessage(p, "commands.research.reset-target", true);
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Unlocks the research for the specified player.
2017-06-23 11:06:08 +00:00
*
* @param p Player to unlock the research
* @param instant Whether to unlock the research instantly
*
* @since 4.0
*/
2016-04-14 16:24:03 +00:00
public void unlock(final 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)));
2019-12-26 20:10:03 +00:00
if (SlimefunPlugin.getSettings().researchFireworksEnabled && (!PersistentDataAPI.hasByte(p, GuideSettings.FIREWORKS_KEY) || PersistentDataAPI.getByte(p, GuideSettings.FIREWORKS_KEY) == (byte) 1)) {
2019-12-29 21:13:00 +00:00
FireworkUtils.launchRandom(p, 1);
2019-03-27 19:38:30 +00:00
}
};
2019-11-28 18:35:28 +00:00
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)));
2019-11-28 18:35:28 +00:00
for (int i = 1; i < RESEARCH_PROGRESS.length + 1; i++) {
2019-11-28 18:35:28 +00:00
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] + "%"));
2019-11-28 18:35:28 +00:00
}, i * 20L);
}
Slimefun.runSync(() -> {
2019-11-28 18:35:28 +00:00
runnable.run();
SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().remove(p.getUniqueId());
}, (RESEARCH_PROGRESS.length + 1) * 20L);
}
}
2019-11-28 18:35:28 +00:00
});
2016-04-14 16:24:03 +00:00
}
});
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Registers the research.
2017-06-23 11:06:08 +00:00
*
* @since 4.0
*/
2016-04-14 16:24:03 +00:00
public void register() {
2019-08-31 09:36:45 +00:00
SlimefunPlugin.getResearchCfg().setDefaultValue("enable-researching", true);
2019-08-31 09:36:45 +00:00
if (SlimefunPlugin.getResearchCfg().contains(this.getID() + ".enabled") && !SlimefunPlugin.getResearchCfg().getBoolean(this.getID() + ".enabled")) {
2016-04-14 16:24:03 +00:00
Iterator<SlimefunItem> iterator = items.iterator();
while (iterator.hasNext()) {
SlimefunItem item = iterator.next();
if (item != null) item.bindToResearch(null);
iterator.remove();
}
return;
}
2019-08-31 09:36:45 +00:00
SlimefunPlugin.getResearchCfg().setDefaultValue(this.getID() + ".cost", this.getCost());
SlimefunPlugin.getResearchCfg().setDefaultValue(this.getID() + ".enabled", true);
2019-08-31 09:36:45 +00:00
this.cost = SlimefunPlugin.getResearchCfg().getInt(this.getID() + ".cost");
this.enabled = SlimefunPlugin.getResearchCfg().getBoolean(this.getID() + ".enabled");
SlimefunPlugin.getRegistry().getResearches().add(this);
SlimefunPlugin.getRegistry().getResearchIds().add(this);
2019-08-31 15:52:20 +00:00
if (SlimefunPlugin.getSettings().printOutLoading) {
Slimefun.getLogger().log(Level.INFO, "Loaded Research \"{0}\"", name);
2019-08-31 15:52:20 +00:00
}
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the list of all registered researches.
2017-06-23 11:06:08 +00:00
*
* @return The list of registered researches
*
* @deprecated Use {@link SlimefunRegistry#getResearches()}
*
2017-06-23 11:06:08 +00:00
* @since 4.0
2017-06-23 16:01:44 +00:00
* @see ResearchSetup
2017-06-23 11:06:08 +00:00
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public static List<Research> list() {
return SlimefunPlugin.getRegistry().getResearches();
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets if the specified player is currently unlocking a research.
2017-06-23 11:06:08 +00:00
*
* @param p Player to check
* @return true if the player is unlocking a research, otherwise false
*
* @since 4.0
*/
2016-04-14 16:24:03 +00:00
public static boolean isResearching(Player p) {
return SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().contains(p.getUniqueId());
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Sends the research statistics and title of the specified player to the command sender.
2017-06-23 11:06:08 +00:00
*
* @param sender CommandSender to send the statistics
* @param p Player to get the statistics
*
* @since 4.0
* @see #getTitle(Player, List)
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public static void sendStats(CommandSender sender, Player p) {
PlayerProfile.get(p).sendStats(sender);
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the title of the specified player.
2017-06-23 11:06:08 +00:00
*
* @param p Player to get the rank
* @param researched List of the player's unlocked researches
* @return the title of the specified player
*
* @since 4.0
* @see #sendStats(CommandSender, Player)
*/
@Deprecated
public static String getTitle(Player p, Collection<Research> researched) {
return PlayerProfile.get(p).getTitle();
2016-04-14 16:24:03 +00:00
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Attempts to get the research with the given ID.
2017-06-23 11:06:08 +00:00
*
* @param id ID of the research to get
* @return Research if found, or null
*
* @since 4.0
*/
2016-04-14 16:24:03 +00:00
public static Research getByID(int id) {
2019-12-26 17:36:21 +00:00
for (Research research : list()) {
2016-04-14 16:24:03 +00:00
if (research.getID() == id) return research;
}
return null;
}
2017-06-23 11:06:08 +00:00
/**
2017-06-23 16:01:44 +00:00
* Gets the list of unlocked researches for a player using his UUID.
2017-06-23 11:06:08 +00:00
*
* @param uuid UUID of the player
* @return the list of unlocked researches for the player
*
* @since 4.0
* @see #getResearches(String)
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public static List<Research> getResearches(UUID uuid) {
List<Research> researched = new ArrayList<>();
2019-12-26 17:36:21 +00:00
for (Research research : list()) {
if (research.hasUnlocked(uuid)) researched.add(research);
2016-04-14 16:24:03 +00:00
}
return researched;
}
2019-09-04 07:54:24 +00:00
@Override
public String toString() {
2019-09-05 22:09:25 +00:00
return "Research {" + id + ',' + name + "}";
2019-09-04 07:54:24 +00:00
}
}