Skip to content

Commit

Permalink
Added support for ItemsAdder
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Sep 17, 2023
1 parent 570ccf2 commit 2db2415
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 36 deletions.
Expand Up @@ -47,4 +47,16 @@ default Key getCustomKey(ItemStack itemStack, Key def) {
*/
boolean isCustomKey(Key key);

/**
* Get the custom item of the custom key.
* The provided key is guaranteed to pass the {@link #isCustomKey(Key)} check.
*
* @param key The key to get the custom item for.
* @return The item stack for that custom key, or null for using the default one.
*/
@Nullable
default ItemStack getCustomKeyItem(Key key) {
return null;
}

}
13 changes: 13 additions & 0 deletions Hooks/ItemsAdder/build.gradle
@@ -0,0 +1,13 @@
group 'Hooks:ItemsAdder'

dependencies {
compileOnly 'dev.lone:ItemsAdder:3.5.0'
compileOnly "org.spigotmc:v1_8_R3-Taco:latest"
compileOnly project(":API")
compileOnly rootProject
}

if (project.hasProperty('hook.compile_itemsadder') &&
!Boolean.valueOf(project.findProperty("hook.compile_itemsadder").toString())) {
project.tasks.all { task -> task.enabled = false }
}
@@ -0,0 +1,113 @@
package com.bgsoftware.superiorskyblock.external;

import com.bgsoftware.common.annotations.Nullable;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.events.IslandGenerateBlockEvent;
import com.bgsoftware.superiorskyblock.api.key.CustomKeyParser;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.key.KeyIndicator;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.listener.BlockChangesListener;
import dev.lone.itemsadder.api.CustomBlock;
import dev.lone.itemsadder.api.CustomStack;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Locale;

public class ItemsAdderHook {

private static final String ITEMS_ADDER_PREFIX = "ITEMS_ADDER";
private static final Key BLOCK_ITEM_KEY = Keys.of(Material.PAPER);
private static final Key BLOCK_KEY = Keys.of(Material.NOTE_BLOCK);

private static Singleton<BlockChangesListener> blockChangesListener;

public static void register(SuperiorSkyblockPlugin plugin) {
blockChangesListener = plugin.getListener(BlockChangesListener.class);
plugin.getBlockValues().registerKeyParser(new ItemsAdderKeyParser(), BLOCK_ITEM_KEY, BLOCK_KEY);
plugin.getServer().getPluginManager().registerEvents(new ListenerImpl(), plugin);
}

private static class ListenerImpl implements Listener {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent e) {
// ItemsAdder calls BlockPlaceEvent when the block is AIR.
if (e.getBlock().getType() != Material.AIR)
return;

BlockState oldState = e.getBlockReplacedState();

BukkitExecutor.sync(() -> {
blockChangesListener.get().onBlockPlace(Keys.of(e.getBlock()), e.getBlock().getLocation(), 1,
oldState, BlockChangesListener.BlockTrackFlags.DIRTY_CHUNKS | BlockChangesListener.BlockTrackFlags.SAVE_BLOCK_COUNT);
}, 1L);

}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIslandGenerateBlock(IslandGenerateBlockEvent event) {
if (!event.getBlock().getGlobalKey().equals(ITEMS_ADDER_PREFIX))
return;

String itemId = event.getBlock().getSubKey().toLowerCase(Locale.ENGLISH);

CustomBlock customBlock = CustomBlock.getInstance(itemId);

if (customBlock == null) {
event.setCancelled(true);
return;
}

event.setPlaceBlock(false);
customBlock.place(event.getLocation());
}

}

