Skip to content

Commit

Permalink
feat: backup item tag before warping to lifetown
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Jul 29, 2024
1 parent ccff0d0 commit ef6a9db
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 8 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "1.15.2+6.16.3"
version = "1.15.2+6.16.4"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down Expand Up @@ -63,6 +63,7 @@ dependencies {
implementation("org.mariadb.jdbc:mariadb-java-client:3.0.6")
implementation("org.yaml:snakeyaml:2.0")
implementation("xyz.acrylicstyle.java-util:expression:2.0.0-SNAPSHOT")
compileOnly("net.azisaba:JoinFilter:1.0.0")
compileOnly("net.azisaba.ballotbox:receiver:1.0.1")
compileOnly("net.azisaba.azipluginmessaging:api:4.0.3")
compileOnly("net.azisaba:RyuZUPluginChat:4.2.0")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/mori01231/lifecore/LifeCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class LifeCore : JavaPlugin() {
registerCommand("life", TransferCommand(this, "life"))
registerCommand("resource", TransferCommand(this, "liferesource"))
registerCommand("event", TransferCommand(this, "lifeevent"))
registerCommand("townserver", TransferCommand(this, "lifetown"))
registerCommand("townserver", TownServerCommand(this))
registerCommand("rank", RankCommand(this))
registerCommand("trash", TrashCommand())
registerCommand("kiai", KiaiCommand())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class LifeCoreUtilCommand(val plugin: LifeCore) : TabExecutor {
val item = CraftItemStack.asNMSCopy((player as Player).inventory.itemInMainHand)
val tag = item.tag ?: NBTTagCompound()
tag.a(MojangsonParser.parse(args.joinToString(" ")))
(player as Player).inventory.setItemInMainHand(CraftItemStack.asBukkitCopy(item))
player.inventory.setItemInMainHand(CraftItemStack.asBukkitCopy(item))
}
},
SetTag {
Expand All @@ -244,7 +244,7 @@ class LifeCoreUtilCommand(val plugin: LifeCore) : TabExecutor {
}
val item = CraftItemStack.asNMSCopy((player as Player).inventory.itemInMainHand)
item.tag = MojangsonParser.parse(args.joinToString(" "))
(player as Player).inventory.setItemInMainHand(CraftItemStack.asBukkitCopy(item))
player.inventory.setItemInMainHand(CraftItemStack.asBukkitCopy(item))
}
},
GetBlock {
Expand Down Expand Up @@ -300,7 +300,7 @@ class LifeCoreUtilCommand(val plugin: LifeCore) : TabExecutor {
ClearPlot("イマサラタウンのplotを消し飛ばします") {
override fun execute(plugin: LifeCore, player: CommandSender, args: Array<String>) {
val minX = (player as Player).chunk.x * 16
val minZ = (player as Player).chunk.z * 16
val minZ = player.chunk.z * 16
val maxX = minX + 15
val maxZ = minZ + 15
scheduleLater(plugin, 0) { Bukkit.dispatchCommand(player, "/chunk") }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.mori01231.lifecore.command

import com.github.mori01231.lifecore.LifeCore
import com.github.mori01231.lifecore.util.ItemUtil
import com.github.mori01231.lifecore.util.JoinFilterUtil
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

class TownServerCommand(private val plugin: LifeCore) : TransferCommand(plugin, "lifetown") {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
val player = sender as? Player ?: return false
if (plugin.config.getBoolean("enable-backup-item-tag", true)) {
val size = player.inventory.size
for (i in 0..<size) {
player.inventory.setItem(i, ItemUtil.restoreTag(player.inventory.getItem(i)))
player.inventory.setItem(i, ItemUtil.backupTag(player.inventory.getItem(i)))
}
}
JoinFilterUtil.addPlayerWithExpire(player.uniqueId, "lifetown", 1)
super.onCommand(sender, command, label, args)
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.github.mori01231.lifecore.LifeCore;
import com.github.mori01231.lifecore.network.PacketHandler;
import com.github.mori01231.lifecore.util.ItemUtil;
import com.github.mori01231.lifecore.util.PlayerUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -36,6 +38,35 @@ public void onPlayerJoin(PlayerJoinEvent e) {
// inject channel handler
PlayerUtil.getChannel(e.getPlayer()).pipeline()
.addBefore("packet_handler", "lifecore", new PacketHandler(e.getPlayer()));

Bukkit.getScheduler().runTaskLater(plugin, () -> {
Player player = Bukkit.getPlayer(e.getPlayer().getUniqueId());
if (player == null || !player.isOnline()) {
return;
}

// restore item tags
if (plugin.getConfig().getBoolean("enable-backup-item-tag", true)) {
int size = player.getInventory().getSize();
for (int i = 0; i < size; i++) {
player.getInventory().setItem(i, ItemUtil.restoreTag(player.getInventory().getItem(i)));
}
}
}, 20 * 2); // 2 seconds
Bukkit.getScheduler().runTaskLater(plugin, () -> {
Player player = Bukkit.getPlayer(e.getPlayer().getUniqueId());
if (player == null || !player.isOnline()) {
return;
}

// restore item tags
if (plugin.getConfig().getBoolean("enable-backup-item-tag", true)) {
int size = player.getInventory().getSize();
for (int i = 0; i < size; i++) {
player.getInventory().setItem(i, ItemUtil.restoreTag(player.getInventory().getItem(i)));
}
}
}, 20 * 10); // 10 seconds
}

@EventHandler
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/com/github/mori01231/lifecore/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ public static boolean isProbablyAdminSword(@Nullable ItemStack stack) {
return CraftItemStack.asBukkitCopy(nms);
}

public static @NotNull ItemStack setTag(@Nullable ItemStack stack, @NotNull String key, @NotNull NBTBase nbt) {
public static @NotNull ItemStack setTag(@Nullable ItemStack stack, @Nullable String key, @NotNull NBTBase nbt) {
if (stack == null || stack.getType().isAir()) return new ItemStack(Material.AIR);
net.minecraft.server.v1_15_R1.ItemStack nms = CraftItemStack.asNMSCopy(stack);
NBTTagCompound tag = nms.getOrCreateTag();
tag.set(key, nbt);
if (key == null) {
if (!(nbt instanceof NBTTagCompound)) {
throw new IllegalArgumentException("key is null, but nbt is not NBTTagCompound");
}
nms.setTag((NBTTagCompound) nbt);
} else {
NBTTagCompound tag = nms.getOrCreateTag();
tag.set(key, nbt);
}
return CraftItemStack.asBukkitCopy(nms);
}

Expand Down Expand Up @@ -134,4 +141,22 @@ public static boolean isEquippedInAnySlot(@NotNull Player player, @NotNull Strin
}
return false;
}

@Contract("null -> null")
public static ItemStack backupTag(@Nullable ItemStack stack) {
if (stack == null || stack.getType().isAir()) return null;
NBTTagCompound tag = CraftItemStack.asNMSCopy(stack).getTag();
if (tag == null || tag.isEmpty()) return stack;
return setTag(stack, "backup", tag);
}

@Contract("null -> null")
public static ItemStack restoreTag(@Nullable ItemStack stack) {
if (stack == null || stack.getType().isAir()) return null;
NBTTagCompound tag = CraftItemStack.asNMSCopy(stack).getTag();
if (tag == null) return stack;
NBTTagCompound backup = tag.getCompound("backup");
if (backup == null || backup.isEmpty()) return stack;
return setTag(stack, null, backup);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.mori01231.lifecore.util;

import com.github.mori01231.lifecore.util.external.JoinFilterBridge;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class JoinFilterUtil {
public static boolean addPlayerWithExpire(@NotNull UUID uuid, @NotNull String server, long expireSeconds) {
if (Bukkit.getPluginManager().isPluginEnabled("JoinFilter")) {
JoinFilterBridge.addPlayerWithExpire(uuid, server, expireSeconds);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.mori01231.lifecore.util.external;

import net.azisaba.joinfilter.JoinFilter;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class JoinFilterBridge {
public static void addPlayerWithExpire(@NotNull UUID uuid, @NotNull String server, long expireSeconds) {
JoinFilter.getPlugin(JoinFilter.class).addPlayerWithExpire(uuid, server, expireSeconds);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

lp-server-name: life

# enable backup of item tag when executing /townserver, and restore when joining.
enable-backup-item-tag: true

use-pve-command-as-teleport: true

website-url: https://azisaba.net/
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ softdepend:
- Towny
- MythicMobs
- ItemStash
- JoinFilter
api-version: 1.13

commands:
Expand Down

0 comments on commit ef6a9db

Please sign in to comment.