Skip to content

Commit

Permalink
feat(research): economy unlock research
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Aug 8, 2023
1 parent baef038 commit 650f47a
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 363 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,20 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>

<exclusions>
<exclusion>
<!-- We don't need any of the dependencies -->
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- TODO: Remove this dependency -->
<dependency>
<groupId>commons-lang</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package io.github.thebusybiscuit.slimefun4.api.researches;

import io.github.thebusybiscuit.slimefun4.api.events.PlayerPreResearchEvent;
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
import io.github.thebusybiscuit.slimefun4.core.services.localization.Language;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.setup.ResearchSetup;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand All @@ -19,16 +26,6 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.events.PlayerPreResearchEvent;
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
import io.github.thebusybiscuit.slimefun4.core.services.localization.Language;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.setup.ResearchSetup;

/**
* Represents a research, which is bound to one
* {@link SlimefunItem} or more and requires XP levels to unlock said item(s).
Expand Down Expand Up @@ -228,7 +225,8 @@ public void unlockFromGuide(SlimefunGuideImplementation guide, Player player, Pl
if (this.canUnlock(player)) {
guide.unlockItem(player, sfItem, pl -> guide.openItemGroup(profile, itemGroup, page));
} else {
Slimefun.getLocalization().sendMessage(player, "messages.not-enough-xp", true);
var tokenName = Slimefun.getRegistry().getSlimefunGuideUnlockMode().getTokenName();
Slimefun.getLocalization().sendMessage(player, "messages.not-enough-token", true, s -> s.replace("%token_name%", tokenName));
}
}
}
Expand All @@ -248,8 +246,10 @@ public boolean canUnlock(@Nonnull Player p) {
return true;
}

boolean creativeResearch = p.getGameMode() == GameMode.CREATIVE && Slimefun.getRegistry().isFreeCreativeResearchingEnabled();
return creativeResearch || p.getLevel() >= cost;
var creativeResearch = p.getGameMode() == GameMode.CREATIVE && Slimefun.getRegistry().isFreeCreativeResearchingEnabled();
var unlockProvider = Slimefun.getRegistry().getSlimefunGuideUnlockMode().getUnlockProvider();

return creativeResearch || unlockProvider.canUnlock(this, p);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package io.github.thebusybiscuit.slimefun4.core;

import io.github.bakedlibs.dough.collections.KeyMap;
import io.github.bakedlibs.dough.config.Config;
import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.ItemHandler;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideMode;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideUnlockMode;
import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlock;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.guide.CheatSheetSlimefunGuide;
import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
Expand All @@ -11,9 +27,11 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Nonnull;

import me.mrCookieSlime.Slimefun.api.BlockInfoConfig;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.inventory.UniversalBlockMenu;
import org.apache.commons.lang.Validate;
import org.bukkit.NamespacedKey;
import org.bukkit.Server;
Expand All @@ -23,27 +41,6 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.collections.KeyMap;
import io.github.bakedlibs.dough.config.Config;
import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.ItemHandler;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideMode;
import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlock;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.guide.CheatSheetSlimefunGuide;
import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;

import me.mrCookieSlime.Slimefun.api.BlockInfoConfig;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.inventory.UniversalBlockMenu;

/**
* This class houses a lot of instances of {@link Map} and {@link List} that hold
* various mappings and collections related to {@link SlimefunItem}.
Expand Down Expand Up @@ -72,6 +69,7 @@ public final class SlimefunRegistry {
private boolean disableLearningAnimation;
private boolean logDuplicateBlockEntries;
private boolean talismanActionBarMessages;
private SlimefunGuideUnlockMode guideUnlockMode;

private final Set<String> tickers = new HashSet<>();
private final Set<SlimefunItem> radioactive = new HashSet<>();
Expand Down Expand Up @@ -111,6 +109,8 @@ public void load(@Nonnull Slimefun plugin, @Nonnull Config cfg) {
freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode");
researchFireworks = cfg.getBoolean("researches.enable-fireworks");
disableLearningAnimation = cfg.getBoolean("researches.disable-learning-animation");
guideUnlockMode = SlimefunGuideUnlockMode.check(cfg.getString("researches.unlock-research-mode"));

logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries");
talismanActionBarMessages = cfg.getBoolean("talismans.use-actionbar");
}
Expand Down Expand Up @@ -260,6 +260,11 @@ public SlimefunGuideImplementation getSlimefunGuide(@Nonnull SlimefunGuideMode m
return guide;
}

@Nonnull
public SlimefunGuideUnlockMode getSlimefunGuideUnlockMode() {
return guideUnlockMode;
}

/**
* This returns a {@link Map} connecting the {@link EntityType} with a {@link Set}
* of {@link ItemStack ItemStacks} which would be dropped when an {@link Entity} of that type was killed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package io.github.thebusybiscuit.slimefun4.core.guide;

import java.util.function.Consumer;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.core.guide.options.SlimefunGuideSettings;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
* This interface is used for the different implementations that add behaviour
Expand Down Expand Up @@ -65,12 +61,13 @@ public interface SlimefunGuideImplementation {

@ParametersAreNonnullByDefault
default void unlockItem(Player p, SlimefunItem sfitem, Consumer<Player> callback) {
Research research = sfitem.getResearch();
var research = sfitem.getResearch();
var unlockProvider = Slimefun.getRegistry().getSlimefunGuideUnlockMode().getUnlockProvider();

if (p.getGameMode() == GameMode.CREATIVE && Slimefun.getRegistry().isFreeCreativeResearchingEnabled()) {
research.unlock(p, true, callback);
} else {
p.setLevel(p.getLevel() - research.getCost());
unlockProvider.processPayment(research, p);

boolean skipLearningAnimation = Slimefun.getRegistry().isLearningAnimationDisabled() || !SlimefunGuideSettings.hasLearningAnimationEnabled(p);
research.unlock(p, skipLearningAnimation, callback);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.github.thebusybiscuit.slimefun4.core.guide;

import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.integrations.VaultIntegration;
import java.util.Locale;
import javax.annotation.Nonnull;
import org.bukkit.entity.Player;

/**
* This enum holds the different unlock research modes a {@link SlimefunGuide} can have.
* Each constant corresponds to a research unlock mode and a unlock provider
*
* @author StarWishsama
* @see SlimefunGuide
* @see SlimefunGuideImplementation
* @see SlimefunGuideUnlockProvider
*/
public enum SlimefunGuideUnlockMode {
/**
* Unlock research by withdrawing player's experience level.
*/
EXPERIENCE(new SlimefunGuideUnlockProvider() {
@Override
public boolean canUnlock(@Nonnull Research research, @Nonnull Player p) {
return p.getLevel() >= research.getCost();
}

@Override
public void processPayment(@Nonnull Research research, @Nonnull Player p) {
p.setLevel(p.getLevel() - research.getCost());
}
}),

/**
* Unlock research by withdrawing player's balance.
*/
ECONOMY(new SlimefunGuideUnlockProvider() {
@Override
public boolean canUnlock(@Nonnull Research research, @Nonnull Player p) {
return VaultIntegration.getPlayerBalance(p) >= research.getCost();
}

@Override
public void processPayment(@Nonnull Research research, @Nonnull Player p) {
VaultIntegration.withdrawPlayer(p, research.getCost());
}
});

/**
* Research unlock provider
* <p>
* Process player can unlock research and process research payment.
*
* @see SlimefunGuideUnlockProvider
*/
@Nonnull
private final SlimefunGuideUnlockProvider unlockProvider;

SlimefunGuideUnlockMode(@Nonnull SlimefunGuideUnlockProvider unlockProvider) {
this.unlockProvider = unlockProvider;
}

/**
* Convert string to certain {@link SlimefunGuideUnlockMode}.
* If string is invalid will fall back to default one (player level)
*
* @param s text to validate
* @return {@link SlimefunGuideUnlockMode}
*/
public static SlimefunGuideUnlockMode check(String s) {
if (s == null) {
return SlimefunGuideUnlockMode.EXPERIENCE;
}

for (SlimefunGuideUnlockMode value : SlimefunGuideUnlockMode.values()) {
if (value.toString().equalsIgnoreCase(s)) {
return value;
}
}

return SlimefunGuideUnlockMode.EXPERIENCE;
}

@Nonnull
public SlimefunGuideUnlockProvider getUnlockProvider() {
return unlockProvider;
}

public String getTokenName() {
return Slimefun.getLocalization().getMessage("guide.unlock-mode-" + toString().toLowerCase(Locale.ROOT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.thebusybiscuit.slimefun4.core.guide;

import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import javax.annotation.Nonnull;
import org.bukkit.entity.Player;

public interface SlimefunGuideUnlockProvider {
boolean canUnlock(@Nonnull Research research, @Nonnull Player p);

void processPayment(@Nonnull Research research, @Nonnull Player p);
}
Loading

0 comments on commit 650f47a

Please sign in to comment.