private static class ItemsAdderKeyParser implements CustomKeyParser {

@Override
public Key getCustomKey(Location location) {
Block block = location.getBlock();
CustomBlock customBlock = CustomBlock.byAlreadyPlaced(block);
if (customBlock == null)
return BLOCK_KEY;
return Keys.of(ITEMS_ADDER_PREFIX, customBlock.getId().toUpperCase(Locale.ENGLISH), KeyIndicator.CUSTOM);
}

@Override
public Key getCustomKey(ItemStack itemStack, Key def) {
CustomStack customStack = CustomStack.byItemStack(itemStack);
if (customStack == null)
return def;
return Keys.of(ITEMS_ADDER_PREFIX, customStack.getId().toUpperCase(Locale.ENGLISH), KeyIndicator.CUSTOM);
}

@Override
public boolean isCustomKey(Key key) {
return key.getGlobalKey().equals(ITEMS_ADDER_PREFIX);
}

@Override
@Nullable
public ItemStack getCustomKeyItem(Key key) {
CustomStack customStack = CustomStack.getInstance(key.getSubKey().toLowerCase(Locale.ENGLISH));
if (customStack == null)
return null;
return customStack.getItemStack();
}

}

}
1 change: 1 addition & 0 deletions gradle.properties
Expand Up @@ -15,6 +15,7 @@ hook.compile_epicspawners6=true
hook.compile_epicspawners7=true
hook.compile_essentials=true
hook.compile_fastasyncworldedit=true
hook.compile_itemsadder=true
hook.compile_jetsminions=true
hook.compile_luckperms=true
hook.compile_mergedspawner=true
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Expand Up @@ -52,4 +52,4 @@ include 'NMS:v1_17'
include 'NMS:v1_18'
include 'NMS:v1_19'
include 'NMS:v1_20_1'

include 'Hooks:ItemsAdder'
Expand Up @@ -8,6 +8,7 @@
import com.bgsoftware.superiorskyblock.core.Text;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.key.types.CustomKey;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -76,6 +77,16 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, String[
if (!Text.isBlank(subKey))
keyName = Formatters.CAPITALIZED_FORMATTER.format(subKey + "_Spawner");
}

