Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Cryptite / PDC.java
Created March 8, 2023 14:59
PDC Handler
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.Optional;
@Cryptite
Cryptite / BuffHandler.java
Created February 5, 2021 21:21
Loka's Buff Handler
public class BuffHandler implements Runnable, Listener {
private static final BuffHandler instance = new BuffHandler();
private final TheArtifact plugin;
private final Map<BuffType, Buff> buffs = new EnumMap<>(BuffType.class);
private final Multimap<LivingEntity, Buff> activeBuffs = Multimaps.synchronizedMultimap(HashMultimap.create());
private BuffHandler() {
this.plugin = TheArtifact.getPlugin();
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 20, 20);
@Cryptite
Cryptite / ChunkTaskHandler.java
Created October 10, 2019 00:16
ChunkTaskHandler - A system for registering sync repeatable Bukkit Tasks on Chunks
public class ChunkTaskHandler implements Listener {
private static ChunkTaskHandler instance = new ChunkTaskHandler();
private final TheArtifact plugin;
private Map<Integer, Map<Object, WorldRunnable>> registeredChunks = new HashMap<>();
public boolean debug = true;
private class WorldRunnable {
private final Object key;
private final Runnable runnable;
private final int delay;
@Cryptite
Cryptite / SpatialLookup.java
Last active September 25, 2019 17:20
Location lookup
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.util.concurrent.TimeUnit
class Location {
public String world;
public double x, y, z;
@Cryptite
Cryptite / ItemStackComparable.java
Created June 6, 2019 14:47
Comparable<ItemStack>
@NotNull
private Comparator<ItemStack> itemStackComparator() {
return (o1, o2) -> {
//Sort first by their order in storableItems (left to right)
ItemStack o1BaseItem = getBaseItem(o1);
ItemStack o2BaseItem = getBaseItem(o2);
List<ItemStack> storablesList = new ArrayList<>(storableItems.keySet());
int compare = Integer.compare(storablesList.indexOf(o1BaseItem), storablesList.indexOf(o2BaseItem));
if (compare != 0) return compare;
@Cryptite
Cryptite / GetPlayerHead.java
Created February 15, 2019 18:38
Get Player Head ItemStack from Base64 encoded URL
public static ItemStack getHeadByUrl(String url) {
if (url == null) return new ItemStack(Material.BARRIER);
ItemStack head = new ItemStack(Material.PLAYER_HEAD, 1, (short) 3);
if (url.isEmpty()) return head;
SkullMeta headMeta = (SkullMeta) head.getItemMeta();
GameProfile profile = getGameProfile(url);
Field profileField;
try {
@Cryptite
Cryptite / WorldEditSchematicLoader.java
Created February 8, 2019 15:18
WorldEdit Schematic Loader
private static final BuildingPreviewExtent customExtent;
private abstract static class BuildingPreviewExtent implements Extent {
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T block) {
return false;
}
}
static {
@Cryptite
Cryptite / FancyMessage.java
Last active January 17, 2019 18:08
FancyMessage replacement for using Components/Builders for chat formatting
public class FancyMessage {
private List<TextComponent> componentList;
private List<TextComponent> lastThenSet;
private UUID setId;
private Map<UUID, Consumer<Player>> commandsMap = new HashMap<>();
private net.md_5.bungee.api.ChatColor currentColor = net.md_5.bungee.api.ChatColor.WHITE;
public FancyMessage() {
this("");
}
@Cryptite
Cryptite / ChunkSnapshotCache.java
Created November 30, 2018 19:06
Paper-based Asynchronous Cache for looking up blocks.
public class ChunkSnapshotCache {
private final LoadingCache<StringChunk, ChunkSnapshot> cache;
private ChunkSnapshotCache() {
cache = CacheBuilder.newBuilder()
.build(new CacheLoader<StringChunk, ChunkSnapshot>() {
@Override
public ChunkSnapshot load(StringChunk chunk) {
return chunk.getChunk().getChunkSnapshot(false, false, false);
}
});
@Cryptite
Cryptite / getEntitiesWithinRegion.java
Last active November 28, 2018 22:24
Get Entities in a Cuboid Region Async with PaperLib
public static void getEntitiesWithinRegion(Location p1, Location p2, Consumer<List<Entity>> entitiesConsumer) {
int minX = Math.min(p1.getBlockX(), p2.getBlockX()) >> 4;
int maxX = Math.max(p1.getBlockX(), p2.getBlockX()) >> 4;
int minZ = Math.min(p1.getBlockZ(), p2.getBlockZ()) >> 4;
int maxZ = Math.max(p1.getBlockZ(), p2.getBlockZ()) >> 4;
List<CompletableFuture> futures = new ArrayList<>();
List<Entity> entities = new ArrayList<>();
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {