Compatibility 1.7, fallbacks etc.

This commit is contained in:
Olof Larsson 2016-05-18 09:27:26 +02:00
parent 5daebc8e33
commit b78417b48b
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
14 changed files with 192 additions and 106 deletions

View File

@ -22,7 +22,6 @@ public abstract class Engine implements Active, Listener, Runnable
private static final transient Set<Engine> allInstances = new MassiveSet<>(); private static final transient Set<Engine> allInstances = new MassiveSet<>();
public static Set<Engine> getAllInstances() { return allInstances; } public static Set<Engine> getAllInstances() { return allInstances; }
// -------------------------------------------- // // -------------------------------------------- //
// PLUGIN // PLUGIN
// -------------------------------------------- // // -------------------------------------------- //
@ -116,9 +115,14 @@ public abstract class Engine implements Active, Listener, Runnable
public void setActiveListener(boolean active) public void setActiveListener(boolean active)
{ {
if (active) if (active)
{
// Support without at load
MassivePlugin plugin = this.getPlugin();
if (plugin.isEnabled())
{ {
Bukkit.getPluginManager().registerEvents(this, this.getPlugin()); Bukkit.getPluginManager().registerEvents(this, this.getPlugin());
} }
}
else else
{ {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
@ -134,6 +138,10 @@ public abstract class Engine implements Active, Listener, Runnable
if (active) if (active)
{ {
if (this.getPeriod() != null) if (this.getPeriod() != null)
{
// Support without at load
MassivePlugin plugin = this.getPlugin();
if (plugin.isEnabled())
{ {
if (this.isSync()) if (this.isSync())
{ {
@ -145,6 +153,7 @@ public abstract class Engine implements Active, Listener, Runnable
} }
} }
} }
}
else else
{ {
if (this.task != null) if (this.task != null)

View File

@ -217,17 +217,20 @@ public class MassiveCore extends MassivePlugin
} }
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // LOAD
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public void onLoad() public void onLoadInner()
{ {
super.onLoad();
// Attempting to fix a race condition within the class asynchronous class loader. // Attempting to fix a race condition within the class asynchronous class loader.
System.out.println("TimeUnit.MILLIS_PER_MINUTE: " + TimeUnit.MILLIS_PER_MINUTE); System.out.println("TimeUnit.MILLIS_PER_MINUTE: " + TimeUnit.MILLIS_PER_MINUTE);
} }
// -------------------------------------------- //
// ENABLE
// -------------------------------------------- //
@Override @Override
public void onEnableInner() public void onEnableInner()
{ {
@ -328,6 +331,10 @@ public class MassiveCore extends MassivePlugin
Bukkit.getScheduler().scheduleSyncDelayedTask(this, MassiveCoreTaskDeleteFiles.get()); Bukkit.getScheduler().scheduleSyncDelayedTask(this, MassiveCoreTaskDeleteFiles.get());
} }
// -------------------------------------------- //
// DISABLE
// -------------------------------------------- //
@Override @Override
public void onDisable() public void onDisable()
{ {

View File

@ -36,19 +36,52 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
public boolean isVersionSynchronized() { return this.versionSynchronized; } public boolean isVersionSynchronized() { return this.versionSynchronized; }
public void setVersionSynchronized(boolean versionSynchronized) { this.versionSynchronized = versionSynchronized; } public void setVersionSynchronized(boolean versionSynchronized) { this.versionSynchronized = versionSynchronized; }
// -------------------------------------------- //
// LOAD
// -------------------------------------------- //
@Override
public void onLoad()
{
this.onLoadPre();
this.onLoadInner();
this.onLoadPost();
}
public void onLoadPre()
{
this.logPrefixColored = Txt.parse("<teal>[<aqua>%s %s<teal>] <i>", this.getDescription().getName(), this.getDescription().getVersion());
this.logPrefixPlain = ChatColor.stripColor(this.logPrefixColored);
}
public void onLoadInner()
{
}
public void onLoadPost()
{
}
// -------------------------------------------- // // -------------------------------------------- //
// ENABLE // ENABLE
// -------------------------------------------- // // -------------------------------------------- //
@Override
public void onEnable()
{
if ( ! this.onEnablePre()) return;
this.onEnableInner();
this.onEnablePost();
}
private long enableTime; private long enableTime;
public long getEnableTime() { return this.enableTime; } public long getEnableTime() { return this.enableTime; }
public boolean preEnable() public boolean onEnablePre()
{ {
enableTime = System.currentTimeMillis(); this.enableTime = System.currentTimeMillis();
this.logPrefixColored = Txt.parse("<teal>[<aqua>%s %s<teal>] <i>", this.getDescription().getName(), this.getDescription().getVersion());
this.logPrefixPlain = ChatColor.stripColor(this.logPrefixColored);
log("=== ENABLE START ==="); log("=== ENABLE START ===");
@ -65,27 +98,6 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
return true; return true;
} }
public void postEnable()
{
// Metrics
if (MassiveCoreMConf.get().mcstatsEnabled)
{
try
{
MetricsLite metrics = new MetricsLite(this);
metrics.start();
}
catch (IOException e)
{
String message = Txt.parse("<b>Metrics Initialization Failed :'(");
log(message);
}
}
long ms = System.currentTimeMillis() - enableTime;
log(Txt.parse("=== ENABLE <g>COMPLETE <i>(Took <h>" + ms + "ms<i>) ==="));
}
public void checkVersionSynchronization() public void checkVersionSynchronization()
{ {
// If this plugin is version synchronized ... // If this plugin is version synchronized ...
@ -121,21 +133,32 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
} }
} }
@Override
public void onEnable()
{
if ( ! this.preEnable()) return;
this.onEnableInner();
this.postEnable();
}
public void onEnableInner() public void onEnableInner()
{ {
} }
public void onEnablePost()
{
// Metrics
if (MassiveCoreMConf.get().mcstatsEnabled)
{
try
{
MetricsLite metrics = new MetricsLite(this);
metrics.start();
}
catch (IOException e)
{
String message = Txt.parse("<b>Metrics Initialization Failed :'(");
log(message);
}
}
long ms = System.currentTimeMillis() - this.enableTime;
log(Txt.parse("=== ENABLE <g>COMPLETE <i>(Took <h>" + ms + "ms<i>) ==="));
}
// -------------------------------------------- // // -------------------------------------------- //
// DISABLE // DISABLE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -87,6 +87,8 @@ public class RegistryType
} }
public static Type<?> getType(Field field) public static Type<?> getType(Field field)
{
try
{ {
EditorType annotation = field.getAnnotation(EditorType.class); EditorType annotation = field.getAnnotation(EditorType.class);
if (annotation != null) if (annotation != null)
@ -113,6 +115,15 @@ public class RegistryType
{ {
return TypeMap.get(getType(annMap.typeKey(), annMap.fieldNameKey()), getType(annMap.typeValue(), annMap.fieldNameValue())); return TypeMap.get(getType(annMap.typeKey(), annMap.fieldNameKey()), getType(annMap.typeValue(), annMap.fieldNameValue()));
} }
}
catch (Throwable t)
{
// This has to do with backwards compatibility (Usually 1.7).
// The EditorType annotations may trigger creation of type class instances.
// Those type classes may refer to Bukkit classes not present.
// This issue was first encountered for TypeDataItemStack.
}
return getType(field.getGenericType()); return getType(field.getGenericType());
} }
private static Type<?> getType(Class<?> clazz, String fieldName) private static Type<?> getType(Class<?> clazz, String fieldName)
@ -225,30 +236,10 @@ public class RegistryType
register(TypeDestination.get()); register(TypeDestination.get());
register(TypeItemStack.get()); register(TypeItemStack.get());
// 1.7 Compat
try
{
register(TypeDataBannerPattern.get()); register(TypeDataBannerPattern.get());
}
catch (Throwable t)
{
}
register(TypeDataPotionEffect.get()); register(TypeDataPotionEffect.get());
register(TypeDataFireworkEffect.get()); register(TypeDataFireworkEffect.get());
// 1.? Compat
// TODO: The annotations breaks with 1.7.
// TODO: Find a solution to dodge the converter annotation class inits.
try
{
register(TypeDataItemStack.get()); register(TypeDataItemStack.get());
}
catch (Throwable t)
{
}
register(TypePermission.get()); register(TypePermission.get());
register(TypePotionEffectType.get()); register(TypePotionEffectType.get());

View File

@ -47,25 +47,31 @@ public class DataBannerPattern implements Comparable<DataBannerPattern>
} }
public DataBannerPattern(Pattern pattern) // Minecraft 1.7 Compatibility
public DataBannerPattern(Object pattern)
{ {
this.write(pattern, false); if ( ! (pattern instanceof Pattern)) throw new IllegalArgumentException("pattern");
this.write((Pattern)pattern, false);
} }
// -------------------------------------------- // // -------------------------------------------- //
// WRITE // WRITE
// -------------------------------------------- // // -------------------------------------------- //
public void write(Pattern pattern, boolean a2b) // Minecraft 1.7 Compatibility
public void write(Object pattern, boolean a2b)
{ {
WriterBannerPattern.get().write(this, pattern, a2b); if ( ! (pattern instanceof Pattern)) throw new IllegalArgumentException("pattern");
WriterBannerPattern.get().write(this, (Pattern)pattern, a2b);
} }
// -------------------------------------------- // // -------------------------------------------- //
// TO BUKKIT // TO BUKKIT
// -------------------------------------------- // // -------------------------------------------- //
public Pattern toBukkit() // Minecraft 1.7 Compatibility
@SuppressWarnings("unchecked")
public <T> T toBukkit()
{ {
// Create // Create
Pattern ret = WriterBannerPattern.get().createOB(); Pattern ret = WriterBannerPattern.get().createOB();
@ -74,7 +80,7 @@ public class DataBannerPattern implements Comparable<DataBannerPattern>
this.write(ret, true); this.write(ret, true);
// Return // Return
return ret; return (T) ret;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -65,7 +65,7 @@ public class Mixin extends Engine
// NOTE: This field must be set in the default constructor! // NOTE: This field must be set in the default constructor!
private Boolean availableDefault = null; private Boolean availableDefault = null;
public boolean isAvailableDefault() { return (this.availableDefault != null ? this.availableDefault : this.getDefault().getAlternatives().isEmpty()); } public boolean isAvailableDefault() { return (this.availableDefault != null ? this.availableDefault : this.getDefault().getAlternatives().isEmpty()); }
public void setAvailableDefault(boolean availableDefault) { this.setAvailableDefault(availableDefault); } public void setAvailableDefault(boolean availableDefault) { this.availableDefault = availableDefault; }
public boolean isAvailable() { return this.isAvailableDefault() || this.getInstance() != this.getDefault(); } public boolean isAvailable() { return this.isAvailableDefault() || this.getInstance() != this.getDefault(); }
public void require() { if (this.isAvailable()) return; throw new IllegalStateException(this.getBaseName() + " is Required"); } public void require() { if (this.isAvailable()) return; throw new IllegalStateException(this.getBaseName() + " is Required"); }

View File

@ -127,10 +127,8 @@ public final class MsonEvent implements Serializable
public static MsonEvent item(ItemStack item) public static MsonEvent item(ItemStack item)
{ {
if (item == null) throw new NullPointerException("item"); if (item == null) throw new NullPointerException("item");
String nbtStringTooltip = null; String value = NmsItemStackTooltip.get().getNbtStringTooltip(item);
NmsItemStackTooltip nms = NmsItemStackTooltip.get(); return MsonEvent.valueOf(MsonEventAction.SHOW_ITEM, value);
if (nms.isAvailable()) nbtStringTooltip = nms.getNbtStringTooltip(item);
return MsonEvent.valueOf(MsonEventAction.SHOW_ITEM, nbtStringTooltip);
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -84,4 +84,13 @@ public class NmsBasics extends Mixin
throw this.notImplemented(); throw this.notImplemented();
} }
// -------------------------------------------- //
// PING
// -------------------------------------------- //
public int getPing(Player player)
{
throw this.notImplemented();
}
} }

View File

@ -72,6 +72,10 @@ public class NmsBasics17R4P extends NmsBasics
// net.minecraft.server.PlayerConnection#sendPacket(Packet) // net.minecraft.server.PlayerConnection#sendPacket(Packet)
private Method methodPlayerConnectionsendPacket; private Method methodPlayerConnectionsendPacket;
// PING
// net.minecraft.server.EntityPlayer#ping
private Field fieldNmsPlayerPing;
// -------------------------------------------- // // -------------------------------------------- //
// SETUP // SETUP
// -------------------------------------------- // // -------------------------------------------- //
@ -107,6 +111,9 @@ public class NmsBasics17R4P extends NmsBasics
this.classNmsPacket = PackageType.MINECRAFT_SERVER.getClass("Packet"); this.classNmsPacket = PackageType.MINECRAFT_SERVER.getClass("Packet");
this.classNmsPlayerConnection = PackageType.MINECRAFT_SERVER.getClass("PlayerConnection"); this.classNmsPlayerConnection = PackageType.MINECRAFT_SERVER.getClass("PlayerConnection");
this.methodPlayerConnectionsendPacket = ReflectionUtil.getMethod(this.classNmsPlayerConnection, "sendPacket", this.classNmsPacket); this.methodPlayerConnectionsendPacket = ReflectionUtil.getMethod(this.classNmsPlayerConnection, "sendPacket", this.classNmsPacket);
// PING
this.fieldNmsPlayerPing = ReflectionUtil.getField(this.classNmsPlayer, "ping");
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -170,4 +177,14 @@ public class NmsBasics17R4P extends NmsBasics
ReflectionUtil.invokeMethod(this.methodPlayerConnectionsendPacket, connection, packet); ReflectionUtil.invokeMethod(this.methodPlayerConnectionsendPacket, connection, packet);
} }
// -------------------------------------------- //
// PING
// -------------------------------------------- //
public int getPing(Player player)
{
Object handle = this.getHandle(player);
return ReflectionUtil.getField(this.fieldNmsPlayerPing, handle);
}
} }

View File

@ -16,7 +16,8 @@ public class NmsChat extends Mixin
private static NmsChat d = new NmsChat().setAlternatives( private static NmsChat d = new NmsChat().setAlternatives(
NmsChat18R2P.class, NmsChat18R2P.class,
NmsChat18R1.class NmsChat18R1.class,
NmsChatFallback.class
); );
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -0,0 +1,12 @@
package com.massivecraft.massivecore.nms;
public class NmsChatFallback extends NmsChat
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static NmsChatFallback i = new NmsChatFallback();
public static NmsChatFallback get() { return i; }
}

View File

@ -11,7 +11,8 @@ public class NmsItemStackTooltip extends Mixin
// -------------------------------------------- // // -------------------------------------------- //
private static NmsItemStackTooltip d = new NmsItemStackTooltip().setAlternatives( private static NmsItemStackTooltip d = new NmsItemStackTooltip().setAlternatives(
NmsItemStackTooltip18R1P.class NmsItemStackTooltip18R1P.class,
NmsItemStackTooltipFallback.class
); );
// -------------------------------------------- // // -------------------------------------------- //
@ -27,7 +28,7 @@ public class NmsItemStackTooltip extends Mixin
public String getNbtStringTooltip(ItemStack item) public String getNbtStringTooltip(ItemStack item)
{ {
throw notImplemented(); return null;
} }
} }

View File

@ -0,0 +1,12 @@
package com.massivecraft.massivecore.nms;
public class NmsItemStackTooltipFallback extends NmsItemStackTooltip
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static NmsItemStackTooltipFallback i = new NmsItemStackTooltipFallback();
public static NmsItemStackTooltipFallback get () { return i; }
}