Skip to content

Commit

Permalink
Added support for Oraxen for custom values for their custom blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Sep 17, 2023
1 parent 2db2415 commit c4194c9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Hooks/Oraxen/build.gradle
Expand Up @@ -8,7 +8,7 @@ java {

dependencies {
compileOnly 'io.th0rgal:Oraxen:1.155.1'
compileOnly "org.spigotmc:v1_8_R3-Taco:latest"
compileOnly "org.spigotmc:v1_16_R3-Tuinity:latest"
compileOnly project(":API")
compileOnly rootProject
}
Expand Down
@@ -1,20 +1,33 @@
package com.bgsoftware.superiorskyblock.external;

import com.bgsoftware.common.annotations.Nullable;
import com.bgsoftware.common.reflection.ReflectMethod;
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.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.key.KeyIndicator;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.listener.BlockChangesListener;
import io.th0rgal.oraxen.api.OraxenBlocks;
import io.th0rgal.oraxen.api.OraxenItems;
import io.th0rgal.oraxen.items.ItemBuilder;
import io.th0rgal.oraxen.mechanics.Mechanic;
import io.th0rgal.oraxen.mechanics.MechanicFactory;
import io.th0rgal.oraxen.mechanics.MechanicsManager;
import io.th0rgal.oraxen.mechanics.provided.gameplay.block.BlockMechanicFactory;
import io.th0rgal.oraxen.mechanics.provided.gameplay.noteblock.NoteBlockMechanicFactory;
import io.th0rgal.oraxen.mechanics.provided.gameplay.stringblock.StringBlockMechanicFactory;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Collections;
import java.util.LinkedList;
Expand All @@ -23,6 +36,10 @@

public class OraxenHook {

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

private static final List<Pair<MechanicFactory, SetBlockModelFunction>> AVAILABLE_MECHANICS;

static {
Expand Down Expand Up @@ -53,15 +70,29 @@ public class OraxenHook {
AVAILABLE_MECHANICS = Collections.unmodifiableList(availableMechanics);
}

private static Singleton<BlockChangesListener> blockChangesListener;

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

private static class GeneratorListener implements Listener {

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onOraxenBlockBreak(BlockBreakEvent e) {
Key blockKey = Keys.of(e.getBlock());
if (blockKey.getGlobalKey().equals(ORAXEN_PREFIX))
blockChangesListener.get().onBlockBreak(blockKey, e.getBlock().getLocation(), 1,
BlockChangesListener.BlockTrackFlags.HANDLE_NEARBY_BLOCKS |
BlockChangesListener.BlockTrackFlags.DIRTY_CHUNKS |
BlockChangesListener.BlockTrackFlags.SAVE_BLOCK_COUNT);
}

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

String itemId = event.getBlock().getSubKey().toLowerCase(Locale.ENGLISH);
Expand All @@ -86,6 +117,40 @@ public void onIslandGenerateBlock(IslandGenerateBlockEvent event) {

}

private static class OraxenKeyParser implements CustomKeyParser {

@Override
public Key getCustomKey(Location location) {
Mechanic mechanic = OraxenBlocks.getOraxenBlock(location);
if (mechanic == null)
return BLOCK_KEY;
return Keys.of(ORAXEN_PREFIX, mechanic.getItemID().toUpperCase(Locale.ENGLISH), KeyIndicator.CUSTOM);
}

@Override
public Key getCustomKey(ItemStack itemStack, Key def) {
String itemId = OraxenItems.getIdByItem(itemStack);
if (itemId == null)
return def;
return Keys.of(ORAXEN_PREFIX, itemId.toUpperCase(Locale.ENGLISH), KeyIndicator.CUSTOM);
}

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

@Override
@Nullable
public ItemStack getCustomKeyItem(Key key) {
ItemBuilder itemBuilder = OraxenItems.getItemById(key.getSubKey().toLowerCase(Locale.ENGLISH));
if (itemBuilder == null)
return null;
return itemBuilder.build();
}

}

private interface SetBlockModelFunction {

void setBlockModel(Block block, String itemId);
Expand Down
Expand Up @@ -95,7 +95,9 @@ public BlockChangesListener(SuperiorSkyblockPlugin plugin) {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent e) {
onBlockPlace(Keys.of(e.getBlock()), e.getBlock().getLocation(), 1, e.getBlockReplacedState(),
boolean shouldAvoidReplacedState = e.getBlockReplacedState().equals(e.getBlock().getState());
onBlockPlace(Keys.of(e.getBlock()), e.getBlock().getLocation(), 1,
shouldAvoidReplacedState ? null : e.getBlockReplacedState(),
BlockTrackFlags.DIRTY_CHUNKS | BlockTrackFlags.SAVE_BLOCK_COUNT);
}

Expand Down

0 comments on commit c4194c9

Please sign in to comment.