1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Micro-optimizations Walshy would be proud of

This commit is contained in:
TheBusyBiscuit 2020-09-09 15:43:44 +02:00
parent 035e35a067
commit 9655761ab1
4 changed files with 43 additions and 12 deletions

View File

@ -148,16 +148,16 @@ enum Instruction {
private static final Map<String, Instruction> nameLookup = new HashMap<>();
public static final Instruction[] values = values();
private final ItemStack item;
private final AndroidType type;
private final AndroidAction method;
static {
for (Instruction instruction : values) {
nameLookup.put(instruction.name(), instruction);
}
}
private final ItemStack item;
private final AndroidType type;
private final AndroidAction method;
@ParametersAreNonnullByDefault
Instruction(AndroidType type, HeadTexture head, @Nullable AndroidAction method) {
this.type = type;
@ -198,7 +198,7 @@ enum Instruction {
* @return The {@link Instruction} or null if it does not exist.
*/
@Nullable
public static Instruction getFromCache(@Nonnull String value) {
public static Instruction getInstruction(@Nonnull String value) {
Validate.notNull(value, "An Instruction cannot be null!");
return nameLookup.get(value);
}

View File

@ -255,7 +255,7 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
});
}
else {
Instruction instruction = Instruction.getFromCache(script[i]);
Instruction instruction = Instruction.getInstruction(script[i]);
if (instruction == null) {
SlimefunPlugin.instance().getLogger().log(Level.WARNING, "Failed to parse Android instruction: {0}, maybe your server is out of date?", script[i]);
@ -655,7 +655,7 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
}
BlockStorage.addBlockInfo(b, "fuel", String.valueOf(fuel - 1));
Instruction instruction = Instruction.getFromCache(script[index]);
Instruction instruction = Instruction.getInstruction(script[index]);
if (instruction == null) {
SlimefunPlugin.instance().getLogger().log(Level.WARNING, "Failed to parse Android instruction: {0}, maybe your server is out of date?", script[index]);

View File

@ -1,13 +1,17 @@
package io.github.thebusybiscuit.slimefun4.utils.tags;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
@ -107,6 +111,15 @@ public enum SlimefunTag implements Tag<Material> {
*/
CLIMBING_PICK_SURFACES;
private static final Map<String, SlimefunTag> nameLookup = new HashMap<>();
public static final SlimefunTag[] values = values();
static {
for (SlimefunTag tag : values) {
nameLookup.put(tag.name(), tag);
}
}
private final NamespacedKey key;
private final Set<Material> includedMaterials = new HashSet<>();
private final Set<Tag<Material>> additionalTags = new HashSet<>();
@ -210,4 +223,21 @@ public enum SlimefunTag implements Tag<Material> {
return getValues().stream();
}
/**
* Get a value from the cache map rather than calling {@link Enum#valueOf(Class, String)}.
* This is 25-40% quicker than the standard {@link Enum#valueOf(Class, String)} depending on
* your Java version. It also means that you can avoid an IllegalArgumentException which let's
* face it is always good.
*
* @param value
* The value which you would like to look up.
*
* @return The {@link SlimefunTag} or null if it does not exist.
*/
@Nullable
public static SlimefunTag getTag(@Nonnull String value) {
Validate.notNull(value, "A tag cannot be null!");
return nameLookup.get(value);
}
}

View File

@ -162,13 +162,14 @@ public class TagParser implements Keyed {
}
}
else if (PatternUtils.SLIMEFUN_TAG.matcher(value).matches()) {
try {
// Get a SlimefunTag enum value for the given key
String keyValue = PatternUtils.COLON.split(value)[1].toUpperCase(Locale.ROOT);
SlimefunTag tag = SlimefunTag.valueOf(keyValue);
// Get a SlimefunTag enum value for the given key
String keyValue = PatternUtils.COLON.split(value)[1].toUpperCase(Locale.ROOT);
SlimefunTag tag = SlimefunTag.getTag(keyValue);
if (tag != null) {
tags.add(tag);
}
catch (IllegalArgumentException x) {
else {
throw new TagMisconfigurationException(key, "There is no '" + value + "' tag in Slimefun");
}
}