Skip to content

Commit

Permalink
Merge pull request #28 from TWME-TW/fix-custommodeldata
Browse files Browse the repository at this point in the history
fix: CustomModelData not update correctly
  • Loading branch information
TWME-TW authored Dec 1, 2024
2 parents f310a0a + 39ace9b commit 7134e4b
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.twme</groupId>
<artifactId>DebugStickPro</artifactId>
<version>0.4.11</version>
<version>0.4.12</version>
<packaging>jar</packaging>

<name>DebugStickPro</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.twme.debugstickpro.mode.freeze.FreezeActionBarDisplay;
import dev.twme.debugstickpro.playerdata.PlayerData;
import dev.twme.debugstickpro.playerdata.PlayerDataManager;
import dev.twme.debugstickpro.utils.DebugStickItem;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
Expand All @@ -30,6 +31,11 @@ public void run() {
continue;
}

if (!DebugStickItem.checkPlayer(player)) {
PlayerDataManager.removePlayerFromDisplayList(uuid);
continue;
}

PlayerData playerData = PlayerDataManager.getPlayerData(uuid);

switch (playerData.getDebugStickMode()) {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/dev/twme/debugstickpro/display/ActionbarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Set;
import java.util.UUID;

public class ActionbarUtil {

private static final Set<UUID> lastIsRemove = new java.util.HashSet<>();
public static void removeActionBar(UUID uuid) {
if (Bukkit.getPlayer(uuid) != null) {
Bukkit.getPlayer(uuid).sendActionBar(Component.text(" "));
Player player = Bukkit.getPlayer(uuid);
if (lastIsRemove.contains(uuid)) {
return;
}
if (player != null) {
player.sendActionBar(Component.text(" "));
lastIsRemove.add(uuid);
}
}

public static void sendActionBar(Player player, String message) {
var mm = MiniMessage.miniMessage();
Component parsed = mm.deserialize(message);
player.sendActionBar(parsed);
lastIsRemove.remove(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void onPlayerItemHeldEvent(PlayerItemHeldEvent event) {
UUID playerUUID = player.getUniqueId();

if (DebugStickItem.isDebugStickItem(newItem)) {
CustomModelDataManager.updatePlayerMode(player);
CustomModelDataManager.updatePlayerMode(player, newItem);
PlayerDataManager.addPlayerToDisplayList(playerUUID);
} else if (DebugStickItem.isDebugStickItem(oldItem)) {
PlayerDataManager.removePlayerFromDisplayList(playerUUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
import java.util.UUID;

public final class AutoCheckCanChangeUtil {
/**
* Check if the player can change the block
*
* @param playerUUID player UUID
* @param block block
* @return true if the player can change the block
*/
public static boolean canChange(UUID playerUUID, Block block) {
Player player = Bukkit.getPlayer(playerUUID);
World world = block.getWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@
import org.bukkit.entity.Player;

public final class CheckPlayerCanUseUtil {
/**
* check player can use
*
* @param player player
* @return can use
*/
public static boolean check(Player player) {
return check(player, true);
}

/**
* check player can use
*
* @param player player
* @param checkItem check item
* @return can use
*/
public static boolean check(Player player, boolean checkItem) {
if (player == null) {
return false;
Expand All @@ -29,6 +42,11 @@ public static boolean check(Player player, boolean checkItem) {
return true;
}

/**
* remove player from display list
*
* @param player player
*/
private static void removePlayerFromDisplayList(Player player) {
PlayerDataManager.removePlayerFromDisplayList(player.getUniqueId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,64 @@
import dev.twme.debugstickpro.playerdata.PlayerDataManager;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

/**
* CustomModelDataManager
*/
public class CustomModelDataManager {
public static void updateItem(Player player, DebugStickMode newMode) {
/**
* updateItem
* @param player Player
* @param newMode DebugStickMode new mode
* @return boolean true: success, false: failed
*/
public static boolean updateItem(Player player, DebugStickMode newMode) {
ItemStack item = player.getInventory().getItemInMainHand();
if(DebugStickItem.isDebugStickItem(item)) {
item.getItemMeta().setCustomModelData(getCustomModelData(newMode));
item.getItemMeta().getPersistentDataContainer().set(PersistentKeys.DEBUG_STICK_MODE, PersistentDataType.STRING, newMode.name());
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setCustomModelData(getCustomModelData(newMode));
itemMeta.getPersistentDataContainer().set(PersistentKeys.DEBUG_STICK_MODE, PersistentDataType.STRING, newMode.name());
item.setItemMeta(itemMeta);
player.getInventory().setItemInMainHand(item);
player.updateInventory();
return true;
}
return false;
}

/**
* updatePlayerMode
* @param player Player
* @return boolean true: success, false: failed
*/
public static boolean updatePlayerMode(Player player) {
ItemStack item = player.getInventory().getItemInMainHand();
return updatePlayerMode(player, item);
}

public static void updatePlayerMode(Player player) {
if (!DebugStickItem.checkPlayer(player)) {
return;
/**
* updatePlayerMode
* @param player Player
* @param item ItemStack
* @return boolean true: success, false: failed
*/
public static boolean updatePlayerMode(Player player, ItemStack item) {
if (!DebugStickItem.isDebugStickItem(item)) {
return false;
}

ItemStack item = player.getInventory().getItemInMainHand();
DebugStickMode mode = DebugStickMode.valueOf(item.getItemMeta().getPersistentDataContainer().get(PersistentKeys.DEBUG_STICK_MODE, PersistentDataType.STRING));
PlayerDataManager.getPlayerData(player.getUniqueId()).setDebugStickMode(mode);
return true;
}

/**
* getCustomModelData
* @param mode DebugStickMode
* @return int
*/
private static int getCustomModelData(DebugStickMode mode) {

if (mode == DebugStickMode.COPY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public static boolean isDebugStickItem(ItemStack item) {
if (!item.getItemMeta().getPersistentDataContainer().has(PersistentKeys.DEBUG_STICK_ITEM)) {
return false;
}
// if (ConfigFile.DebugStickItem.CustomModelData.Enabled) {
// if (!item.getItemMeta().hasCustomModelData()) {
// return false;
// }
// return item.getItemMeta().getCustomModelData() == ConfigFile.DebugStickItem.CustomModelData.CustomModelData;
// }
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/dev/twme/debugstickpro/utils/Log.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.twme.debugstickpro.utils;

import dev.twme.debugstickpro.DebugStickPro;
import org.bukkit.Bukkit;

import java.util.HashMap;
Expand All @@ -10,14 +11,14 @@ public final class Log {
private static final String prefix = "[DebugStickPro] ";

public static void info(String message) {
Bukkit.getLogger().info(prefix + message);
DebugStickPro.getInstance().getLogger().info(prefix + message);
}

public static void warning(String message) {
if (warnCooldown.containsKey(message) && warnCooldown.get(message) + 10 * 1000 > System.currentTimeMillis()) {
return;
}
Bukkit.getLogger().warning(prefix + message);
DebugStickPro.getInstance().getLogger().warning(prefix + message);
warnCooldown.put(message, System.currentTimeMillis());
}

Expand Down

0 comments on commit 7134e4b

Please sign in to comment.