ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

Back to src/main/java/com/scriptslab/core/item
J

ItemManagerImpl.java

Java · 206 lines · 6.5 KB

src/main/java/com/scriptslab/core/item/ItemManagerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.scriptslab.core.item;

import com.scriptslab.api.item.CustomItem;
import com.scriptslab.api.item.ItemAbility;
import com.scriptslab.api.item.ItemManager;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * Thread-safe implementation of ItemManager.
 */
public final class ItemManagerImpl implements ItemManager {
    
    private final Plugin plugin;
    private final Logger logger;
    private final Map<String, CustomItem> items;
    private final Map<String, ItemAbility> abilities;
    private final Map<UUID, Map<String, Long>> cooldowns;
    private final NamespacedKey itemIdKey;
    
    public ItemManagerImpl(Plugin plugin) {
        this.plugin = plugin;
        this.logger = Logger.getLogger("ItemManager");
        this.items = new ConcurrentHashMap<>();
        this.abilities = new ConcurrentHashMap<>();
        this.cooldowns = new ConcurrentHashMap<>();
        this.itemIdKey = new NamespacedKey(plugin, "custom_item_id");
    }
    
    @Override
    public CompletableFuture<Void> registerItem(CustomItem item) {
        return CompletableFuture.runAsync(() -> {
            if (item == null) {
                throw new IllegalArgumentException("Item cannot be null");
            }
            
            items.put(item.getId(), item);
            
            // Register abilities
            for (ItemAbility ability : item.getAbilities()) {
                abilities.put(ability.getId(), ability);
            }
            
            logger.fine("Registered item: " + item.getId());
        });
    }
    
    @Override
    public CompletableFuture<Void> unregisterItem(String itemId) {
        return CompletableFuture.runAsync(() -> {
            CustomItem item = items.remove(itemId);
            if (item != null) {
                // Unregister abilities
                for (ItemAbility ability : item.getAbilities()) {
                    abilities.remove(ability.getId());
                }
                logger.fine("Unregistered item: " + itemId);
            }
        });
    }
    
    @Override
    public Optional<CustomItem> getItem(String itemId) {
        return Optional.ofNullable(items.get(itemId));
    }
    
    @Override
    public Collection<CustomItem> getAllItems() {
        return Collections.unmodifiableCollection(items.values());
    }
    
    @Override
    public boolean isCustomItem(ItemStack itemStack) {
        if (itemStack == null || !itemStack.hasItemMeta()) {
            return false;
        }
        
        ItemMeta meta = itemStack.getItemMeta();
        return meta.getPersistentDataContainer().has(itemIdKey, PersistentDataType.STRING);
    }
    
    @Override
    public Optional<String> getCustomItemId(ItemStack itemStack) {
        if (!isCustomItem(itemStack)) {
            return Optional.empty();
        }
        
        ItemMeta meta = itemStack.getItemMeta();
        String id = meta.getPersistentDataContainer().get(itemIdKey, PersistentDataType.STRING);
        return Optional.ofNullable(id);
    }
    
    @Override
    public Optional<CustomItem> getCustomItem(ItemStack itemStack) {
        return getCustomItemId(itemStack)
                .flatMap(this::getItem);
    }
    
    @Override
    public Optional<ItemStack> createItemStack(String itemId, int amount) {
        CustomItem item = items.get(itemId);
        if (item == null) {
            return Optional.empty();
        }
        
        ItemStack itemStack = item.toItemStack(amount);
        
        // Add custom item ID to NBT
        ItemMeta meta = itemStack.getItemMeta();
        if (meta != null) {
            meta.getPersistentDataContainer().set(itemIdKey, PersistentDataType.STRING, itemId);
            itemStack.setItemMeta(meta);
        }
        
        return Optional.of(itemStack);
    }
    
    @Override
    public CompletableFuture<Void> registerAbility(ItemAbility ability) {
        return CompletableFuture.runAsync(() -> {
            if (ability == null) {
                throw new IllegalArgumentException("Ability cannot be null");
            }
            
            abilities.put(ability.getId(), ability);
            logger.fine("Registered ability: " + ability.getId());
        });
    }
    
    @Override
    public Optional<ItemAbility> getAbility(String abilityId) {
        return Optional.ofNullable(abilities.get(abilityId));
    }
    
    @Override
    public boolean canUseAbility(UUID playerId, String abilityId) {
        Map<String, Long> playerCooldowns = cooldowns.get(playerId);
        if (playerCooldowns == null) {
            return true;
        }
        
        Long cooldownEnd = playerCooldowns.get(abilityId);
        if (cooldownEnd == null) {
            return true;
        }
        
        long currentTime = System.currentTimeMillis();
        return currentTime >= cooldownEnd;
    }
    
    @Override
    public void setCooldown(UUID playerId, String abilityId, long cooldownTicks) {
        long cooldownMillis = cooldownTicks * 50; // 1 tick = 50ms
        long cooldownEnd = System.currentTimeMillis() + cooldownMillis;
        
        cooldowns.computeIfAbsent(playerId, k -> new ConcurrentHashMap<>())
                .put(abilityId, cooldownEnd);
    }
    
    @Override
    public long getRemainingCooldown(UUID playerId, String abilityId) {
        Map<String, Long> playerCooldowns = cooldowns.get(playerId);
        if (playerCooldowns == null) {
            return 0;
        }
        
        Long cooldownEnd = playerCooldowns.get(abilityId);
        if (cooldownEnd == null) {
            return 0;
        }
        
        long currentTime = System.currentTimeMillis();
        long remaining = cooldownEnd - currentTime;
        
        if (remaining <= 0) {
            playerCooldowns.remove(abilityId);
            return 0;
        }
        
        return remaining / 50; // Convert to ticks
    }
    
    /**
     * Cleans up expired cooldowns.
     * Should be called periodically.
     */
    public void cleanupCooldowns() {
        long currentTime = System.currentTimeMillis();
        
        cooldowns.values().forEach(playerCooldowns -> 
            playerCooldowns.entrySet().removeIf(entry -> entry.getValue() <= currentTime)
        );
        
        // Remove empty player cooldown maps
        cooldowns.entrySet().removeIf(entry -> entry.getValue().isEmpty());
    }
}