Added NMS code for 1.15

MMOCore should now be fully compatible with 1.15
(Spigot 1_15_R1)
This commit is contained in:
Aria 2019-12-11 04:17:30 +01:00
parent 7342f5420b
commit bbb0b08c0a
3 changed files with 229 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,229 @@
package net.Indyuce.mmocore.version.nms;
import java.lang.reflect.Field;
import java.util.Set;
import java.util.UUID;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.reflect.FieldUtils;
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.util.BoundingBox;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import net.Indyuce.mmocore.api.item.NBTItem;
import net.minecraft.server.v1_15_R1.BlockPosition;
import net.minecraft.server.v1_15_R1.ChatMessage;
import net.minecraft.server.v1_15_R1.ChatMessageType;
import net.minecraft.server.v1_15_R1.Container;
import net.minecraft.server.v1_15_R1.ContainerAccess;
import net.minecraft.server.v1_15_R1.ContainerAnvil;
import net.minecraft.server.v1_15_R1.Containers;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_15_R1.ItemStack;
import net.minecraft.server.v1_15_R1.NBTTagCompound;
import net.minecraft.server.v1_15_R1.PacketPlayOutChat;
import net.minecraft.server.v1_15_R1.PacketPlayOutCloseWindow;
import net.minecraft.server.v1_15_R1.PacketPlayOutOpenWindow;
import net.minecraft.server.v1_15_R1.PacketPlayOutTitle;
import net.minecraft.server.v1_15_R1.PacketPlayOutTitle.EnumTitleAction;
import net.minecraft.server.v1_15_R1.TileEntitySkull;
public class NMSHandler_1_15_R1 implements NMSHandler {
@Override
public void sendJson(Player player, String message) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutChat(ChatSerializer.a(message)));
}
@Override
public void sendTitle(Player player, String msgTitle, String msgSubTitle, int fadeIn, int ticks, int fadeOut) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\": \"" + msgTitle + "\"}")));
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\": \"" + msgSubTitle + "\"}")));
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutTitle(EnumTitleAction.TIMES, null, fadeIn, ticks, fadeOut));
}
@Override
public void sendActionBar(Player player, String message) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutChat(ChatSerializer.a("{\"text\": \"" + message + "\"}"), ChatMessageType.GAME_INFO));
}
@Override
public int getNextContainerId(Player player) {
return toNMS(player).nextContainerCounter();
}
@Override
public void handleInventoryCloseEvent(Player player) {
CraftEventFactory.handleInventoryCloseEvent(toNMS(player));
}
@Override
public void sendPacketOpenWindow(Player player, int containerId) {
toNMS(player).playerConnection.sendPacket(new PacketPlayOutOpenWindow(containerId, Containers.ANVIL, new ChatMessage("Repair & Name")));
}
@Override
public void sendPacketCloseWindow(Player player, int containerId) {
toNMS(player).playerConnection.sendPacket(new PacketPlayOutCloseWindow(containerId));
}
@Override
public void setActiveContainerDefault(Player player) {
toNMS(player).activeContainer = toNMS(player).defaultContainer;
}
@Override
public void setActiveContainer(Player player, Object container) {
toNMS(player).activeContainer = (Container) container;
}
@Override
public void setActiveContainerId(Object container, int containerId) {
Field field = null;
try {
field = Container.class.getField("windowId");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
FieldUtils.removeFinalModifier(field);
try {
FieldUtils.writeField(field, container, containerId);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void addActiveContainerSlotListener(Object container, Player player) {
((Container) container).addSlotListener(toNMS(player));
}
@Override
public Inventory toBukkitInventory(Object container) {
return ((Container) container).getBukkitView().getTopInventory();
}
@Override
public Object newContainerAnvil(Player player) {
return new AnvilContainer(player);
}
private EntityPlayer toNMS(Player player) {
return ((CraftPlayer) player).getHandle();
}
private class AnvilContainer extends ContainerAnvil {
public AnvilContainer(Player player) {
super(getNextContainerId(player), ((CraftPlayer) player).getHandle().inventory, ContainerAccess.at(((CraftWorld) player.getWorld()).getHandle(), new BlockPosition(0, 0, 0)));
this.checkReachable = false;
setTitle(new ChatMessage("Repair & Name"));
}
}
@Override
public NBTItem getNBTItem(org.bukkit.inventory.ItemStack item) {
return new NBTItem_v1_13_2(item);
}
public class NBTItem_v1_13_2 extends NBTItem {
private final ItemStack nms;
private final NBTTagCompound compound;
public NBTItem_v1_13_2(org.bukkit.inventory.ItemStack item) {
super(item);
nms = CraftItemStack.asNMSCopy(item);
compound = nms.hasTag() ? nms.getTag() : new NBTTagCompound();
}
@Override
public String getString(String path) {
return compound.getString(path);
}
@Override
public boolean has(String path) {
return compound.hasKey(path);
}
@Override
public boolean getBoolean(String path) {
return compound.getBoolean(path);
}
@Override
public double getDouble(String path) {
return compound.getDouble(path);
}
@Override
public int getInt(String path) {
return compound.getInt(path);
}
@Override
public NBTItem add(ItemTag... tags) {
for (ItemTag tag : tags) {
if (tag.getValue() instanceof Boolean)
compound.setBoolean(tag.getPath(), (boolean) tag.getValue());
else if (tag.getValue() instanceof Double)
compound.setDouble(tag.getPath(), (double) tag.getValue());
else if (tag.getValue() instanceof String)
compound.setString(tag.getPath(), (String) tag.getValue());
else if (tag.getValue() instanceof Integer)
compound.setInt(tag.getPath(), (int) tag.getValue());
}
return this;
}
@Override
public NBTItem remove(String... paths) {
for (String path : paths)
compound.remove(path);
return this;
}
@Override
public Set<String> getTags() {
return compound.getKeys();
}
@Override
public org.bukkit.inventory.ItemStack toItem() {
nms.setTag(compound);
return CraftItemStack.asBukkitCopy(nms);
}
}
@Override
public BoundingBox getBoundingBox(Entity target) {
return target.getBoundingBox();
}
@Override
public String getSkullValue(Block block) {
TileEntitySkull skullTile = (TileEntitySkull)((CraftWorld)block.getWorld()).getHandle().getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if(skullTile.gameProfile == null) return "";
return skullTile.gameProfile.getProperties().get("textures").iterator().next().getValue();
}
@Override
public void setSkullValue(Block block, String value) {
TileEntitySkull skullTile = (TileEntitySkull)((CraftWorld)block.getWorld()).getHandle().getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
profile.getProperties().put("textures", new Property("textures", value));
skullTile.setGameProfile(profile);
skullTile.update();
}
}