Reformatting and cleaning.

This commit is contained in:
MattBDev 2019-04-24 16:11:24 -04:00
parent bef3dbc6f4
commit d0be0780fd
13 changed files with 995 additions and 1169 deletions

View File

@ -2,7 +2,7 @@ package com.github.intellectualsites.plotsquared.bukkit;
import com.github.intellectualsites.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.github.intellectualsites.plotsquared.bukkit.generator.BukkitPlotGenerator;
import com.github.intellectualsites.plotsquared.bukkit.listeners.*; import com.github.intellectualsites.plotsquared.bukkit.listeners.*;
import com.github.intellectualsites.plotsquared.bukkit.titles.DefaultTitle_111; import com.github.intellectualsites.plotsquared.bukkit.titles.DefaultTitle;
import com.github.intellectualsites.plotsquared.bukkit.util.*; import com.github.intellectualsites.plotsquared.bukkit.util.*;
import com.github.intellectualsites.plotsquared.bukkit.util.block.BukkitLocalQueue; import com.github.intellectualsites.plotsquared.bukkit.util.block.BukkitLocalQueue;
import com.github.intellectualsites.plotsquared.bukkit.uuid.*; import com.github.intellectualsites.plotsquared.bukkit.uuid.*;
@ -727,7 +727,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
+ " &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature."); + " &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature.");
Settings.TITLES = false; Settings.TITLES = false;
} else { } else {
AbstractTitle.TITLE_CLASS = new DefaultTitle_111(); AbstractTitle.TITLE_CLASS = new DefaultTitle();
if (wrapper instanceof DefaultUUIDWrapper if (wrapper instanceof DefaultUUIDWrapper
|| wrapper.getClass() == OfflineUUIDWrapper.class && !Bukkit.getOnlineMode()) { || wrapper.getClass() == OfflineUUIDWrapper.class && !Bukkit.getOnlineMode()) {
Settings.UUID.NATIVE_UUID_PROVIDER = true; Settings.UUID.NATIVE_UUID_PROVIDER = true;

View File

@ -12,25 +12,26 @@ import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent; import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
@SuppressWarnings("unused") public class WorldEvents implements Listener { @SuppressWarnings("unused")
public class WorldEvents implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWorldInit(WorldInitEvent event) { public void onWorldInit(WorldInitEvent event) {
World world = event.getWorld(); World world = event.getWorld();
String name = world.getName(); String name = world.getName();
PlotAreaManager manager = PlotSquared.get().getPlotAreaManager(); PlotAreaManager manager = PlotSquared.get().getPlotAreaManager();
if (manager instanceof SinglePlotAreaManager) { if (manager instanceof SinglePlotAreaManager) {
SinglePlotAreaManager single = (SinglePlotAreaManager) manager; SinglePlotAreaManager single = (SinglePlotAreaManager) manager;
if (single.isWorld(name)) { if (single.isWorld(name)) {
world.setKeepSpawnInMemory(false); world.setKeepSpawnInMemory(false);
return; return;
} }
}
ChunkGenerator gen = world.getGenerator();
if (gen instanceof GeneratorWrapper) {
PlotSquared.get().loadWorld(name, (GeneratorWrapper<?>) gen);
} else {
PlotSquared.get().loadWorld(name, new BukkitPlotGenerator(name, gen));
}
} }
ChunkGenerator gen = world.getGenerator();
if (gen instanceof GeneratorWrapper) {
PlotSquared.get().loadWorld(name, (GeneratorWrapper<?>) gen);
} else {
PlotSquared.get().loadWorld(name, new BukkitPlotGenerator(name, gen));
}
}
} }

View File

@ -282,6 +282,7 @@ public final class ReplicatingEntityWrapper extends EntityWrapper {
this.dataByte = (byte) 0; this.dataByte = (byte) 0;
} }
storeLiving((LivingEntity) entity); storeLiving((LivingEntity) entity);
return;
// END LIVING // // END LIVING //
default: default:
PlotSquared.debug("&cCOULD NOT IDENTIFY ENTITY: " + entity.getType()); PlotSquared.debug("&cCOULD NOT IDENTIFY ENTITY: " + entity.getType());
@ -317,8 +318,9 @@ public final class ReplicatingEntityWrapper extends EntityWrapper {
if (this.lived.leashed) { if (this.lived.leashed) {
// TODO leashes // TODO leashes
// World world = entity.getWorld(); // World world = entity.getWorld();
// Entity leash = world.spawnEntity(new Location(world, Math.floor(x) + lived.leashX, Math.floor(y) + lived.leashY, Math // Entity leash = world.spawnEntity(new Location(world, Math.floor(x) +
// .floor(z) + lived.leashZ), EntityType.LEASH_HITCH); // lived.leashX, Math.floor(y) + lived.leashY, Math.floor(z) + lived.leashZ),
// EntityType.LEASH_HITCH);
// entity.setLeashHolder(leash); // entity.setLeashHolder(leash);
} }
} }
@ -343,7 +345,7 @@ public final class ReplicatingEntityWrapper extends EntityWrapper {
} }
} }
public void storeLiving(LivingEntity lived) { private void storeLiving(LivingEntity lived) {
this.lived = new LivingEntityStats(); this.lived = new LivingEntityStats();
this.lived.potions = lived.getActivePotionEffects(); this.lived.potions = lived.getActivePotionEffects();
this.lived.loot = lived.getCanPickupItems(); this.lived.loot = lived.getCanPickupItems();

View File

@ -0,0 +1,19 @@
package com.github.intellectualsites.plotsquared.bukkit.titles;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.AbstractTitle;
import org.bukkit.entity.Player;
public class DefaultTitle extends AbstractTitle {
@Override
public void sendTitle(PlotPlayer player, String head, String sub, int in, int delay, int out) {
try {
final Player playerObj = ((BukkitPlayer) player).player;
TitleManager_1_11 title = new TitleManager_1_11(head, sub, in, delay, out);
title.send(playerObj);
} catch (Throwable ignored) {
}
}
}

View File

@ -9,121 +9,119 @@ import java.lang.reflect.Method;
public class DefaultTitleManager extends TitleManager { public class DefaultTitleManager extends TitleManager {
/** /**
* Create a new 1.8 title. * Create a new 1.8 title.
* *
* @param title Title text * @param title Title text
* @param subtitle Subtitle text * @param subtitle Subtitle text
* @param fadeInTime Fade in time * @param fadeInTime Fade in time
* @param stayTime Stay on screen time * @param stayTime Stay on screen time
* @param fadeOutTime Fade out time * @param fadeOutTime Fade out time
*/ */
DefaultTitleManager(String title, String subtitle, int fadeInTime, int stayTime, DefaultTitleManager(String title, String subtitle, int fadeInTime, int stayTime,
int fadeOutTime) { int fadeOutTime) {
super(title, subtitle, fadeInTime, stayTime, fadeOutTime); super(title, subtitle, fadeInTime, stayTime, fadeOutTime);
} }
/** /**
* Load spigot and NMS classes. * Load spigot and NMS classes.
*/ */
@Override void loadClasses() { @Override void loadClasses() {
this.packetTitle = Reflection.getNMSClass("PacketPlayOutTitle"); this.packetTitle = Reflection.getNMSClass("PacketPlayOutTitle");
this.packetActions = Reflection.getNMSClass("EnumTitleAction"); this.packetActions = Reflection.getNMSClass("EnumTitleAction");
this.chatBaseComponent = Reflection.getNMSClass("IChatBaseComponent"); this.chatBaseComponent = Reflection.getNMSClass("IChatBaseComponent");
this.nmsChatSerializer = Reflection.getNMSClass("ChatSerializer"); this.nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
} }
@Override public void send(Player player) @Override public void send(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException { throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
if (this.packetTitle != null) { if (this.packetTitle != null) {
// First reset previous settings // First reset previous settings
resetTitle(player); resetTitle(player);
// Send timings first // Send timings first
Object handle = getHandle(player); Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle); Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = this.packetActions.getEnumConstants(); Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket"); Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet = this.packetTitle Object packet = this.packetTitle
.getConstructor(this.packetActions, this.chatBaseComponent, Integer.TYPE, .getConstructor(this.packetActions, this.chatBaseComponent, Integer.TYPE, Integer.TYPE,
Integer.TYPE, Integer.TYPE) Integer.TYPE).newInstance(actions[2], null, this.fadeInTime * (this.ticks ? 1 : 20),
.newInstance(actions[2], null, this.fadeInTime * (this.ticks ? 1 : 20), this.stayTime * (this.ticks ? 1 : 20), this.fadeOutTime * (this.ticks ? 1 : 20));
this.stayTime * (this.ticks ? 1 : 20), // Send if set
this.fadeOutTime * (this.ticks ? 1 : 20)); if (this.fadeInTime != -1 && this.fadeOutTime != -1 && this.stayTime != -1) {
// Send if set
if (this.fadeInTime != -1 && this.fadeOutTime != -1 && this.stayTime != -1) {
sendPacket.invoke(connection, packet);
}
// Send title
Object serialized = getMethod(this.nmsChatSerializer, "a", String.class).invoke(null,
"{text:\"" + ChatColor.translateAlternateColorCodes('&', this.getTitle())
+ "\",color:" + this.titleColor.name().toLowerCase() + '}');
packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
.newInstance(actions[0], serialized);
sendPacket.invoke(connection, packet);
if (!this.getSubtitle().isEmpty()) {
// Send subtitle if present
serialized = getMethod(this.nmsChatSerializer, "a", String.class).invoke(null,
"{text:\"" + ChatColor.translateAlternateColorCodes('&', this.getSubtitle())
+ "\",color:" + this.subtitleColor.name().toLowerCase() + '}');
packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
.newInstance(actions[1], serialized);
sendPacket.invoke(connection, packet);
}
}
}
@Override public void clearTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
// Send timings first
Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
.newInstance(actions[3], null);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
} }
// Send title
/** Object serialized = getMethod(this.nmsChatSerializer, "a", String.class).invoke(null,
* Reset the title settings. "{text:\"" + ChatColor.translateAlternateColorCodes('&', this.getTitle()) + "\",color:"
* + this.titleColor.name().toLowerCase() + '}');
* @param player Player packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
* @throws SecurityException .newInstance(actions[0], serialized);
* @throws ReflectiveOperationException sendPacket.invoke(connection, packet);
* @throws SecurityException if (!this.getSubtitle().isEmpty()) {
*/ // Send subtitle if present
@Override public void resetTitle(Player player) serialized = getMethod(this.nmsChatSerializer, "a", String.class).invoke(null,
throws IllegalArgumentException, ReflectiveOperationException, SecurityException { "{text:\"" + ChatColor.translateAlternateColorCodes('&', this.getSubtitle())
// Send timings first + "\",color:" + this.subtitleColor.name().toLowerCase() + '}');
Object handle = getHandle(player); packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
Object connection = getField(handle.getClass(), "playerConnection").get(handle); .newInstance(actions[1], serialized);
Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
.newInstance(actions[4], null);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
}
} }
}
Field getField(Class<?> clazz, String name) { @Override public void clearTitle(Player player)
try { throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
Field field = clazz.getDeclaredField(name); // Send timings first
field.setAccessible(true); Object handle = getHandle(player);
return field; Object connection = getField(handle.getClass(), "playerConnection").get(handle);
} catch (NoSuchFieldException | SecurityException e) { Object[] actions = this.packetActions.getEnumConstants();
e.printStackTrace(); Method sendPacket = getMethod(connection.getClass(), "sendPacket");
return null; Object packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
} .newInstance(actions[3], null);
} sendPacket.invoke(connection, packet);
}
Method getMethod(Class<?> clazz, String name, Class<?>... args) { /**
for (Method m : clazz.getMethods()) { * Reset the title settings.
if (m.getName().equals(name) && (args.length == 0 || classListEqual(args, *
m.getParameterTypes()))) { * @param player Player
m.setAccessible(true); * @throws SecurityException
return m; * @throws ReflectiveOperationException
} * @throws SecurityException
} */
return null; @Override public void resetTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
// Send timings first
Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet = this.packetTitle.getConstructor(this.packetActions, this.chatBaseComponent)
.newInstance(actions[4], null);
sendPacket.invoke(connection, packet);
}
Field getField(Class<?> clazz, String name) {
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
return null;
} }
}
Method getMethod(Class<?> clazz, String name, Class<?>... args) {
for (Method m : clazz.getMethods()) {
if (m.getName().equals(name) && (args.length == 0 || classListEqual(args,
m.getParameterTypes()))) {
m.setAccessible(true);
return m;
}
}
return null;
}
} }

View File

@ -1,20 +0,0 @@
package com.github.intellectualsites.plotsquared.bukkit.titles;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.AbstractTitle;
import org.bukkit.entity.Player;
@SuppressWarnings("deprecation") public class DefaultTitle_111 extends AbstractTitle {
@Override
public void sendTitle(PlotPlayer player, String head, String sub, int in, int delay, int out) {
try {
final Player playerObj = ((BukkitPlayer) player).player;
TitleManager_1_11 title = new TitleManager_1_11(head, sub, in, delay, out);
title.send(playerObj);
return;
} catch (Throwable ignored) {
}
}
}

View File

@ -1,22 +0,0 @@
package com.github.intellectualsites.plotsquared.bukkit.titles;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.AbstractTitle;
public class HackTitle extends AbstractTitle {
@Override
public void sendTitle(PlotPlayer player, String head, String sub, int in, int delay, int out) {
try {
HackTitleManager title = new HackTitleManager(head, sub, in, delay, out);
title.send(((BukkitPlayer) player).player);
} catch (Exception ignored) {
PlotSquared.debug("&cYour server version does not support titles!");
Settings.TITLES = false;
AbstractTitle.TITLE_CLASS = null;
}
}
}

View File

@ -1,177 +0,0 @@
package com.github.intellectualsites.plotsquared.bukkit.titles;
import com.github.intellectualsites.plotsquared.bukkit.chat.Reflection;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class HackTitleManager extends TitleManager {
/**
* Create a new 1.8 title.
*
* @param title Title text
* @param subtitle Subtitle text
* @param fadeInTime Fade in time
* @param stayTime Stay on screen time
* @param fadeOutTime Fade out time
*/
HackTitleManager(String title, String subtitle, int fadeInTime, int stayTime, int fadeOutTime) {
super(title, subtitle, fadeInTime, stayTime, fadeOutTime);
}
/**
* Load spigot and NMS classes.
*/
@Override void loadClasses() {
this.packetTitle = getClass("org.spigotmc.ProtocolInjector$PacketTitle");
this.packetActions = getClass("org.spigotmc.ProtocolInjector$PacketTitle$Action");
this.nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
}
@Override public void send(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
if ((getProtocolVersion(player) >= 47) && isSpigot() && (this.packetTitle != null)) {
// First reset previous settings
resetTitle(player);
// Send timings first
Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet = this.packetTitle
.getConstructor(this.packetActions, Integer.TYPE, Integer.TYPE, Integer.TYPE)
.newInstance(actions[2], this.fadeInTime * (this.ticks ? 1 : 20),
this.stayTime * (this.ticks ? 1 : 20),
this.fadeOutTime * (this.ticks ? 1 : 20));
// Send if set
if ((this.fadeInTime != -1) && (this.fadeOutTime != -1) && (this.stayTime != -1)) {
sendPacket.invoke(connection, packet);
}
// Send title
Object serialized = getMethod(this.nmsChatSerializer, "a", String.class).invoke(null,
"{text:\"" + ChatColor.translateAlternateColorCodes('&', this.getTitle())
+ "\",color:" + this.titleColor.name().toLowerCase() + "}");
packet = this.packetTitle
.getConstructor(this.packetActions, Reflection.getNMSClass("IChatBaseComponent"))
.newInstance(actions[0], serialized);
sendPacket.invoke(connection, packet);
if (!this.getSubtitle().isEmpty()) {
// Send subtitle if present
serialized = getMethod(this.nmsChatSerializer, "a", String.class).invoke(null,
"{text:\"" + ChatColor.translateAlternateColorCodes('&', this.getSubtitle())
+ "\",color:" + this.subtitleColor.name().toLowerCase() + "}");
packet = this.packetTitle.getConstructor(this.packetActions,
Reflection.getNMSClass("IChatBaseComponent"))
.newInstance(actions[1], serialized);
sendPacket.invoke(connection, packet);
}
}
}
@Override public void clearTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
if ((getProtocolVersion(player) >= 47) && isSpigot()) {
// Send timings first
Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet =
this.packetTitle.getConstructor(this.packetActions).newInstance(actions[3]);
sendPacket.invoke(connection, packet);
}
}
@Override public void resetTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
if ((getProtocolVersion(player) >= 47) && isSpigot()) {
// Send timings first
Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = this.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket");
Object packet =
this.packetTitle.getConstructor(this.packetActions).newInstance(actions[4]);
sendPacket.invoke(connection, packet);
}
}
/**
* Get the protocol version of the player.
*
* @param player Player
* @return Protocol version
* @throws IllegalArgumentException
* @throws ReflectiveOperationException
* @throws SecurityException
*/
private int getProtocolVersion(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {
Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object networkManager = getValue("networkManager", connection);
return (Integer) getMethod("getVersion", networkManager.getClass()).invoke(networkManager);
}
/**
* Check if running spigot.
*
* @return Spigot
*/
private boolean isSpigot() {
return Bukkit.getVersion().contains("Spigot");
}
/**
* Get class by url.
*
* @param namespace Namespace url
* @return Class
*/
private Class<?> getClass(String namespace) {
try {
return Class.forName(namespace);
} catch (ClassNotFoundException ignored) {
}
return null;
}
private Field getField(String name, Class<?> clazz)
throws NoSuchFieldException, SecurityException {
return clazz.getDeclaredField(name);
}
private Object getValue(String name, Object obj)
throws ReflectiveOperationException, SecurityException, IllegalArgumentException {
Field f = getField(name, obj.getClass());
f.setAccessible(true);
return f.get(obj);
}
private Field getField(Class<?> clazz, String name) {
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
return field;
} catch (SecurityException | NoSuchFieldException e) {
e.printStackTrace();
return null;
}
}
private Method getMethod(Class<?> clazz, String name, Class<?>... args) {
for (Method m : clazz.getMethods()) {
if (m.getName().equals(name) && ((args.length == 0) || classListEqual(args,
m.getParameterTypes()))) {
m.setAccessible(true);
return m;
}
}
return null;
}
}

View File

@ -11,246 +11,246 @@ import java.util.Map;
public abstract class TitleManager { public abstract class TitleManager {
private static final Map<Class<?>, Class<?>> CORRESPONDING_TYPES = new HashMap<>(); private static final Map<Class<?>, Class<?>> CORRESPONDING_TYPES = new HashMap<>();
/* Title packet */ Class<?> packetTitle; /* Title packet */ Class<?> packetTitle;
/* Title packet actions ENUM */ Class<?> packetActions; /* Title packet actions ENUM */ Class<?> packetActions;
/* Chat serializer */ Class<?> nmsChatSerializer; /* Chat serializer */ Class<?> nmsChatSerializer;
Class<?> chatBaseComponent; Class<?> chatBaseComponent;
ChatColor titleColor = ChatColor.WHITE; ChatColor titleColor = ChatColor.WHITE;
ChatColor subtitleColor = ChatColor.WHITE; ChatColor subtitleColor = ChatColor.WHITE;
/* Title timings */ int fadeInTime = -1; /* Title timings */ int fadeInTime = -1;
int stayTime = -1; int stayTime = -1;
int fadeOutTime = -1; int fadeOutTime = -1;
boolean ticks = false; boolean ticks = false;
/* Title text and color */ /* Title text and color */
private String title = ""; private String title;
/* Subtitle text and color */ /* Subtitle text and color */
private String subtitle = ""; private String subtitle;
/** /**
* Create a new 1.8 title. * Create a new 1.8 title.
* *
* @param title Title text * @param title Title text
* @param subtitle Subtitle text * @param subtitle Subtitle text
* @param fadeInTime Fade in time * @param fadeInTime Fade in time
* @param stayTime Stay on screen time * @param stayTime Stay on screen time
* @param fadeOutTime Fade out time * @param fadeOutTime Fade out time
*/ */
TitleManager(String title, String subtitle, int fadeInTime, int stayTime, int fadeOutTime) { TitleManager(String title, String subtitle, int fadeInTime, int stayTime, int fadeOutTime) {
this.title = title; this.title = title;
this.subtitle = subtitle; this.subtitle = subtitle;
this.fadeInTime = fadeInTime; this.fadeInTime = fadeInTime;
this.stayTime = stayTime; this.stayTime = stayTime;
this.fadeOutTime = fadeOutTime; this.fadeOutTime = fadeOutTime;
loadClasses(); loadClasses();
}
abstract void loadClasses();
/**
* Gets title text.
*
* @return Title text
*/
public final String getTitle() {
return this.title;
}
/**
* Sets the text for the title.
*
* @param title Title
*/
public final void setTitle(String title) {
this.title = title;
}
/**
* Gets the subtitle text.
*
* @return Subtitle text
*/
public final String getSubtitle() {
return this.subtitle;
}
/**
* Sets subtitle text.
*
* @param subtitle Subtitle text
*/
public final void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
/**
* Sets the title color.
*
* @param color Chat color
*/
public final void setTitleColor(ChatColor color) {
this.titleColor = color;
}
/**
* Sets the subtitle color.
*
* @param color Chat color
*/
public final void setSubtitleColor(ChatColor color) {
this.subtitleColor = color;
}
/**
* Sets title fade in time.
*
* @param time Time
*/
public final void setFadeInTime(int time) {
this.fadeInTime = time;
}
/**
* Sets title fade out time.
*
* @param time Time
*/
public final void setFadeOutTime(int time) {
this.fadeOutTime = time;
}
/**
* Sets title stay time.
*
* @param time Time
*/
public final void setStayTime(int time) {
this.stayTime = time;
}
/**
* Sets timings to ticks.
*/
public final void setTimingsToTicks() {
this.ticks = true;
}
/**
* Sets timings to seconds.
*/
public final void setTimingsToSeconds() {
this.ticks = false;
}
/**
* Sends the title to a player.
*
* @param player Player
* @throws IllegalArgumentException
* @throws ReflectiveOperationException
* @throws SecurityException
*/
public abstract void send(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException;
/**
* Broadcasts the title to all players.
*
* @throws Exception
*/
public final void broadcast() throws Exception {
for (Player player : Bukkit.getOnlinePlayers()) {
send(player);
} }
}
abstract void loadClasses(); /**
* Clears the title.
*
* @param player Player
* @throws IllegalArgumentException
* @throws ReflectiveOperationException
* @throws SecurityException
*/
public abstract void clearTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException;
/** /**
* Gets title text. * Resets the title settings.
* *
* @return Title text * @param player Player
*/ * @throws IllegalArgumentException
public final String getTitle() { * @throws ReflectiveOperationException
return this.title; * @throws SecurityException
*/
public abstract void resetTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException;
private Class<?> getPrimitiveType(Class<?> clazz) {
if (CORRESPONDING_TYPES.containsKey(clazz)) {
return CORRESPONDING_TYPES.get(clazz);
} else {
return clazz;
} }
}
/** private Class<?>[] toPrimitiveTypeArray(Class<?>[] classes) {
* Sets the text for the title. int a;
* if (classes != null) {
* @param title Title a = classes.length;
*/ } else {
public final void setTitle(String title) { a = 0;
this.title = title;
} }
Class<?>[] types = new Class<?>[a];
/** for (int i = 0; i < a; i++) {
* Gets the subtitle text. types[i] = getPrimitiveType(classes[i]);
*
* @return Subtitle text
*/
public final String getSubtitle() {
return this.subtitle;
} }
return types;
}
/** final Object getHandle(Object obj) {
* Sets subtitle text. try {
* return getMethod("getHandle", obj.getClass()).invoke(obj);
* @param subtitle Subtitle text } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
*/ e.printStackTrace();
public final void setSubtitle(String subtitle) { return null;
this.subtitle = subtitle;
} }
}
/** final Method getMethod(String name, Class<?> clazz, Class<?>... paramTypes) {
* Sets the title color. Class<?>[] t = toPrimitiveTypeArray(paramTypes);
* for (Method m : clazz.getMethods()) {
* @param color Chat color Class<?>[] types = toPrimitiveTypeArray(m.getParameterTypes());
*/ if (m.getName().equals(name) && equalsTypeArray(types, t)) {
public final void setTitleColor(ChatColor color) { return m;
this.titleColor = color; }
} }
return null;
}
/** private boolean equalsTypeArray(Class<?>[] a, Class<?>[] o) {
* Sets the subtitle color. if (a.length != o.length) {
* return false;
* @param color Chat color
*/
public final void setSubtitleColor(ChatColor color) {
this.subtitleColor = color;
} }
for (int i = 0; i < a.length; i++) {
/** if (!a[i].equals(o[i]) && !a[i].isAssignableFrom(o[i])) {
* Sets title fade in time. return false;
* }
* @param time Time
*/
public final void setFadeInTime(int time) {
this.fadeInTime = time;
} }
return true;
}
/** boolean classListEqual(Class<?>[] l1, Class<?>[] l2) {
* Sets title fade out time. if (l1.length != l2.length) {
* return false;
* @param time Time
*/
public final void setFadeOutTime(int time) {
this.fadeOutTime = time;
} }
boolean equal = true;
/** for (int i = 0; i < l1.length; i++) {
* Sets title stay time. if (l1[i] != l2[i]) {
* equal = false;
* @param time Time break;
*/ }
public final void setStayTime(int time) {
this.stayTime = time;
}
/**
* Sets timings to ticks.
*/
public final void setTimingsToTicks() {
this.ticks = true;
}
/**
* Sets timings to seconds.
*/
public final void setTimingsToSeconds() {
this.ticks = false;
}
/**
* Sends the title to a player.
*
* @param player Player
* @throws IllegalArgumentException
* @throws ReflectiveOperationException
* @throws SecurityException
*/
public abstract void send(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException;
/**
* Broadcasts the title to all players.
*
* @throws Exception
*/
public final void broadcast() throws Exception {
for (Player player : Bukkit.getOnlinePlayers()) {
send(player);
}
}
/**
* Clears the title.
*
* @param player Player
* @throws IllegalArgumentException
* @throws ReflectiveOperationException
* @throws SecurityException
*/
public abstract void clearTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException;
/**
* Resets the title settings.
*
* @param player Player
* @throws IllegalArgumentException
* @throws ReflectiveOperationException
* @throws SecurityException
*/
public abstract void resetTitle(Player player)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException;
private Class<?> getPrimitiveType(Class<?> clazz) {
if (CORRESPONDING_TYPES.containsKey(clazz)) {
return CORRESPONDING_TYPES.get(clazz);
} else {
return clazz;
}
}
private Class<?>[] toPrimitiveTypeArray(Class<?>[] classes) {
int a;
if (classes != null) {
a = classes.length;
} else {
a = 0;
}
Class<?>[] types = new Class<?>[a];
for (int i = 0; i < a; i++) {
types[i] = getPrimitiveType(classes[i]);
}
return types;
}
final Object getHandle(Object obj) {
try {
return getMethod("getHandle", obj.getClass()).invoke(obj);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
final Method getMethod(String name, Class<?> clazz, Class<?>... paramTypes) {
Class<?>[] t = toPrimitiveTypeArray(paramTypes);
for (Method m : clazz.getMethods()) {
Class<?>[] types = toPrimitiveTypeArray(m.getParameterTypes());
if (m.getName().equals(name) && equalsTypeArray(types, t)) {
return m;
}
}
return null;
}
private boolean equalsTypeArray(Class<?>[] a, Class<?>[] o) {
if (a.length != o.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (!a[i].equals(o[i]) && !a[i].isAssignableFrom(o[i])) {
return false;
}
}
return true;
}
boolean classListEqual(Class<?>[] l1, Class<?>[] l2) {
if (l1.length != l2.length) {
return false;
}
boolean equal = true;
for (int i = 0; i < l1.length; i++) {
if (l1[i] != l2[i]) {
equal = false;
break;
}
}
return equal;
} }
return equal;
}
} }

