Skip to content

Commit

Permalink
Improve item getters
Browse files Browse the repository at this point in the history
Implement separate skull getters
  • Loading branch information
Krakenied committed Nov 20, 2023
1 parent 9206827 commit 6268201
Show file tree
Hide file tree
Showing 15 changed files with 985 additions and 746 deletions.
2 changes: 1 addition & 1 deletion bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ repositories {
dependencies {
compileOnly project(':common')
// Paper
compileOnly('io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT') {
compileOnly('io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT') {
exclude(group: 'it.unimi.dsi', module: 'fastutil') // exclude fastutil just to don't use it (for 1.8 support)
}
// Folia
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
import com.leonardobishop.quests.bukkit.hook.essentials.AbstractEssentialsHook;
import com.leonardobishop.quests.bukkit.hook.essentials.EssentialsHook;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetter;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetterLatest;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetter_1_13;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetter_Late_1_8;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetter13;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetter14;
import com.leonardobishop.quests.bukkit.hook.itemgetter.ItemGetter8;
import com.leonardobishop.quests.bukkit.hook.papi.AbstractPlaceholderAPIHook;
import com.leonardobishop.quests.bukkit.hook.papi.PlaceholderAPIHook;
import com.leonardobishop.quests.bukkit.hook.playerblocktracker.AbstractPlayerBlockTrackerHook;
import com.leonardobishop.quests.bukkit.hook.playerblocktracker.PlayerBlockTrackerHook;
import com.leonardobishop.quests.bukkit.hook.skullgetter.BukkitSkullGetter;
import com.leonardobishop.quests.bukkit.hook.skullgetter.LegacySkullGetter;
import com.leonardobishop.quests.bukkit.hook.skullgetter.ModernSkullGetter;
import com.leonardobishop.quests.bukkit.hook.skullgetter.PaperSkullGetter;
import com.leonardobishop.quests.bukkit.hook.skullgetter.SkullGetter;
import com.leonardobishop.quests.bukkit.hook.title.QuestsTitle;
import com.leonardobishop.quests.bukkit.hook.title.Title_Bukkit;
import com.leonardobishop.quests.bukkit.hook.title.Title_BukkitNoTimings;
Expand Down Expand Up @@ -116,6 +121,7 @@
import com.leonardobishop.quests.common.tasktype.TaskType;
import com.leonardobishop.quests.common.tasktype.TaskTypeManager;
import com.leonardobishop.quests.common.updater.Updater;
import com.mojang.authlib.GameProfile;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bstats.bukkit.MetricsLite;
Expand Down Expand Up @@ -169,6 +175,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests {
private AbstractEssentialsHook essentialsHook;
private AbstractPlayerBlockTrackerHook playerBlockTrackerHook;
private ItemGetter itemGetter;
private SkullGetter skullGetter;
private QuestsTitle titleHandle;
private QuestsBossBar bossBarHandle;
private QuestsActionBar actionBarHandle;
Expand Down Expand Up @@ -293,13 +300,10 @@ public void onEnable() {
setActionBarHandle();

// (itemstacks)
if (version <= 12) {
itemGetter = new ItemGetter_Late_1_8();
} else if (version == 13) {
itemGetter = new ItemGetter_1_13();
} else {
itemGetter = new ItemGetterLatest();
}
setItemGetter();

// (skulls)
setSkullGetter();

// (version specific handler)
// TODO move above to version specific handlers
Expand Down Expand Up @@ -530,7 +534,6 @@ public QuestItem getConfiguredQuestItem(String path, ConfigurationSection config
return new ParsedQuestItem("defined", null, getConfiguredItemStack(path, config, excludes));
}


public ItemStack getConfiguredItemStack(String path, ConfigurationSection config, ItemGetter.Filter... excludes) {
return itemGetter.getItem(path, config, excludes);
}
Expand Down Expand Up @@ -678,6 +681,46 @@ private void setActionBarHandle() {
actionBarHandle = new ActionBar_Nothing();
}

private void setItemGetter() {
// Spigot 1.14+
if (CompatUtils.classWithMethodExists("org.bukkit.inventory.meta.ItemMeta", "setCustomModelData", Integer.class)) {
itemGetter = new ItemGetter14(this);
return;
}

// Spigot 1.13+
if (CompatUtils.classWithMethodExists("org.bukkit.inventory.meta.ItemMeta", "getAttributeModifiers")) {
itemGetter = new ItemGetter13(this);
return;
}

// Spigot 1.8+
itemGetter = new ItemGetter8(this);
}

private void setSkullGetter() {
// Paper 1.12+
if (CompatUtils.classExists("com.destroystokyo.paper.profile.PlayerProfile")) {
skullGetter = new PaperSkullGetter(this);
return;
}

if (CompatUtils.classWithMethodExists("org.bukkit.craftbukkit.{}.inventory.CraftMetaSkull", "setProfile", GameProfile.class)) {
// Spigot 1.18.1+
if (CompatUtils.classExists("org.bukkit.profile.PlayerProfile")) {
skullGetter = new ModernSkullGetter(this);
return;
}

// Spigot 1.15.1+
skullGetter = new BukkitSkullGetter(this);
return;
}

// Spigot 1.8+
skullGetter = new LegacySkullGetter(this);
}

public boolean isValidConfiguration() {
return validConfiguration;
}
Expand Down Expand Up @@ -710,6 +753,10 @@ public ItemGetter getItemGetter() {
return itemGetter;
}

public SkullGetter getSkullGetter() {
return skullGetter;
}

public QuestsTitle getTitleHandle() {
return titleHandle;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
package com.leonardobishop.quests.bukkit.hook.itemgetter;

import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

public interface ItemGetter {
public abstract class ItemGetter {

protected static final ItemStack invalidItemStack = new ItemStack(Material.STONE, 1);
protected final BukkitQuestsPlugin plugin;

public ItemGetter(BukkitQuestsPlugin plugin) {
this.plugin = plugin;
}

/**
* Gets an ItemStack from a configuration.
* Implementations should specific to the server version.
*
* @param path the path to where the item is defined in the config (null if item is defined in second param)
* @param config the configuration file
* @param path the path to where the item is defined in the config (null if item is defined in second param)
* @param config the configuration file
* @param excludes exclude certain fields in the configuration
* @return {@link ItemStack}
*/
ItemStack getItem(String path, ConfigurationSection config, Filter... excludes);
public abstract ItemStack getItem(String path, ConfigurationSection config, Filter... excludes);

/**
* Gets an ItemStack from a given string (which represents a material).
* For pre-1.13 server implementations, the string may use a data code.
*
* @param material the string
* @param typeString the string
* @return {@link ItemStack}
*/
ItemStack getItemStack(String material);
public abstract ItemStack getItemStack(String typeString);

/**
* Validates a material from a string.
* For pre-1.13 server implementations, the string may use a data code.
*
* @param material the string
* @param typeString the string
* @return true if it a material
*/
boolean isValidMaterial(String material);
public abstract boolean isValidMaterial(String typeString);

enum Filter {
public enum Filter {
DISPLAY_NAME,
LORE,
ENCHANTMENTS,
ITEM_FLAGS,
UNBREAKABLE,
ATTRIBUTE_MODIFIER,
CUSTOM_MODEL_DATA;
CUSTOM_MODEL_DATA
}
}
}
Loading

0 comments on commit 6268201

Please sign in to comment.