if (keyName.isEmpty() && toCheck instanceof CustomKey) {
String subKey = toCheck.getSubKey();
if (Text.isBlank(subKey)) {
keyName = Formatters.CAPITALIZED_FORMATTER.format(toCheck.toString());
} else {
keyName = Formatters.CAPITALIZED_FORMATTER.format(subKey);
}
}

} else {
toCheck = Keys.ofMaterialAndData(args[1]);
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.menu.button.MenuTemplateButton;
import com.bgsoftware.superiorskyblock.api.menu.button.PagedMenuTemplateButton;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.ServerVersion;
Expand All @@ -23,6 +24,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Optional;

public class CountsPagedObjectButton extends AbstractPagedMenuButton<MenuCounts.View, MenuCounts.BlockCount> {

Expand Down Expand Up @@ -161,47 +163,56 @@ public void onButtonClick(InventoryClickEvent clickEvent) {
@Override
public ItemStack modifyViewItem(ItemStack buttonItem) {
Key rawKey = pagedObject.getBlockKey();
Key blockKey = plugin.getBlockValues().convertKey(rawKey);
Pair<Key, ItemStack> customKeyItem = plugin.getBlockValues().convertCustomKeyItem(rawKey);

BigDecimal amount = new BigDecimal(pagedObject.getAmount());

String convertedItem = BLOCKS_TO_ITEMS.get(blockKey.getGlobalKey());
ItemMeta currentMeta = buttonItem.getItemMeta();
ItemBuilder itemBuilder;
String materialName = null;

if (convertedItem != null) {
Key tempBlockType = Keys.ofMaterialAndData(convertedItem);
if (tempBlockType instanceof MaterialKey)
blockKey = tempBlockType;
}
ItemStack customItem = customKeyItem.getValue();
if (customItem != null) {
itemBuilder = new ItemBuilder(customItem);
materialName = Optional.ofNullable(rawKey.getSubKey()).orElseGet(rawKey::toString);
} else {
Key blockKey = customKeyItem.getKey();

Material blockMaterial;
byte damage = 0;
String materialName = null;
String convertedItem = BLOCKS_TO_ITEMS.get(blockKey.getGlobalKey());

if (convertedItem != null) {
Key tempBlockType = Keys.ofMaterialAndData(convertedItem);
if (tempBlockType instanceof MaterialKey)
blockKey = tempBlockType;
}

Material blockMaterial;
byte damage = 0;

try {
blockMaterial = Material.valueOf(blockKey.getGlobalKey());
if (!blockKey.getSubKey().isEmpty()) {
try {
damage = Byte.parseByte(blockKey.getSubKey());
} catch (Throwable ignored) {
try {
blockMaterial = Material.valueOf(blockKey.getGlobalKey());
if (!blockKey.getSubKey().isEmpty()) {
try {
damage = Byte.parseByte(blockKey.getSubKey());
} catch (Throwable ignored) {
}
}
} catch (Exception ex) {
blockMaterial = Material.BEDROCK;
materialName = blockKey.getGlobalKey();
}
} catch (Exception ex) {
blockMaterial = Material.BEDROCK;
materialName = blockKey.getGlobalKey();
}

ItemMeta currentMeta = buttonItem.getItemMeta();
ItemBuilder itemBuilder;
String texture;
String texture;

if (blockMaterial == Materials.SPAWNER.toBukkitType() && !blockKey.getSubKey().isEmpty() &&
!(texture = ItemSkulls.getTexture(blockKey.getSubKey())).isEmpty()) {
itemBuilder = new ItemBuilder(ItemSkulls.getPlayerHead(Materials.PLAYER_HEAD.toBukkitItem(), texture));
materialName = blockKey.getSubKey() + "_SPAWNER";
} else {
itemBuilder = new ItemBuilder(blockMaterial, damage);
if (materialName == null)
materialName = rawKey.getGlobalKey();
if (blockMaterial == Materials.SPAWNER.toBukkitType() && !blockKey.getSubKey().isEmpty() &&
!(texture = ItemSkulls.getTexture(blockKey.getSubKey())).isEmpty()) {
itemBuilder = new ItemBuilder(ItemSkulls.getPlayerHead(Materials.PLAYER_HEAD.toBukkitItem(), texture));
materialName = blockKey.getSubKey() + "_SPAWNER";
} else {
itemBuilder = new ItemBuilder(blockMaterial, damage);
if (materialName == null)
materialName = rawKey.getGlobalKey();
}
}

BigDecimal worthValue = plugin.getBlockValues().getBlockWorth(rawKey);
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.key.KeyMap;
import com.bgsoftware.superiorskyblock.api.key.KeySet;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.Manager;
import com.bgsoftware.superiorskyblock.core.key.BaseKey;
import com.bgsoftware.superiorskyblock.core.key.KeyIndicator;
Expand Down Expand Up @@ -252,13 +253,15 @@ public Key convertKey(Key original, Entity entity) {
return key == null ? original : key;
}

public Key convertKey(Key original) {
@Nullable
public Pair<Key, ItemStack> convertCustomKeyItem(Key original) {
for (Map.Entry<Key, CustomKeyParser> entry : customKeyParsers.entrySet()) {
if (entry.getValue().isCustomKey(original))
return entry.getKey();
if (entry.getValue().isCustomKey(original)) {
return new Pair<>(entry.getKey(), entry.getValue().getCustomKeyItem(original));
}
}

return original;
return new Pair<>(original, null);
}

public BigDecimal convertValueToLevel(BigDecimal value) {
Expand Down
Expand Up @@ -417,6 +417,9 @@ private void registerGeneralHooks() {

if (Bukkit.getPluginManager().isPluginEnabled("Oraxen"))
registerHook("OraxenHook");

if (Bukkit.getPluginManager().isPluginEnabled("ItemsAdder"))
registerHook("ItemsAdderHook");
}

private void registerSpawnersProvider() {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Expand Up @@ -15,6 +15,7 @@ softdepend:
- EpicSpawners
- Essentials
- FastAsyncWorldEdit
- ItemsAdder
- JetsMinions
- LuckPerms
- MergedSpawner
Expand Down

0 comments on commit 2db2415

Please sign in to comment.