3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-09-17 01:23:43 +02:00

Add item-cache as a less expensive way of getting items. (Will rewrite eventually to not depend on Bukkit)

Dieser Commit ist enthalten in:
Myles 2016-04-17 00:52:35 +01:00
Ursprung 57d58db35e
Commit 4f8a248c6b
4 geänderte Dateien mit 66 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -10,6 +10,7 @@ import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.remapper.ValueTransformer;
@ -62,23 +63,27 @@ public class Protocol1_9TO1_8 extends Protocol {
return line; return line;
} }
public static ItemStack getHandItem(final UserConnection info) { public static Item getHandItem(final UserConnection info) {
try { if(HandItemCache.CACHE){
return Bukkit.getScheduler().callSyncMethod(Bukkit.getPluginManager().getPlugin("ViaVersion"), new Callable<ItemStack>() { return HandItemCache.getHandItem(info.get(ProtocolInfo.class).getUuid());
@Override }else {
public ItemStack call() throws Exception { try {
UUID playerUUID = info.get(ProtocolInfo.class).getUuid(); return Bukkit.getScheduler().callSyncMethod(Bukkit.getPluginManager().getPlugin("ViaVersion"), new Callable<Item>() {
if (Bukkit.getPlayer(playerUUID) != null) { @Override
return Bukkit.getPlayer(playerUUID).getItemInHand(); public Item call() throws Exception {
UUID playerUUID = info.get(ProtocolInfo.class).getUuid();
if (Bukkit.getPlayer(playerUUID) != null) {
return Item.getItem(Bukkit.getPlayer(playerUUID).getItemInHand());
}
return null;
} }
return null; }).get(10, TimeUnit.SECONDS);
} } catch (Exception e) {
}).get(10, TimeUnit.SECONDS); System.out.println("Error fetching hand item: " + e.getClass().getName());
} catch (Exception e) { if (ViaVersion.getInstance().isDebug())
System.out.println("Error fetching hand item: " + e.getClass().getName()); e.printStackTrace();
if (ViaVersion.getInstance().isDebug()) return null;
e.printStackTrace(); }
return null;
} }
} }
@ -104,6 +109,10 @@ public class Protocol1_9TO1_8 extends Protocol {
} }
if (plugin.getConfig().getBoolean("simulate-pt", true)) if (plugin.getConfig().getBoolean("simulate-pt", true))
new ViaIdleThread(plugin.getPortedPlayers()).runTaskTimerAsynchronously(plugin, 1L, 1L); // Updates player's idle status new ViaIdleThread(plugin.getPortedPlayers()).runTaskTimerAsynchronously(plugin, 1L, 1L); // Updates player's idle status
if (plugin.getConfig().getBoolean("item-cache", true)) {
new HandItemCache().runTaskTimerAsynchronously(plugin, 2L, 2L); // Updates player's items :)
HandItemCache.CACHE = true;
}
} }
@Override @Override

Datei anzeigen

@ -0,0 +1,36 @@
package us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import us.myles.ViaVersion.api.minecraft.item.Item;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class HandItemCache extends BukkitRunnable {
public static boolean CACHE = false;
private static ConcurrentHashMap<UUID, Item> handCache = new ConcurrentHashMap<>();
public static Item getHandItem(UUID player) {
if (!handCache.containsKey(player))
return null;
return handCache.get(player);
}
@Override
public void run() {
List<UUID> players = new ArrayList<>(handCache.keySet());
for (Player p : Bukkit.getOnlinePlayers()) {
handCache.put(p.getUniqueId(), Item.getItem(p.getItemInHand()));
players.remove(p.getUniqueId());
}
// Remove offline players
for (UUID uuid : players) {
handCache.remove(uuid);
}
}
}

Datei anzeigen

@ -288,7 +288,7 @@ public class WorldPackets {
wrapper.write(Type.LONG, -1L); wrapper.write(Type.LONG, -1L);
wrapper.write(Type.BYTE, (byte) 255); wrapper.write(Type.BYTE, (byte) 255);
// Write item in hand // Write item in hand
Item item = Item.getItem(Protocol1_9TO1_8.getHandItem(wrapper.user())); Item item = Protocol1_9TO1_8.getHandItem(wrapper.user());
// Blocking patch // Blocking patch
if (ViaVersion.getConfig().isShieldBlocking()) { if (ViaVersion.getConfig().isShieldBlocking()) {
if (item != null) { if (item != null) {
@ -368,7 +368,7 @@ public class WorldPackets {
create(new ValueCreator() { create(new ValueCreator() {
@Override @Override
public void write(PacketWrapper wrapper) throws Exception { public void write(PacketWrapper wrapper) throws Exception {
Item item = Item.getItem(Protocol1_9TO1_8.getHandItem(wrapper.user())); Item item = Protocol1_9TO1_8.getHandItem(wrapper.user());
wrapper.write(Type.ITEM, item); // 3 - Item wrapper.write(Type.ITEM, item); // 3 - Item
} }
}); });

Datei anzeigen

@ -28,4 +28,6 @@ use-new-deathmessages: false
# This error message means one of you plugins is sending bad packets! # This error message means one of you plugins is sending bad packets!
suppress-entityid-errors: false suppress-entityid-errors: false
# Our patch for block breaking issue, if you have issues with block updates then disable this. # Our patch for block breaking issue, if you have issues with block updates then disable this.
block-break-patch: true block-break-patch: true
# Should we cache our items, this will prevent server from being lagged out, however the cost is a constant task caching items
item-cache: true