Clean up providers a bit (#3279)

This commit is contained in:
pop4959 2020-05-28 12:06:03 -07:00 committed by GitHub
parent 9e01a1a427
commit 9f3c2683ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 32 additions and 32 deletions

View File

@ -24,7 +24,7 @@ public class FlatSpawnEggProvider extends SpawnEggProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "1.13+ flat spawn egg provider"; return "1.13+ flat spawn egg provider";
} }
} }

View File

@ -14,7 +14,7 @@ public class LegacyPotionMetaProvider extends PotionMetaProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "legacy potion meta provider"; return "legacy potion meta provider";
} }
} }

View File

@ -24,7 +24,7 @@ public class LegacySpawnEggProvider extends SpawnEggProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "legacy item data provider"; return "legacy item data provider";
} }
} }

View File

@ -18,7 +18,7 @@ public class LegacySpawnerProvider extends SpawnerProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "legacy item data provider"; return "legacy item data provider";
} }
} }

View File

@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack;
public abstract class SpawnEggProvider implements Provider { public abstract class SpawnEggProvider implements Provider {
public abstract ItemStack createEggItem(EntityType type) throws IllegalArgumentException; public abstract ItemStack createEggItem(EntityType type) throws IllegalArgumentException;
public abstract EntityType getSpawnedType(ItemStack eggItem) throws IllegalArgumentException; public abstract EntityType getSpawnedType(ItemStack eggItem) throws IllegalArgumentException;
@Override @Override

View File

@ -25,6 +25,7 @@ public abstract class SpawnerProvider implements Provider {
.build(); .build();
public abstract ItemStack setEntityType(ItemStack is, EntityType type) throws IllegalArgumentException; public abstract ItemStack setEntityType(ItemStack is, EntityType type) throws IllegalArgumentException;
public abstract EntityType getEntityType(ItemStack is) throws IllegalArgumentException; public abstract EntityType getEntityType(ItemStack is) throws IllegalArgumentException;
@Override @Override

View File

@ -3,5 +3,5 @@ package net.ess3.providers;
public interface Provider { public interface Provider {
boolean tryProvider(); boolean tryProvider();
String getHumanName(); String getDescription();
} }

View File

@ -5,25 +5,24 @@ import java.util.logging.Logger;
public class ProviderFactory<T extends Provider> { public class ProviderFactory<T extends Provider> {
private Logger logger; private Logger logger;
private String providerType; private String providerType;
private Iterable<Class <? extends T>> availableProviders; private Iterable<Class<? extends T>> providers;
public ProviderFactory(Logger logger, Iterable<Class <? extends T>> availableProviders, String providerType) { public ProviderFactory(Logger logger, Iterable<Class<? extends T>> providers, String providerType) {
this.logger = logger; this.logger = logger;
this.providerType = providerType; this.providerType = providerType;
this.availableProviders = availableProviders; this.providers = providers;
} }
public T getProvider() { public T getProvider() {
T finalProvider = null; for (Class<? extends T> providerClass : providers) {
for (Class<? extends T> providerClass : availableProviders) { T provider = loadProvider(providerClass);
finalProvider = loadProvider(providerClass); if (provider != null && provider.tryProvider()) {
if (finalProvider != null && finalProvider.tryProvider()) { logger.info("Using " + provider.getDescription() + " as " + providerType + " provider.");
break; return provider;
} }
} }
assert finalProvider != null; logger.severe("The " + providerType + " provider failed to load! Some parts of Essentials may not function correctly.");
logger.info("Using " + finalProvider.getHumanName() + " as " + providerType + " provider."); return null;
return finalProvider;
} }
private T loadProvider(Class<? extends T> providerClass) { private T loadProvider(Class<? extends T> providerClass) {

View File

@ -30,7 +30,7 @@ public class ReflSpawnEggProvider extends SpawnEggProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "Reflection based provider"; return "Reflection based provider";
} }
} }

View File

@ -36,7 +36,7 @@ public class ReflUtil {
} }
return nmsVersion; return nmsVersion;
} }
public static NMSVersion getNmsVersionObject() { public static NMSVersion getNmsVersionObject() {
if (nmsVersionObject == null) { if (nmsVersionObject == null) {
nmsVersionObject = NMSVersion.fromString(getNMSVersion()); nmsVersionObject = NMSVersion.fromString(getNMSVersion());
@ -224,7 +224,7 @@ public class ReflUtil {
/** /**
* https://gist.github.com/SupaHam/dad1db6406596c5f8e4b221ff473831c * https://gist.github.com/SupaHam/dad1db6406596c5f8e4b221ff473831c
* *
* @author SupaHam (<a href="https://github.com/SupaHam">https://github.com/SupaHam</a>) * @author SupaHam (<a href="https://github.com/SupaHam">https://github.com/SupaHam</a>)
*/ */
public static class NMSVersion implements Comparable<NMSVersion> { public static class NMSVersion implements Comparable<NMSVersion> {
@ -232,7 +232,7 @@ public class ReflUtil {
private final int major; private final int major;
private final int minor; private final int minor;
private final int release; private final int release;
public static NMSVersion fromString(String string) { public static NMSVersion fromString(String string) {
Preconditions.checkNotNull(string, "string cannot be null."); Preconditions.checkNotNull(string, "string cannot be null.");
Matcher matcher = VERSION_PATTERN.matcher(string); Matcher matcher = VERSION_PATTERN.matcher(string);
@ -251,19 +251,19 @@ public class ReflUtil {
this.minor = minor; this.minor = minor;
this.release = release; this.release = release;
} }
public boolean isHigherThan(NMSVersion o) { public boolean isHigherThan(NMSVersion o) {
return compareTo(o) > 0; return compareTo(o) > 0;
} }
public boolean isHigherThanOrEqualTo(NMSVersion o) { public boolean isHigherThanOrEqualTo(NMSVersion o) {
return compareTo(o) >= 0; return compareTo(o) >= 0;
} }
public boolean isLowerThan(NMSVersion o) { public boolean isLowerThan(NMSVersion o) {
return compareTo(o) < 0; return compareTo(o) < 0;
} }
public boolean isLowerThanOrEqualTo(NMSVersion o) { public boolean isLowerThanOrEqualTo(NMSVersion o) {
return compareTo(o) <= 0; return compareTo(o) <= 0;
} }
@ -290,8 +290,8 @@ public class ReflUtil {
} }
NMSVersion that = (NMSVersion) o; NMSVersion that = (NMSVersion) o;
return major == that.major && return major == that.major &&
minor == that.minor && minor == that.minor &&
release == that.release; release == that.release;
} }
@Override @Override
@ -301,7 +301,7 @@ public class ReflUtil {
@Override @Override
public String toString() { public String toString() {
return "v" + major + "_" + minor + "_R" + release; return "v" + major + "_" + minor + "_R" + release;
} }
@Override @Override

View File

@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>. * along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************** ******************************************************************************
*/ */
package net.ess3.nms.refl; package net.ess3.nms.refl;
import org.bukkit.Material; import org.bukkit.Material;
@ -95,7 +95,7 @@ public class SpawnEggRefl {
} }
Object id = NBTTagCompoundConstructor.newInstance(); Object id = NBTTagCompoundConstructor.newInstance();
Method tagSetString = ReflUtil.getMethodCached(NBTTagCompoundClass, "setString", String.class, String.class); Method tagSetString = ReflUtil.getMethodCached(NBTTagCompoundClass, "setString", String.class, String.class);
String idString = type.getName(); String idString = type.getName();
if (ReflUtil.getNmsVersionObject().isHigherThanOrEqualTo(ReflUtil.V1_11_R1)) { if (ReflUtil.getNmsVersionObject().isHigherThanOrEqualTo(ReflUtil.V1_11_R1)) {
// 1.11 requires domain prefix of minecraft. // 1.11 requires domain prefix of minecraft.
@ -152,4 +152,3 @@ public class SpawnEggRefl {
} }
} }
} }

View File

@ -67,7 +67,7 @@ public class BasePotionDataProvider extends PotionMetaProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "1.9+ BasePotionData provider"; return "1.9+ BasePotionData provider";
} }
} }

View File

@ -26,7 +26,7 @@ public class BlockMetaSpawnerProvider extends SpawnerProvider {
} }
@Override @Override
public String getHumanName() { public String getDescription() {
return "1.8.3+ BlockStateMeta provider"; return "1.8.3+ BlockStateMeta provider";
} }
} }