2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2023-01-12 12:45:53 +01:00
|
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8;
|
2016-04-17 01:52:35 +02:00
|
|
|
|
2021-06-04 12:15:14 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.item.DataItem;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
2018-10-27 13:25:42 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2016-04-17 01:52:35 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2023-02-12 11:44:25 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2016-04-17 01:52:35 +02:00
|
|
|
|
|
|
|
public class HandItemCache extends BukkitRunnable {
|
2020-04-13 20:19:55 +02:00
|
|
|
|
2019-08-23 22:13:37 +02:00
|
|
|
private final Map<UUID, Item> handCache = new ConcurrentHashMap<>();
|
2016-04-17 01:52:35 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
List<UUID> players = new ArrayList<>(handCache.keySet());
|
|
|
|
|
|
|
|
for (Player p : Bukkit.getOnlinePlayers()) {
|
2016-09-25 19:06:04 +02:00
|
|
|
handCache.put(p.getUniqueId(), convert(p.getItemInHand()));
|
2016-04-17 01:52:35 +02:00
|
|
|
players.remove(p.getUniqueId());
|
|
|
|
}
|
|
|
|
// Remove offline players
|
|
|
|
for (UUID uuid : players) {
|
|
|
|
handCache.remove(uuid);
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 19:06:04 +02:00
|
|
|
|
2019-08-23 22:13:37 +02:00
|
|
|
public Item getHandItem(UUID player) {
|
|
|
|
return handCache.get(player);
|
|
|
|
}
|
|
|
|
|
2016-09-25 19:06:04 +02:00
|
|
|
public static Item convert(ItemStack itemInHand) {
|
2021-06-04 12:15:14 +02:00
|
|
|
if (itemInHand == null) return new DataItem(0, (byte) 0, (short) 0, null);
|
|
|
|
return new DataItem(itemInHand.getTypeId(), (byte) itemInHand.getAmount(), itemInHand.getDurability(), null);
|
2016-09-25 19:06:04 +02:00
|
|
|
}
|
2016-04-17 01:52:35 +02:00
|
|
|
}
|