9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-19 15:09:24 +00:00

[Feature] Placeable skull

This commit is contained in:
XiaoMoMi
2024-04-27 23:42:15 +08:00
parent 7f471bac3c
commit 9326bb3a2c
6 changed files with 148 additions and 116 deletions

View File

@@ -44,6 +44,8 @@ public interface ItemBuilder extends BuildableItem {
ItemBuilder unbreakable(boolean unbreakable);
ItemBuilder placeable(boolean placeable);
ItemBuilder lore(List<String> lore);
ItemBuilder nbt(Map<String, Object> nbt);

View File

@@ -7,7 +7,7 @@ plugins {
allprojects {
version = "2.1.4.1"
version = "2.1.5"
apply<JavaPlugin>()
apply(plugin = "java")

View File

@@ -18,7 +18,7 @@ dependencies {
compileOnly("me.clip:placeholderapi:2.11.5")
// config
compileOnly("dev.dejvokep:boosted-yaml:1.3.2")
compileOnly("dev.dejvokep:boosted-yaml:1.3.4")
// mythic
compileOnly("io.lumine:Mythic-Dist:5.3.5")
@@ -89,5 +89,6 @@ tasks {
relocate ("dev.dejvokep.boostedyaml", "net.momirealms.customfishing.libraries.boostedyaml")
relocate ("org.bstats", "net.momirealms.customfishing.libraries.bstats")
relocate ("net.momirealms.biomeapi", "net.momirealms.customfishing.libraries.biomeapi")
relocate ("xyz.xenondevs", "net.momirealms.customfishing.libraries")
}
}

View File

@@ -26,6 +26,7 @@
package net.momirealms.customfishing.libraries.dependencies;
import com.google.common.collect.ImmutableList;
import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customfishing.libraries.dependencies.relocation.Relocation;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable;
@@ -61,102 +62,6 @@ public enum Dependency {
null,
"jar-relocator"
),
KYORI_OPTION(
"net{}kyori",
"option",
"1.0.0",
null,
"kyori-option",
Relocation.of("option", "net{}kyori{}option")
),
ADVENTURE_API(
"net{}kyori",
"adventure-api",
"4.16.0",
null,
"adventure-api",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_KEY(
"net{}kyori",
"adventure-key",
"4.16.0",
null,
"adventure-key",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_NBT(
"net{}kyori",
"adventure-nbt",
"4.16.0",
null,
"adventure-nbt",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_LEGACY_SERIALIZER(
"net{}kyori",
"adventure-text-serializer-legacy",
"4.16.0",
null,
"adventure-text-serializer-legacy",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_TEXT_LOGGER(
"net{}kyori",
"adventure-text-logger-slf4j",
"4.16.0",
null,
"adventure-text-logger-slf4j",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_GSON(
"net{}kyori",
"adventure-text-serializer-gson",
"4.16.0",
null,
"adventure-text-serializer-gson",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_GSON_LEGACY(
"net{}kyori",
"adventure-text-serializer-gson-legacy-impl",
"4.16.0",
null,
"adventure-text-serializer-gson-legacy-impl",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_PLATFORM(
"net{}kyori",
"adventure-platform-api",
"4.3.2",
null,
"adventure-platform-api",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_PLATFORM_BUKKIT(
"net{}kyori",
"adventure-platform-bukkit",
"4.3.2",
null,
"adventure-platform-bukkit",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_PLATFORM_FACET(
"net{}kyori",
"adventure-platform-facet",
"4.3.2",
null,
"adventure-platform-facet",
Relocation.of("adventure", "net{}kyori{}adventure")
),
ADVENTURE_TEXT_MINIMESSAGE(
"net{}kyori",
"adventure-text-minimessage",
"4.16.0",
null,
"adventure-text-minimessage",
Relocation.of("adventure", "net{}kyori{}adventure")
),
COMMAND_API(
"dev{}jorel",
"commandapi-bukkit-shade",
@@ -176,7 +81,7 @@ public enum Dependency {
BOOSTED_YAML(
"dev{}dejvokep",
"boosted-yaml",
"1.3.2",
"1.3.4",
null,
"boosted-yaml",
Relocation.of("boostedyaml", "dev{}dejvokep{}boostedyaml")
@@ -304,23 +209,27 @@ public enum Dependency {
INV_UI(
"xyz{}xenondevs{}invui",
"invui-core",
"1.28",
"1.29",
"xenondevs",
"invui-core"
"invui-core",
Relocation.of("invui", "xyz{}xenondevs{}invui"),
Relocation.of("inventoryaccess", "xyz{}xenondevs{}inventoryaccess")
),
INV_UI_ACCESS(
"xyz{}xenondevs{}invui",
"inventory-access",
"1.28",
"1.29",
"xenondevs",
"inventory-access"
"inventory-access",
Relocation.of("inventoryaccess", "xyz{}xenondevs{}inventoryaccess")
),
INV_UI_NMS(
"xyz{}xenondevs{}invui",
getInvUINms(),
"1.28",
"1.29",
"xenondevs",
getInvUINms()
getInvUINms(),
Relocation.of("inventoryaccess", "xyz{}xenondevs{}inventoryaccess")
),
BIOME_API(
"com{}github{}Xiao-MoMi",
@@ -415,19 +324,20 @@ public enum Dependency {
}
private static String getInvUINms() {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
String version = Bukkit.getServer().getBukkitVersion().split("-")[0];
String artifact;
switch (version) {
case "v1_17_R1" -> artifact = "r9";
case "v1_18_R1" -> artifact = "r10";
case "v1_18_R2" -> artifact = "r11";
case "v1_19_R1" -> artifact = "r12";
case "v1_19_R2" -> artifact = "r13";
case "v1_19_R3" -> artifact = "r15";
case "v1_20_R1" -> artifact = "r16";
case "v1_20_R2" -> artifact = "r17";
case "v1_20_R3" -> artifact = "r18";
default -> artifact = "r18";
case "1.17.1" -> artifact = "r9";
case "1.18.1" -> artifact = "r10";
case "1.18.2" -> artifact = "r11";
case "1.19.1", "1.19.2" -> artifact = "r13";
case "1.19.3" -> artifact = "r14";
case "1.19.4" -> artifact = "r15";
case "1.20.1" -> artifact = "r16";
case "1.20.2" -> artifact = "r17";
case "1.20.3", "1.20.4" -> artifact = "r18";
case "1.20.5" -> artifact = "r19";
default -> throw new RuntimeException("Unsupported version: " + version);
}
return String.format("inventory-access-%s", artifact);
}

View File

@@ -47,11 +47,14 @@ import net.momirealms.customfishing.compatibility.papi.PlaceholderManagerImpl;
import net.momirealms.customfishing.setting.CFConfig;
import net.momirealms.customfishing.util.ConfigUtils;
import net.momirealms.customfishing.util.ItemUtils;
import net.momirealms.customfishing.util.LocationUtils;
import net.momirealms.customfishing.util.NBTUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
@@ -61,6 +64,8 @@ import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.player.PlayerAttemptPickupItemEvent;
@@ -71,6 +76,8 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -363,6 +370,7 @@ public class ItemManagerImpl implements ItemManager, Listener {
.randomDamage(section.getBoolean("random-durability", false))
.unbreakable(section.getBoolean("unbreakable", false))
.preventGrabbing(section.getBoolean("prevent-grabbing", true))
.placeable(section.getBoolean("placeable", false))
.head(section.getString("head64"))
.name(section.getString("display.name"))
.lore(section.getStringList("display.lore"));
@@ -620,6 +628,16 @@ public class ItemManagerImpl implements ItemManager, Listener {
return this;
}
@Override
public ItemBuilder placeable(boolean placeable) {
editors.put("placeable", (player, nbtItem, placeholders) -> {
if (!placeable) return;
NBTCompound cfCompound = nbtItem.getOrCreateCompound("CustomFishing");
cfCompound.setByte("placeable", (byte) 1);
});
return this;
}
@Override
public ItemBuilder lore(List<String> lore) {
if (lore.size() == 0) return this;
@@ -1006,4 +1024,101 @@ public class ItemManagerImpl implements ItemManager, Listener {
}
}
}
@EventHandler (ignoreCancelled = true)
public void onPlaceBlock(BlockPlaceEvent event) {
ItemStack itemStack = event.getItemInHand();
if (itemStack.getType() == Material.AIR || itemStack.getAmount() == 0 || !itemStack.hasItemMeta()) {
return;
}
NBTItem nbtItem = new NBTItem(itemStack);
NBTCompound compound = nbtItem.getCompound("CustomFishing");
if (compound != null) {
if (!compound.hasTag("placeable") || compound.getByte("placeable") != 1) {
event.setCancelled(true);
return;
}
Block block = event.getBlock();
if (block.getState() instanceof Skull) {
PersistentDataContainer pdc = block.getChunk().getPersistentDataContainer();
ItemStack cloned = itemStack.clone();
cloned.setAmount(1);
pdc.set(new NamespacedKey(plugin, LocationUtils.toChunkPosString(block.getLocation())), PersistentDataType.STRING, ItemUtils.toBase64(cloned));
} else {
event.setCancelled(true);
}
}
}
@EventHandler (ignoreCancelled = true)
public void onBreakBlock(BlockBreakEvent event) {
final Block block = event.getBlock();
if (block.getState() instanceof Skull) {
PersistentDataContainer pdc = block.getChunk().getPersistentDataContainer();
String base64 = pdc.get(new NamespacedKey(plugin, LocationUtils.toChunkPosString(block.getLocation())), PersistentDataType.STRING);
if (base64 != null) {
ItemStack itemStack = ItemUtils.fromBase64(base64);
event.setDropItems(false);
block.getLocation().getWorld().dropItemNaturally(block.getLocation(), itemStack);
}
}
}
@EventHandler (ignoreCancelled = true)
public void onPiston(BlockPistonExtendEvent event) {
for (Block block : event.getBlocks()) {
if (block.getState() instanceof Skull) {
PersistentDataContainer pdc = block.getChunk().getPersistentDataContainer();
if (pdc.has(new NamespacedKey(plugin, LocationUtils.toChunkPosString(block.getLocation())), PersistentDataType.STRING)) {
event.setCancelled(true);
return;
}
}
}
}
@EventHandler (ignoreCancelled = true)
public void onPiston(BlockPistonRetractEvent event) {
for (Block block : event.getBlocks()) {
if (block.getState() instanceof Skull) {
PersistentDataContainer pdc = block.getChunk().getPersistentDataContainer();
if (pdc.has(new NamespacedKey(plugin, LocationUtils.toChunkPosString(block.getLocation())), PersistentDataType.STRING)) {
event.setCancelled(true);
return;
}
}
}
}
@EventHandler (ignoreCancelled = true)
public void onExplosion(BlockExplodeEvent event) {
handleExplode(event.blockList());
}
@EventHandler (ignoreCancelled = true)
public void onExplosion(EntityExplodeEvent event) {
handleExplode(event.blockList());
}
private void handleExplode(List<Block> blocks) {
ArrayList<Block> blockToRemove = new ArrayList<>();
for (Block block : blocks) {
if (block.getState() instanceof Skull) {
PersistentDataContainer pdc = block.getChunk().getPersistentDataContainer();
var nk = new NamespacedKey(plugin, LocationUtils.toChunkPosString(block.getLocation()));
String base64 = pdc.get(nk, PersistentDataType.STRING);
if (base64 != null) {
ItemStack itemStack = ItemUtils.fromBase64(base64);
block.getLocation().getWorld().dropItemNaturally(block.getLocation(), itemStack);
blockToRemove.add(block);
block.setType(Material.AIR);
pdc.remove(nk);
}
}
}
blocks.removeAll(blockToRemove);
}
}

View File

@@ -41,4 +41,8 @@ public class LocationUtils {
public static Location getAnyLocationInstance() {
return new Location(Bukkit.getWorlds().get(0), 0, 64, 0);
}
public static String toChunkPosString(Location location) {
return (location.getBlockX() % 16) + "_" + location.getBlockY() + "_" + (location.getBlockZ() % 16);
}
}