View File

@ -18,485 +18,488 @@ import java.util.stream.IntStream;
* @version 1.1.0 * @version 1.1.0
*/ */
public class TitleManager_1_11 { public class TitleManager_1_11 {
private static final Map<Class<?>, Class<?>> CORRESPONDING_TYPES = new HashMap<>(); private static final Map<Class<?>, Class<?>> CORRESPONDING_TYPES = new HashMap<>();
/* Title packet */ /* Title packet */
private static Class<?> packetTitle; private static Class<?> packetTitle;
/* Title packet actions ENUM */ /* Title packet actions ENUM */
private static Class<?> packetActions; private static Class<?> packetActions;
/* Chat serializer */ /* Chat serializer */
private static Class<?> nmsChatSerializer; private static Class<?> nmsChatSerializer;
private static Class<?> chatBaseComponent; private static Class<?> chatBaseComponent;
/* NMS player and connection */ /* NMS player and connection */
private static Class<?> nmsPlayer; private static Class<?> nmsPlayer;
private static Class<?> nmsPlayerConnection; private static Class<?> nmsPlayerConnection;
private static Field playerConnection; private static Field playerConnection;
private static Method sendPacket; private static Method sendPacket;
private static Class<?> obcPlayer; private static Class<?> obcPlayer;
private static Method methodPlayerGetHandle; private static Method methodPlayerGetHandle;
/* Title text and color */ /* Title text and color */
private String title = ""; private String title = "";
private ChatColor titleColor = ChatColor.WHITE; private ChatColor titleColor = ChatColor.WHITE;
/* Subtitle text and color */ /* Subtitle text and color */
private String subtitle = ""; private String subtitle = "";
private ChatColor subtitleColor = ChatColor.WHITE; private ChatColor subtitleColor = ChatColor.WHITE;
/* Title timings */ /* Title timings */
private int fadeInTime = -1; private int fadeInTime = -1;
private int stayTime = -1; private int stayTime = -1;
private int fadeOutTime = -1; private int fadeOutTime = -1;
private boolean ticks = false; private boolean ticks = false;
public TitleManager_1_11() { public TitleManager_1_11() {
loadClasses(); loadClasses();
}
/**
* Create a new 1.8 title.
*
* @param title Title
*/
public TitleManager_1_11(String title) {
this.title = title;
loadClasses();
}
/**
* Create a new 1.8 title
*
* @param title Title text
* @param subtitle Subtitle text
*/
public TitleManager_1_11(String title, String subtitle) {
this.title = title;
this.subtitle = subtitle;
loadClasses();
}
/**
* Copy 1.8 title.
*
* @param title Title
*/
public TitleManager_1_11(TitleManager_1_11 title) {
// Copy title
this.title = title.getTitle();
this.subtitle = title.getSubtitle();
this.titleColor = title.getTitleColor();
this.subtitleColor = title.getSubtitleColor();
this.fadeInTime = title.getFadeInTime();
this.fadeOutTime = title.getFadeOutTime();
this.stayTime = title.getStayTime();
this.ticks = title.isTicks();
loadClasses();
}
/**
* Create a new 1.8 title.
*
* @param title Title text
* @param subtitle Subtitle text
* @param fadeInTime Fade in time
* @param stayTime Stay on screen time
* @param fadeOutTime Fade out time
*/
public TitleManager_1_11(String title, String subtitle, int fadeInTime, int stayTime,
int fadeOutTime) {
this.title = title;
this.subtitle = subtitle;
this.fadeInTime = fadeInTime;
this.stayTime = stayTime;
this.fadeOutTime = fadeOutTime;
loadClasses();
}
private static boolean equalsTypeArray(Class<?>[] a, Class<?>[] o) {
if (a.length != o.length) {
return false;
} }
return IntStream.range(0, a.length)
.noneMatch(i -> !a[i].equals(o[i]) && !a[i].isAssignableFrom(o[i]));
}
/** /**
* Create a new 1.8 title * Load spigot and NMS classes.
* */
* @param title Title private void loadClasses() {
*/ if (packetTitle == null) {
public TitleManager_1_11(String title) { packetTitle = getNMSClass("PacketPlayOutTitle");
this.title = title; packetActions = getNMSClass("PacketPlayOutTitle$EnumTitleAction");
loadClasses(); chatBaseComponent = getNMSClass("IChatBaseComponent");
nmsChatSerializer = getNMSClass("ChatComponentText");
nmsPlayer = getNMSClass("EntityPlayer");
nmsPlayerConnection = getNMSClass("PlayerConnection");
playerConnection = getField(nmsPlayer, "playerConnection");
sendPacket = getMethod(nmsPlayerConnection, "sendPacket");
obcPlayer = getOBCClass("entity.CraftPlayer");
methodPlayerGetHandle = getMethod("getHandle", obcPlayer);
} }
}
/** /**
* Create a new 1.8 title * Gets the title text.
* *
* @param title Title text * @return Title text
* @param subtitle Subtitle text */
*/ public String getTitle() {
public TitleManager_1_11(String title, String subtitle) { return this.title;
this.title = title; }
this.subtitle = subtitle;
loadClasses();
}
/** /**
* Copy 1.8 title * Sets the title text.
* *
* @param title Title * @param title Title
*/ */
public TitleManager_1_11(TitleManager_1_11 title) { public void setTitle(String title) {
// Copy title this.title = title;
this.title = title.getTitle(); }
this.subtitle = title.getSubtitle();
this.titleColor = title.getTitleColor();
this.subtitleColor = title.getSubtitleColor();
this.fadeInTime = title.getFadeInTime();
this.fadeOutTime = title.getFadeOutTime();
this.stayTime = title.getStayTime();
this.ticks = title.isTicks();
loadClasses();
}
/** /**
* Create a new 1.8 title * Gets the subtitle text.
* *
* @param title Title text * @return Subtitle text
* @param subtitle Subtitle text */
* @param fadeInTime Fade in time public String getSubtitle() {
* @param stayTime Stay on screen time return this.subtitle;
* @param fadeOutTime Fade out time }
*/
public TitleManager_1_11(String title, String subtitle, int fadeInTime, int stayTime,
int fadeOutTime) {
this.title = title;
this.subtitle = subtitle;
this.fadeInTime = fadeInTime;
this.stayTime = stayTime;
this.fadeOutTime = fadeOutTime;
loadClasses();
}
private static boolean equalsTypeArray(Class<?>[] a, Class<?>[] o) { /**
if (a.length != o.length) { * Sets the subtitle text.
return false; *
* @param subtitle Subtitle text
*/
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
/**
* Sets timings to ticks.
*/
public void setTimingsToTicks() {
ticks = true;
}
/**
* Sets timings to seconds.
*/
public void setTimingsToSeconds() {
ticks = false;
}
/**
* Sends the title to a player.
*
* @param player Player
*/
public void send(Player player) {
if (packetTitle != null) {
// First reset previous settings
resetTitle(player);
try {
// Send timings first
Object handle = getHandle(player);
Object connection = playerConnection.get(handle);
Object[] actions = packetActions.getEnumConstants();
Object packet = packetTitle
.getConstructor(packetActions, chatBaseComponent, Integer.TYPE, Integer.TYPE,
Integer.TYPE).newInstance(actions[3], null, fadeInTime * (ticks ? 1 : 20),
stayTime * (ticks ? 1 : 20), fadeOutTime * (ticks ? 1 : 20));
// Send if set
if (fadeInTime != -1 && fadeOutTime != -1 && stayTime != -1) {
sendPacket.invoke(connection, packet);
} }
return IntStream.range(0, a.length)
.noneMatch(i -> !a[i].equals(o[i]) && !a[i].isAssignableFrom(o[i]));
}
/** Object serialized;
* Load spigot and NMS classes if (!subtitle.equals("")) {
*/ // Send subtitle if present
private void loadClasses() { serialized = nmsChatSerializer.getConstructor(String.class)
if (packetTitle == null) { .newInstance(subtitleColor + ChatColor.translateAlternateColorCodes('&', subtitle));
packetTitle = getNMSClass("PacketPlayOutTitle"); packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
packetActions = getNMSClass("PacketPlayOutTitle$EnumTitleAction"); .newInstance(actions[1], serialized);
chatBaseComponent = getNMSClass("IChatBaseComponent"); sendPacket.invoke(connection, packet);
nmsChatSerializer = getNMSClass("ChatComponentText");
nmsPlayer = getNMSClass("EntityPlayer");
nmsPlayerConnection = getNMSClass("PlayerConnection");
playerConnection = getField(nmsPlayer, "playerConnection");
sendPacket = getMethod(nmsPlayerConnection, "sendPacket");
obcPlayer = getOBCClass("entity.CraftPlayer");
methodPlayerGetHandle = getMethod("getHandle", obcPlayer);
} }
// Send title
serialized = nmsChatSerializer.getConstructor(String.class)
.newInstance(titleColor + ChatColor.translateAlternateColorCodes('&', title));
packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
.newInstance(actions[0], serialized);
sendPacket.invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
} }
}
/** public void updateTimes(Player player) {
* Get title text if (TitleManager_1_11.packetTitle != null) {
* try {
* @return Title text Object handle = getHandle(player);
*/ Object connection = playerConnection.get(handle);
public String getTitle() { Object[] actions = TitleManager_1_11.packetActions.getEnumConstants();
return this.title; Object packet = TitleManager_1_11.packetTitle.getConstructor(
} new Class[] {TitleManager_1_11.packetActions, chatBaseComponent, Integer.TYPE,
Integer.TYPE, Integer.TYPE})
/** .newInstance(actions[3], null, this.fadeInTime * (this.ticks ? 1 : 20),
* Set title text this.stayTime * (this.ticks ? 1 : 20), this.fadeOutTime * (this.ticks ? 1 : 20));
* if ((this.fadeInTime != -1) && (this.fadeOutTime != -1) && (this.stayTime != -1)) {
* @param title Title sendPacket.invoke(connection, packet);
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Get subtitle text
*
* @return Subtitle text
*/
public String getSubtitle() {
return this.subtitle;
}
/**
* Set subtitle text
*
* @param subtitle Subtitle text
*/
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
/**
* Set timings to ticks
*/
public void setTimingsToTicks() {
ticks = true;
}
/**
* Set timings to seconds
*/
public void setTimingsToSeconds() {
ticks = false;
}
/**
* Send the title to a player
*
* @param player Player
*/
public void send(Player player) {
if (packetTitle != null) {
// First reset previous settings
resetTitle(player);
try {
// Send timings first
Object handle = getHandle(player);
Object connection = playerConnection.get(handle);
Object[] actions = packetActions.getEnumConstants();
Object packet = packetTitle
.getConstructor(packetActions, chatBaseComponent, Integer.TYPE, Integer.TYPE,
Integer.TYPE).newInstance(actions[3], null, fadeInTime * (ticks ? 1 : 20),
stayTime * (ticks ? 1 : 20), fadeOutTime * (ticks ? 1 : 20));
// Send if set
if (fadeInTime != -1 && fadeOutTime != -1 && stayTime != -1) {
sendPacket.invoke(connection, packet);
}
Object serialized;
if (!subtitle.equals("")) {
// Send subtitle if present
serialized = nmsChatSerializer.getConstructor(String.class).newInstance(
subtitleColor + ChatColor.translateAlternateColorCodes('&', subtitle));
packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
.newInstance(actions[1], serialized);
sendPacket.invoke(connection, packet);
}
// Send title
serialized = nmsChatSerializer.getConstructor(String.class)
.newInstance(titleColor + ChatColor.translateAlternateColorCodes('&', title));
packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
.newInstance(actions[0], serialized);
sendPacket.invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
} }
} catch (Exception e) {
e.printStackTrace();
}
} }
}
public void updateTimes(Player player) { public void updateTitle(Player player) {
if (TitleManager_1_11.packetTitle != null) { if (TitleManager_1_11.packetTitle != null) {
try { try {
Object handle = getHandle(player); Object handle = getHandle(player);
Object connection = playerConnection.get(handle); Object connection = getField(handle.getClass(), "playerConnection").get(handle);
Object[] actions = TitleManager_1_11.packetActions.getEnumConstants(); Object[] actions = TitleManager_1_11.packetActions.getEnumConstants();
Object packet = TitleManager_1_11.packetTitle.getConstructor( Method sendPacket = getMethod(connection.getClass(), "sendPacket");
new Class[] {TitleManager_1_11.packetActions, chatBaseComponent, Integer.TYPE, Object serialized = nmsChatSerializer.getConstructor(String.class)
Integer.TYPE, Integer.TYPE}) .newInstance(titleColor + ChatColor.translateAlternateColorCodes('&', this.title));
.newInstance(actions[3], null, this.fadeInTime * (this.ticks ? 1 : 20), Object packet = TitleManager_1_11.packetTitle
this.stayTime * (this.ticks ? 1 : 20), .getConstructor(new Class[] {TitleManager_1_11.packetActions, chatBaseComponent})
this.fadeOutTime * (this.ticks ? 1 : 20)); .newInstance(actions[0], serialized);
if ((this.fadeInTime != -1) && (this.fadeOutTime != -1) && (this.stayTime != -1)) { sendPacket.invoke(connection, packet);
sendPacket.invoke(connection, packet); } catch (Exception e) {
} e.printStackTrace();
} catch (Exception e) { }
e.printStackTrace();
}
}
} }
}
public void updateTitle(Player player) { public void updateSubtitle(Player player) {
if (TitleManager_1_11.packetTitle != null) { if (TitleManager_1_11.packetTitle != null) {
try { try {
Object handle = getHandle(player); Object handle = getHandle(player);
Object connection = getField(handle.getClass(), "playerConnection").get(handle); Object connection = playerConnection.get(handle);
Object[] actions = TitleManager_1_11.packetActions.getEnumConstants(); Object[] actions = TitleManager_1_11.packetActions.getEnumConstants();
Method sendPacket = getMethod(connection.getClass(), "sendPacket"); Object serialized = nmsChatSerializer.getConstructor(String.class).newInstance(
Object serialized = nmsChatSerializer.getConstructor(String.class).newInstance( subtitleColor + ChatColor.translateAlternateColorCodes('&', this.subtitle));
titleColor + ChatColor.translateAlternateColorCodes('&', this.title)); Object packet = TitleManager_1_11.packetTitle
Object packet = TitleManager_1_11.packetTitle.getConstructor( .getConstructor(new Class[] {TitleManager_1_11.packetActions, chatBaseComponent})
new Class[] {TitleManager_1_11.packetActions, chatBaseComponent}) .newInstance(actions[1], serialized);
.newInstance(actions[0], serialized); sendPacket.invoke(connection, packet);
sendPacket.invoke(connection, packet); } catch (Exception e) {
} catch (Exception e) { e.printStackTrace();
e.printStackTrace(); }
}
}
} }
}
public void updateSubtitle(Player player) { /**
if (TitleManager_1_11.packetTitle != null) { * Broadcasts the title to all players.
try { */
Object handle = getHandle(player); public void broadcast() {
Object connection = playerConnection.get(handle); for (Player p : Bukkit.getOnlinePlayers()) {
Object[] actions = TitleManager_1_11.packetActions.getEnumConstants(); send(p);
Object serialized = nmsChatSerializer.getConstructor(String.class).newInstance(
subtitleColor + ChatColor.translateAlternateColorCodes('&', this.subtitle));
Object packet = TitleManager_1_11.packetTitle.getConstructor(
new Class[] {TitleManager_1_11.packetActions, chatBaseComponent})
.newInstance(actions[1], serialized);
sendPacket.invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
} }
}
/** /**
* Broadcast the title to all players * Clears the title from the players screen.
*/ *
public void broadcast() { * @param player Player
for (Player p : Bukkit.getOnlinePlayers()) { */
send(p); public void clearTitle(Player player) {
} try {
// Send timings first
Object handle = getHandle(player);
Object connection = playerConnection.get(handle);
Object[] actions = packetActions.getEnumConstants();
Object packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
.newInstance(actions[4], null);
sendPacket.invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
} }
}
/** /**
* Clear the title * Resets the title settings.
* *
* @param player Player * @param player Player
*/ */
public void clearTitle(Player player) { public void resetTitle(Player player) {
try { try {
// Send timings first // Send timings first
Object handle = getHandle(player); Object handle = getHandle(player);
Object connection = playerConnection.get(handle); Object connection = playerConnection.get(handle);
Object[] actions = packetActions.getEnumConstants(); Object[] actions = packetActions.getEnumConstants();
Object packet = packetTitle.getConstructor(packetActions, chatBaseComponent) Object packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
.newInstance(actions[4], null); .newInstance(actions[5], null);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
}
} }
}
/** private Class<?> getPrimitiveType(Class<?> clazz) {
* Reset the title settings return CORRESPONDING_TYPES.containsKey(clazz) ? CORRESPONDING_TYPES.get(clazz) : clazz;
* }
* @param player Player
*/
public void resetTitle(Player player) {
try {
// Send timings first
Object handle = getHandle(player);
Object connection = playerConnection.get(handle);
Object[] actions = packetActions.getEnumConstants();
Object packet = packetTitle.getConstructor(packetActions, chatBaseComponent)
.newInstance(actions[5], null);
sendPacket.invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
private Class<?> getPrimitiveType(Class<?> clazz) { private Class<?>[] toPrimitiveTypeArray(Class<?>[] classes) {
return CORRESPONDING_TYPES.containsKey(clazz) ? CORRESPONDING_TYPES.get(clazz) : clazz; int a = classes != null ? classes.length : 0;
Class<?>[] types = new Class<?>[a];
for (int i = 0; i < a; i++) {
types[i] = getPrimitiveType(classes[i]);
} }
return types;
}
private Class<?>[] toPrimitiveTypeArray(Class<?>[] classes) { private Object getHandle(Player player) {
int a = classes != null ? classes.length : 0; try {
Class<?>[] types = new Class<?>[a]; return methodPlayerGetHandle.invoke(player);
for (int i = 0; i < a; i++) { } catch (Exception e) {
types[i] = getPrimitiveType(classes[i]); e.printStackTrace();
} return null;
return types;
} }
}
private Object getHandle(Player player) { private Method getMethod(String name, Class<?> clazz, Class<?>... paramTypes) {
try { Class<?>[] t = toPrimitiveTypeArray(paramTypes);
return methodPlayerGetHandle.invoke(player); for (Method m : clazz.getMethods()) {
} catch (Exception e) { Class<?>[] types = toPrimitiveTypeArray(m.getParameterTypes());
e.printStackTrace(); if (m.getName().equals(name) && equalsTypeArray(types, t)) {
return null; return m;
} }
} }
return null;
}
private Method getMethod(String name, Class<?> clazz, Class<?>... paramTypes) { private String getVersion() {
Class<?>[] t = toPrimitiveTypeArray(paramTypes); String name = Bukkit.getServer().getClass().getPackage().getName();
for (Method m : clazz.getMethods()) { return name.substring(name.lastIndexOf('.') + 1) + ".";
Class<?>[] types = toPrimitiveTypeArray(m.getParameterTypes()); }
if (m.getName().equals(name) && equalsTypeArray(types, t)) {
return m;
}
}
return null;
}
private String getVersion() { private Class<?> getNMSClass(String className) {
String name = Bukkit.getServer().getClass().getPackage().getName(); String fullName = "net.minecraft.server." + getVersion() + className;
String version = name.substring(name.lastIndexOf('.') + 1) + "."; Class<?> clazz = null;
return version; try {
clazz = Class.forName(fullName);
} catch (Exception e) {
e.printStackTrace();
} }
return clazz;
}
private Class<?> getNMSClass(String className) { private Class<?> getOBCClass(String className) {
String fullName = "net.minecraft.server." + getVersion() + className; String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
Class<?> clazz = null; Class<?> clazz = null;
try { try {
clazz = Class.forName(fullName); clazz = Class.forName(fullName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
}
return clazz;
} }
return clazz;
}
private Class<?> getOBCClass(String className) { private Field getField(Class<?> clazz, String name) {
String fullName = "org.bukkit.craftbukkit." + getVersion() + className; try {
Class<?> clazz = null; Field field = clazz.getDeclaredField(name);
try { field.setAccessible(true);
clazz = Class.forName(fullName); return field;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} return null;
return clazz;
} }
}
private Field getField(Class<?> clazz, String name) { private Method getMethod(Class<?> clazz, String name, Class<?>... args) {
try { for (Method m : clazz.getMethods()) {
Field field = clazz.getDeclaredField(name); if (m.getName().equals(name) && (args.length == 0 || ClassListEqual(args,
field.setAccessible(true); m.getParameterTypes()))) {
return field; m.setAccessible(true);
} catch (Exception e) { return m;
e.printStackTrace(); }
return null;
}
} }
return null;
}
private Method getMethod(Class<?> clazz, String name, Class<?>... args) { private boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) {
for (Method m : clazz.getMethods()) { if (l1.length != l2.length) {
if (m.getName().equals(name) && (args.length == 0 || ClassListEqual(args, return false;
m.getParameterTypes()))) {
m.setAccessible(true);
return m;
}
}
return null;
} }
boolean equal = true;
for (int i = 0; i < l1.length; i++) {
if (l1[i] != l2[i]) {
equal = false;
break;
}
}
return equal;
}
private boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) { public ChatColor getTitleColor() {
boolean equal = true; return titleColor;
if (l1.length != l2.length) { }
return false;
}
for (int i = 0; i < l1.length; i++) {
if (l1[i] != l2[i]) {
equal = false;
break;
}
}
return equal;
}
public ChatColor getTitleColor() { /**
return titleColor; * Sets the title color.
} *
* @param color Chat color
*/
public void setTitleColor(ChatColor color) {
this.titleColor = color;
}
/** public ChatColor getSubtitleColor() {
* Set the title color return subtitleColor;
* }
* @param color Chat color
*/
public void setTitleColor(ChatColor color) {
this.titleColor = color;
}
public ChatColor getSubtitleColor() { /**
return subtitleColor; * Sets the subtitle color.
} *
* @param color Chat color
*/
public void setSubtitleColor(ChatColor color) {
this.subtitleColor = color;
}
/** public int getFadeInTime() {
* Set the subtitle color return fadeInTime;
* }
* @param color Chat color
*/
public void setSubtitleColor(ChatColor color) {
this.subtitleColor = color;
}
public int getFadeInTime() { /**
return fadeInTime; * Sets the fade in time for the title.
} *
* @param time Time
*/
public void setFadeInTime(int time) {
this.fadeInTime = time;
}
/** /**
* Set title fade in time * Gets the fade out time for the title.
* *
* @param time Time * @return the time to fade out
*/ */
public void setFadeInTime(int time) { public int getFadeOutTime() {
this.fadeInTime = time; return fadeOutTime;
} }
public int getFadeOutTime() { /**
return fadeOutTime; * Sets the fade out time for the title.
} *
* @param time fade-out time
*/
public void setFadeOutTime(int time) {
this.fadeOutTime = time;
}
/** public int getStayTime() {
* Set title fade out time return stayTime;
* }
* @param time Time
*/
public void setFadeOutTime(int time) {
this.fadeOutTime = time;
}
public int getStayTime() { /**
return stayTime; * Sets the title stay time.
} *
* @param time Time
*/
public void setStayTime(int time) {
this.stayTime = time;
}
/** public boolean isTicks() {
* Set title stay time return ticks;
* }
* @param time Time
*/
public void setStayTime(int time) {
this.stayTime = time;
}
public boolean isTicks() {
return ticks;
}
} }

View File

@ -275,8 +275,6 @@ public class BukkitChunkManager extends ChunkManager {
continue; continue;
} }
final LocalBlockQueue queue = GlobalBlockQueue.IMP.getNewQueue(world, false); final LocalBlockQueue queue = GlobalBlockQueue.IMP.getNewQueue(world, false);
RegionWrapper currentPlotClear =
new RegionWrapper(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ());
if (xxb >= p1x && xxt <= p2x && zzb >= p1z && zzt <= p2z) { if (xxb >= p1x && xxt <= p2x && zzb >= p1z && zzt <= p2z) {
AugmentedUtils AugmentedUtils
.bypass(ignoreAugment, () -> queue.regenChunkSafe(chunk.x, chunk.z)); .bypass(ignoreAugment, () -> queue.regenChunkSafe(chunk.x, chunk.z));
@ -341,6 +339,8 @@ public class BukkitChunkManager extends ChunkManager {
if (checkX2 && checkZ2) { if (checkX2 && checkZ2) {
map.saveRegion(bukkitWorldObj, xxt2, xxt, zzt2, zzt); // map.saveRegion(bukkitWorldObj, xxt2, xxt, zzt2, zzt); //
} }
RegionWrapper currentPlotClear =
new RegionWrapper(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ());
map.saveEntitiesOut(chunkObj, currentPlotClear); map.saveEntitiesOut(chunkObj, currentPlotClear);
AugmentedUtils.bypass(ignoreAugment, AugmentedUtils.bypass(ignoreAugment,
() -> setChunkInPlotArea(null, new RunnableVal<ScopedLocalBlockQueue>() { () -> setChunkInPlotArea(null, new RunnableVal<ScopedLocalBlockQueue>() {
@ -611,11 +611,7 @@ public class BukkitChunkManager extends ChunkManager {
} }
} }
public void saveEntitiesIn(Chunk chunk, RegionWrapper region) { void saveEntitiesOut(Chunk chunk, RegionWrapper region) {
saveEntitiesIn(chunk, region, 0, 0, false);
}
public void saveEntitiesOut(Chunk chunk, RegionWrapper region) {
for (Entity entity : chunk.getEntities()) { for (Entity entity : chunk.getEntities()) {
Location loc = BukkitUtil.getLocation(entity); Location loc = BukkitUtil.getLocation(entity);
int x = loc.getX(); int x = loc.getX();
@ -632,7 +628,11 @@ public class BukkitChunkManager extends ChunkManager {
} }
} }
public void saveEntitiesIn(Chunk chunk, RegionWrapper region, int offsetX, int offsetZ, void saveEntitiesIn(Chunk chunk, RegionWrapper region) {
saveEntitiesIn(chunk, region, 0, 0, false);
}
void saveEntitiesIn(Chunk chunk, RegionWrapper region, int offsetX, int offsetZ,
boolean delete) { boolean delete) {
for (Entity entity : chunk.getEntities()) { for (Entity entity : chunk.getEntities()) {
Location loc = BukkitUtil.getLocation(entity); Location loc = BukkitUtil.getLocation(entity);
@ -657,7 +657,7 @@ public class BukkitChunkManager extends ChunkManager {
} }
} }
public void restoreEntities(World world, int xOffset, int zOffset) { void restoreEntities(World world, int xOffset, int zOffset) {
for (EntityWrapper entity : this.entities) { for (EntityWrapper entity : this.entities) {
try { try {
entity.spawn(world, xOffset, zOffset); entity.spawn(world, xOffset, zOffset);
@ -670,8 +670,7 @@ public class BukkitChunkManager extends ChunkManager {
} }
//todo optimize maxY //todo optimize maxY
public void saveBlocks(BukkitWorld world, int maxY, int x, int z, int offsetX, void saveBlocks(BukkitWorld world, int maxY, int x, int z, int offsetX, int offsetZ) {
int offsetZ) {
maxY = Math.min(255, maxY); maxY = Math.min(255, maxY);
BaseBlock[] ids; BaseBlock[] ids;
ids = new BaseBlock[maxY + 1]; ids = new BaseBlock[maxY + 1];

View File

@ -1,9 +1,30 @@
package com.github.intellectualsites.plotsquared.bukkit.util; package com.github.intellectualsites.plotsquared.bukkit.util;
import com.github.intellectualsites.plotsquared.bukkit.events.*; import com.github.intellectualsites.plotsquared.bukkit.events.PlayerClaimPlotEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlayerEnterPlotEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlayerLeavePlotEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlayerPlotDeniedEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlayerPlotHelperEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlayerPlotTrustedEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlayerTeleportToPlotEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotAutoMergeEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotChangeOwnerEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotClearEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotComponentSetEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotDeleteEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotFlagAddEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotFlagRemoveEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotMergeEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotRateEvent;
import com.github.intellectualsites.plotsquared.bukkit.events.PlotUnlinkEvent;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer; import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.flag.Flag;
import com.github.intellectualsites.plotsquared.plot.object.*; import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.Rating;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil; import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -16,97 +37,95 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
/** /**
* Utility class for handling Bukkit {@link Event events} * Utility class for handling Bukkit Events.
*/ */
public final class BukkitEventUtil extends EventUtil { public final class BukkitEventUtil extends EventUtil {
@Nullable public Player getPlayer(final PlotPlayer player) { @Nullable public Player getPlayer(final PlotPlayer player) {
if (player instanceof BukkitPlayer) { if (player instanceof BukkitPlayer) {
return ((BukkitPlayer) player).player; return ((BukkitPlayer) player).player;
}
return null;
} }
return null;
}
private boolean callEvent(@Nonnull final Event event) { private boolean callEvent(@Nonnull final Event event) {
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
return !(event instanceof Cancellable) || !((Cancellable) event).isCancelled(); return !(event instanceof Cancellable) || !((Cancellable) event).isCancelled();
} }
@Override public boolean callClaim(PlotPlayer player, Plot plot, boolean auto) { @Override public boolean callClaim(PlotPlayer player, Plot plot, boolean auto) {
return callEvent(new PlayerClaimPlotEvent(getPlayer(player), plot, auto)); return callEvent(new PlayerClaimPlotEvent(getPlayer(player), plot, auto));
} }
@Override public boolean callTeleport(PlotPlayer player, Location from, Plot plot) { @Override public boolean callTeleport(PlotPlayer player, Location from, Plot plot) {
return callEvent(new PlayerTeleportToPlotEvent(getPlayer(player), from, plot)); return callEvent(new PlayerTeleportToPlotEvent(getPlayer(player), from, plot));
} }
@Override public boolean callComponentSet(Plot plot, String component) { @Override public boolean callComponentSet(Plot plot, String component) {
return callEvent(new PlotComponentSetEvent(plot, component)); return callEvent(new PlotComponentSetEvent(plot, component));
} }
@Override public boolean callClear(Plot plot) { @Override public boolean callClear(Plot plot) {
return callEvent(new PlotClearEvent(plot)); return callEvent(new PlotClearEvent(plot));
} }
@Override public boolean callDelete(Plot plot) { @Override public boolean callDelete(Plot plot) {
return callEvent(new PlotDeleteEvent(plot)); return callEvent(new PlotDeleteEvent(plot));
} }
@Override public boolean callFlagAdd(Flag flag, Plot plot) { @Override public boolean callFlagAdd(Flag flag, Plot plot) {
return callEvent(new PlotFlagAddEvent(flag, plot)); return callEvent(new PlotFlagAddEvent(flag, plot));
} }
@Override public boolean callFlagRemove(Flag<?> flag, Plot plot, Object value) { @Override public boolean callFlagRemove(Flag<?> flag, Plot plot, Object value) {
return callEvent(new PlotFlagRemoveEvent(flag, plot)); return callEvent(new PlotFlagRemoveEvent(flag, plot));
} }
@Override public boolean callMerge(Plot plot, int dir, int max) { @Override public boolean callMerge(Plot plot, int dir, int max) {
return callEvent( return callEvent(new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, dir, max));
new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, dir, max)); }
}
@Override public boolean callAutoMerge(Plot plot, List<PlotId> plots) { @Override public boolean callAutoMerge(Plot plot, List<PlotId> plots) {
return callEvent( return callEvent(new PlotAutoMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots));
new PlotAutoMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots)); }
}
@Override public boolean callUnlink(PlotArea area, List<PlotId> plots) { @Override public boolean callUnlink(PlotArea area, List<PlotId> plots) {
return callEvent(new PlotUnlinkEvent(BukkitUtil.getWorld(area.worldname), area, plots)); return callEvent(new PlotUnlinkEvent(BukkitUtil.getWorld(area.worldname), area, plots));
} }
@Override public void callEntry(PlotPlayer player, Plot plot) { @Override public void callEntry(PlotPlayer player, Plot plot) {
callEvent(new PlayerEnterPlotEvent(getPlayer(player), plot)); callEvent(new PlayerEnterPlotEvent(getPlayer(player), plot));
} }
@Override public void callLeave(PlotPlayer player, Plot plot) { @Override public void callLeave(PlotPlayer player, Plot plot) {
callEvent(new PlayerLeavePlotEvent(getPlayer(player), plot)); callEvent(new PlayerLeavePlotEvent(getPlayer(player), plot));
} }
@Override public void callDenied(PlotPlayer initiator, Plot plot, UUID player, boolean added) { @Override public void callDenied(PlotPlayer initiator, Plot plot, UUID player, boolean added) {
callEvent(new PlayerPlotDeniedEvent(getPlayer(initiator), plot, player, added)); callEvent(new PlayerPlotDeniedEvent(getPlayer(initiator), plot, player, added));
} }
@Override public void callTrusted(PlotPlayer initiator, Plot plot, UUID player, boolean added) { @Override public void callTrusted(PlotPlayer initiator, Plot plot, UUID player, boolean added) {
callEvent(new PlayerPlotTrustedEvent(getPlayer(initiator), plot, player, added)); callEvent(new PlayerPlotTrustedEvent(getPlayer(initiator), plot, player, added));
} }
@Override public void callMember(PlotPlayer initiator, Plot plot, UUID player, boolean added) { @Override public void callMember(PlotPlayer initiator, Plot plot, UUID player, boolean added) {
callEvent(new PlayerPlotHelperEvent(getPlayer(initiator), plot, player, added)); callEvent(new PlayerPlotHelperEvent(getPlayer(initiator), plot, player, added));
} }
@Override @Override
public boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID oldOwner, UUID newOwner, public boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID oldOwner, UUID newOwner,
boolean hasOldOwner) { boolean hasOldOwner) {
return callEvent( return callEvent(
new PlotChangeOwnerEvent(getPlayer(initiator), plot, oldOwner, newOwner, hasOldOwner)); new PlotChangeOwnerEvent(getPlayer(initiator), plot, oldOwner, newOwner, hasOldOwner));
} }
@Override @Nullable public Rating callRating(PlotPlayer player, Plot plot, Rating rating) { @Override @Nullable public Rating callRating(PlotPlayer player, Plot plot, Rating rating) {
PlotRateEvent event = new PlotRateEvent(player, rating, plot); PlotRateEvent event = new PlotRateEvent(player, rating, plot);
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
return null; return null;
}
return event.getRating();
} }
return event.getRating();
}
} }

View File

@ -21,100 +21,104 @@ import java.util.stream.IntStream;
public class BukkitInventoryUtil extends InventoryUtil { public class BukkitInventoryUtil extends InventoryUtil {
public static ItemStack getItem(PlotItemStack item) { @Override public void open(PlotInventory inv) {
if (item == null) { BukkitPlayer bp = (BukkitPlayer) inv.player;
return null; Inventory inventory = Bukkit.createInventory(null, inv.size * 9, inv.getTitle());
} PlotItemStack[] items = inv.getItems();
ItemStack stack = new ItemStack(BukkitUtil.getMaterial(item.getPlotBlock()), item.amount); for (int i = 0; i < inv.size * 9; i++) {
ItemMeta meta = null; PlotItemStack item = items[i];
if (item.name != null) { if (item != null) {
meta = stack.getItemMeta(); inventory.setItem(i, getItem(item));
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', item.name)); }
}
if (item.lore != null) {
if (meta == null) {
meta = stack.getItemMeta();
}
List<String> lore = new ArrayList<>();
for (String entry : item.lore) {
lore.add(ChatColor.translateAlternateColorCodes('&', entry));
}
meta.setLore(lore);
}
if (meta != null) {
stack.setItemMeta(meta);
}
return stack;
} }
bp.player.openInventory(inventory);
}
@Override public void open(PlotInventory inv) { @Override public void close(PlotInventory inv) {
BukkitPlayer bp = (BukkitPlayer) inv.player; if (!inv.isOpen()) {
Inventory inventory = Bukkit.createInventory(null, inv.size * 9, inv.getTitle()); return;
PlotItemStack[] items = inv.getItems();
for (int i = 0; i < inv.size * 9; i++) {
PlotItemStack item = items[i];
if (item != null) {
inventory.setItem(i, getItem(item));
}
}
bp.player.openInventory(inventory);
} }
BukkitPlayer bp = (BukkitPlayer) inv.player;
bp.player.closeInventory();
}
@Override public void close(PlotInventory inv) { @Override public void setItem(PlotInventory inv, int index, PlotItemStack item) {
if (!inv.isOpen()) { BukkitPlayer bp = (BukkitPlayer) inv.player;
return; InventoryView opened = bp.player.getOpenInventory();
} if (!inv.isOpen()) {
BukkitPlayer bp = (BukkitPlayer) inv.player; return;
bp.player.closeInventory();
} }
opened.setItem(index, getItem(item));
bp.player.updateInventory();
}
@Override public void setItem(PlotInventory inv, int index, PlotItemStack item) { private static ItemStack getItem(PlotItemStack item) {
BukkitPlayer bp = (BukkitPlayer) inv.player; if (item == null) {
InventoryView opened = bp.player.getOpenInventory(); return null;
if (!inv.isOpen()) {
return;
}
opened.setItem(index, getItem(item));
bp.player.updateInventory();
} }
ItemStack stack = new ItemStack(BukkitUtil.getMaterial(item.getPlotBlock()), item.amount);
ItemMeta meta = null;
if (item.name != null) {
meta = stack.getItemMeta();
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', item.name));
}
if (item.lore != null) {
if (meta == null) {
meta = stack.getItemMeta();
}
List<String> lore = new ArrayList<>();
for (String entry : item.lore) {
lore.add(ChatColor.translateAlternateColorCodes('&', entry));
}
meta.setLore(lore);
}
if (meta != null) {
stack.setItemMeta(meta);
}
return stack;
}
public PlotItemStack getItem(ItemStack item) { public PlotItemStack getItem(ItemStack item) {
if (item == null) { if (item == null) {
return null; return null;
}
// int id = item.getTypeId();
Material id = item.getType();
short data = item.getDurability();
int amount = item.getAmount();
String name = null;
String[] lore = null;
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
if (meta.hasDisplayName()) {
name = meta.getDisplayName();
}
if (meta.hasLore()) {
List<String> itemLore = meta.getLore();
lore = itemLore.toArray(new String[0]);
}
}
return new PlotItemStack(id.name(), amount, name, lore);
} }
// int id = item.getTypeId();
Material id = item.getType();
ItemMeta meta = item.getItemMeta();
int amount = item.getAmount();
String name = null;
String[] lore = null;
if (item.hasItemMeta()) {
assert meta != null;
if (meta.hasDisplayName()) {
name = meta.getDisplayName();
}
if (meta.hasLore()) {
List<String> itemLore = meta.getLore();
lore = itemLore.toArray(new String[0]);
}
}
return new PlotItemStack(id.name(), amount, name, lore);
}
@Override public PlotItemStack[] getItems(PlotPlayer player) { @Override public PlotItemStack[] getItems(PlotPlayer player) {
BukkitPlayer bp = (BukkitPlayer) player; BukkitPlayer bp = (BukkitPlayer) player;
PlayerInventory inv = bp.player.getInventory(); PlayerInventory inv = bp.player.getInventory();
return IntStream.range(0, 36).mapToObj(i -> getItem(inv.getItem(i))) return IntStream.range(0, 36).mapToObj(i -> getItem(inv.getItem(i)))
.toArray(PlotItemStack[]::new); .toArray(PlotItemStack[]::new);
} }
@Override public boolean isOpen(PlotInventory plotInventory) { @Override public boolean isOpen(PlotInventory plotInventory) {
if (!plotInventory.isOpen()) { if (!plotInventory.isOpen()) {
return false; return false;
}
BukkitPlayer bp = (BukkitPlayer) plotInventory.player;
InventoryView opened = bp.player.getOpenInventory();
return plotInventory.isOpen() && opened.getType() == InventoryType.CRAFTING
&& opened.getTitle() == null;
} }
BukkitPlayer bp = (BukkitPlayer) plotInventory.player;
InventoryView opened = bp.player.getOpenInventory();
if (plotInventory.isOpen()) {
if (opened.getType() == InventoryType.CRAFTING) {
opened.getTitle();
}
}
return false;
}
} }