3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-20 06:50:08 +01:00

Refactor code to make field / class names better

Dieser Commit ist enthalten in:
Myles 2017-10-08 15:33:43 +01:00
Ursprung 471f1ae71d
Commit e372a56827
11 geänderte Dateien mit 97 neuen und 86 gelöschten Zeilen

Datei anzeigen

@ -174,6 +174,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform {
}
}
@Override
public TaskId runSync(Runnable runnable, Long ticks) {
return new BukkitTaskId(getServer().getScheduler().runTaskLater(this, runnable, ticks).getTaskId());
}
@Override
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
return new BukkitTaskId(getServer().getScheduler().runTaskTimer(this, runnable, ticks, 0).getTaskId());

Datei anzeigen

@ -14,11 +14,11 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.platform.ViaPlatformLoader;
import us.myles.ViaVersion.bukkit.listeners.UpdateListener;
import us.myles.ViaVersion.bukkit.listeners.protocol1_9to1_8.*;
import us.myles.ViaVersion.bukkit.providers.BukkitInvContainerItemProvider;
import us.myles.ViaVersion.bukkit.providers.BukkitInventoryQuickMoveProvider;
import us.myles.ViaVersion.bukkit.providers.BukkitViaBulkChunkTranslator;
import us.myles.ViaVersion.bukkit.providers.BukkitViaMovementTransmitter;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InvContainerItemProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
@ -65,7 +65,7 @@ public class BukkitViaLoader implements ViaPlatformLoader {
Via.getManager().getProviders().use(BulkChunkTranslatorProvider.class, new BukkitViaBulkChunkTranslator());
Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BukkitViaMovementTransmitter());
if (plugin.getConf().is1_12QuickMoveActionFix()) {
Via.getManager().getProviders().use(InvContainerItemProvider.class, new BukkitInvContainerItemProvider());
Via.getManager().getProviders().use(InventoryQuickMoveProvider.class, new BukkitInventoryQuickMoveProvider());
}
Via.getManager().getProviders().use(HandItemProvider.class, new HandItemProvider() {
@Override

Datei anzeigen

@ -1,5 +1,19 @@
package us.myles.ViaVersion.bukkit.providers;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.bukkit.tasks.protocol1_12to1_11_1.BukkitInventoryUpdateTask;
import us.myles.ViaVersion.bukkit.util.NMSUtil;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.ItemTransaction;
import us.myles.ViaVersion.util.ReflectionUtil;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@ -7,63 +21,46 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider {
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.bukkit.protocol1_12to1_11_1.BukkitInvContainerUpdateTask;
import us.myles.ViaVersion.bukkit.util.NMSUtil;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InvContainerItemProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.InvItemStorage;
import us.myles.ViaVersion.util.ReflectionUtil;
public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
private static Map<UUID, BukkitInvContainerUpdateTask> updateTasks = new ConcurrentHashMap<UUID, BukkitInvContainerUpdateTask>();
private static Map<UUID, BukkitInventoryUpdateTask> updateTasks = new ConcurrentHashMap<UUID, BukkitInventoryUpdateTask>();
private boolean supported;
// packet class
private Class<?> wclickPacketClass;
private Class<?> windowClickPacketClass;
private Object clickTypeEnum;
// Use for nms
private Method nmsItemMethod;
private Method ephandle;
private Method craftPlayerHandle;
private Field connection;
private Method packetMethod;
public BukkitInvContainerItemProvider() {
public BukkitInventoryQuickMoveProvider() {
this.supported = isSupported();
setupReflection();
}
@Override
public boolean registerInvClickPacket(short windowId, short slotId, short anumber, UserConnection uconnection) {
public boolean registerQuickMove(short windowId, short slotId, short actionId, UserConnection userConnection) {
if (!supported) {
return false;
}
ProtocolInfo info = uconnection.get(ProtocolInfo.class);
ProtocolInfo info = userConnection.get(ProtocolInfo.class);
UUID uuid = info.getUuid();
BukkitInvContainerUpdateTask utask = updateTasks.get(uuid);
final boolean registered = utask != null;
BukkitInventoryUpdateTask updateTask = updateTasks.get(uuid);
final boolean registered = updateTask != null;
if (!registered) {
utask = new BukkitInvContainerUpdateTask(this, uuid);
updateTasks.put(uuid, utask);
updateTask = new BukkitInventoryUpdateTask(this, uuid);
updateTasks.put(uuid, updateTask);
}
// http://wiki.vg/index.php?title=Protocol&oldid=13223#Click_Window
utask.addItem(windowId, slotId, anumber);
updateTask.addItem(windowId, slotId, actionId);
if (!registered) {
scheduleTask(utask);
Via.getPlatform().runSync(updateTask, 5L);
}
return true;
}
public Object buildWindowClickPacket(Player p, InvItemStorage storage) {
public Object buildWindowClickPacket(Player p, ItemTransaction storage) {
if (!supported) {
return null;
}
@ -79,25 +76,25 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
if (itemstack == null) {
return null;
}
Object cinstance = null;
Object packet = null;
try {
cinstance = wclickPacketClass.newInstance();
packet = windowClickPacketClass.newInstance();
Object nmsItem = nmsItemMethod.invoke(null, itemstack);
ReflectionUtil.set(cinstance, "a", (int) storage.getWindowId());
ReflectionUtil.set(cinstance, "slot", (int) slotId);
ReflectionUtil.set(cinstance, "button", 0); // shift + left mouse click
ReflectionUtil.set(cinstance, "d", storage.getActionNumber());
ReflectionUtil.set(cinstance, "item", nmsItem);
ReflectionUtil.set(packet, "a", (int) storage.getWindowId());
ReflectionUtil.set(packet, "slot", (int) slotId);
ReflectionUtil.set(packet, "button", 0); // shift + left mouse click
ReflectionUtil.set(packet, "d", storage.getActionId());
ReflectionUtil.set(packet, "item", nmsItem);
int protocolId = ProtocolRegistry.SERVER_PROTOCOL;
if (protocolId == ProtocolVersion.v1_8.getId()) {
ReflectionUtil.set(cinstance, "shift", 1);
ReflectionUtil.set(packet, "shift", 1);
} else if (protocolId >= ProtocolVersion.v1_9.getId()) { // 1.9+
ReflectionUtil.set(cinstance, "shift", clickTypeEnum);
ReflectionUtil.set(packet, "shift", clickTypeEnum);
}
} catch (Exception e) {
e.printStackTrace();
}
return cinstance;
return packet;
}
public boolean sendPlayer(Player p, Object packet) {
@ -105,10 +102,10 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
return false;
}
try {
Object entityPlayer = ephandle.invoke(p);
Object pconnection = connection.get(entityPlayer);
Object entityPlayer = craftPlayerHandle.invoke(p);
Object playerConnection = connection.get(entityPlayer);
// send
packetMethod.invoke(pconnection, packet);
packetMethod.invoke(playerConnection, packet);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return false;
@ -120,46 +117,36 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
updateTasks.remove(uuid);
}
private void scheduleTask(BukkitInvContainerUpdateTask utask) {
BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
Plugin instance = Bukkit.getServer().getPluginManager().getPlugin("ViaVersion");
scheduler.runTaskLater(instance, utask, 5); // 5 ticks later (possible double click action).
}
private void setupReflection() {
if (!supported) {
return;
}
try {
this.wclickPacketClass = NMSUtil.nms("PacketPlayInWindowClick");
this.windowClickPacketClass = NMSUtil.nms("PacketPlayInWindowClick");
int protocolId = ProtocolRegistry.SERVER_PROTOCOL;
if (protocolId >= ProtocolVersion.v1_9.getId()) {
Class<?> eclassz = NMSUtil.nms("InventoryClickType");
Object[] constants = eclassz.getEnumConstants();
this.clickTypeEnum = constants[1]; // QUICK_MOVE
}
Class<?> citemStack = NMSUtil.obc("inventory.CraftItemStack");
this.nmsItemMethod = citemStack.getDeclaredMethod("asNMSCopy", ItemStack.class);
Class<?> craftItemStack = NMSUtil.obc("inventory.CraftItemStack");
this.nmsItemMethod = craftItemStack.getDeclaredMethod("asNMSCopy", ItemStack.class);
} catch (Exception e) {
this.supported = false;
throw new RuntimeException("Couldn't find required inventory classes", e);
}
try {
this.ephandle = NMSUtil.obc("entity.CraftPlayer").getDeclaredMethod("getHandle");
this.craftPlayerHandle = NMSUtil.obc("entity.CraftPlayer").getDeclaredMethod("getHandle");
} catch (NoSuchMethodException | ClassNotFoundException e) {
this.supported = false;
throw new RuntimeException("Couldn't find CraftPlayer", e);
}
try {
this.connection = NMSUtil.nms("EntityPlayer").getDeclaredField("playerConnection");
} catch (NoSuchFieldException | ClassNotFoundException e) {
this.supported = false;
throw new RuntimeException("Couldn't find Player Connection", e);
}
try {
this.packetMethod = NMSUtil.nms("PlayerConnection").getDeclaredMethod("a", wclickPacketClass);
this.packetMethod = NMSUtil.nms("PlayerConnection").getDeclaredMethod("a", windowClickPacketClass);
} catch (NoSuchMethodException | ClassNotFoundException e) {
this.supported = false;
throw new RuntimeException("Couldn't find CraftPlayer", e);
}
}

Datei anzeigen

@ -1,4 +1,4 @@
package us.myles.ViaVersion.bukkit.protocol1_12to1_11_1;
package us.myles.ViaVersion.bukkit.tasks.protocol1_12to1_11_1;
import java.util.ArrayList;
import java.util.Collections;
@ -8,23 +8,23 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import us.myles.ViaVersion.bukkit.providers.BukkitInvContainerItemProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.InvItemStorage;
import us.myles.ViaVersion.bukkit.providers.BukkitInventoryQuickMoveProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.ItemTransaction;
public class BukkitInvContainerUpdateTask implements Runnable {
public class BukkitInventoryUpdateTask implements Runnable {
private BukkitInvContainerItemProvider provider;
private BukkitInventoryQuickMoveProvider provider;
private final UUID uuid;
private final List<InvItemStorage> items;
private final List<ItemTransaction> items;
public BukkitInvContainerUpdateTask(BukkitInvContainerItemProvider provider, UUID uuid) {
public BukkitInventoryUpdateTask(BukkitInventoryQuickMoveProvider provider, UUID uuid) {
this.provider = provider;
this.uuid = uuid;
this.items = Collections.synchronizedList(new ArrayList<InvItemStorage>());
this.items = Collections.synchronizedList(new ArrayList<ItemTransaction>());
}
public void addItem(short windowId, short slotId, short anumber) {
InvItemStorage storage = new InvItemStorage(windowId, slotId, anumber);
public void addItem(short windowId, short slotId, short actionId) {
ItemTransaction storage = new ItemTransaction(windowId, slotId, actionId);
items.add(storage);
}
@ -37,7 +37,7 @@ public class BukkitInvContainerUpdateTask implements Runnable {
}
try {
synchronized (items) {
for (InvItemStorage storage : items) {
for (ItemTransaction storage : items) {
Object packet = provider.buildWindowClickPacket(p, storage);
boolean result = provider.sendPlayer(p, packet);
if (!result) {

Datei anzeigen

@ -79,6 +79,11 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener {
return runAsync(runnable);
}
@Override
public TaskId runSync(Runnable runnable, Long ticks) {
return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, ticks * 50, TimeUnit.MILLISECONDS).getId());
}
@Override
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, 0, ticks * 50, TimeUnit.MILLISECONDS).getId());

Datei anzeigen

@ -59,6 +59,16 @@ public interface ViaPlatform<T> {
*/
TaskId runSync(Runnable runnable);
/**
* Run a task Sync after a interval
* This must be only used after plugin enable.
*
* @param runnable The task to run
* @param ticks The interval to run it after
* @return The Task ID
*/
TaskId runSync(Runnable runnable, Long ticks);
/**
* Run a task at a repeating interval.
* Initial interval is the same as repeat.

Datei anzeigen

@ -20,7 +20,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_12;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.packets.InventoryPackets;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InvContainerItemProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker;
import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
@ -389,7 +389,7 @@ public class Protocol1_12To1_11_1 extends Protocol {
@Override
protected void register(ViaProviders providers) {
providers.register(InvContainerItemProvider.class, new InvContainerItemProvider());
providers.register(InventoryQuickMoveProvider.class, new InventoryQuickMoveProvider());
}
@Override

Datei anzeigen

@ -9,7 +9,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.BedRewriter;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.Protocol1_12To1_11_1;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InvContainerItemProvider;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
public class InventoryPackets {
public static void register(Protocol1_12To1_11_1 protocol) {
@ -131,9 +131,9 @@ public class InventoryPackets {
if (mode == 1 && button == 0 && item == null) {
short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
short slotId = wrapper.get(Type.SHORT, 0);
short anumber = wrapper.get(Type.SHORT, 1);
InvContainerItemProvider provider = Via.getManager().getProviders().get(InvContainerItemProvider.class);
boolean succeed = provider.registerInvClickPacket(windowId, slotId, anumber, wrapper.user());
short actionId = wrapper.get(Type.SHORT, 1);
InventoryQuickMoveProvider provider = Via.getManager().getProviders().get(InventoryQuickMoveProvider.class);
boolean succeed = provider.registerQuickMove(windowId, slotId, actionId, wrapper.user());
if (succeed) {
wrapper.cancel();
}

Datei anzeigen

@ -3,9 +3,9 @@ package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.providers.Provider;
public class InvContainerItemProvider implements Provider {
public class InventoryQuickMoveProvider implements Provider {
public boolean registerInvClickPacket(short windowId, short slotId, short anumber, UserConnection uconnection) {
public boolean registerQuickMove(short windowId, short slotId, short actionId, UserConnection userConnection) {
return false; // not supported :/ plays very sad violin
}
}

Datei anzeigen

@ -7,9 +7,8 @@ import lombok.ToString;
@AllArgsConstructor
@ToString
@Getter
public class InvItemStorage {
public class ItemTransaction {
private short windowId;
private short slotId;
private short actionNumber;
private short actionId;
}

Datei anzeigen

@ -3,7 +3,6 @@ package us.myles.ViaVersion;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
@ -113,6 +112,12 @@ public class SpongePlugin implements ViaPlatform {
return new SpongeTaskId(null);
}
@Override
public TaskId runSync(Runnable runnable, Long ticks) {
Long delay = ticks * 50L;
return new SpongeTaskId(syncExecutor.schedule(runnable, delay, TimeUnit.MILLISECONDS).getTask());
}
@Override
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
Long time = ticks * 50L;