1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Merge pull request #2301 from WalshyDev/performance/666-performance-fixes

Micro-optimisations for #666
This commit is contained in:
TheBusyBiscuit 2020-09-08 11:31:37 +02:00 committed by GitHub
commit 0c11a41d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 69 additions and 13 deletions

View File

@ -50,6 +50,8 @@ public enum MinecraftVersion {
*/
UNIT_TEST("Unit Test Environment");
public static final MinecraftVersion[] values = values();
private final String name;
private final String prefix;

View File

@ -1,7 +1,7 @@
package io.github.thebusybiscuit.slimefun4.api.geo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.OptionalInt;
@ -175,7 +175,7 @@ public class ResourceManager {
menu.addItem(4, new CustomItem(HeadTexture.MINECRAFT_CHUNK.getAsItemStack(), ChatColor.YELLOW + SlimefunPlugin.getLocalization().getResourceString(p, "tooltips.chunk"), "", "&8\u21E8 &7" + SlimefunPlugin.getLocalization().getResourceString(p, "tooltips.world") + ": " + block.getWorld().getName(), "&8\u21E8 &7X: " + x + " Z: " + z), ChestMenuUtils.getEmptyClickHandler());
List<GEOResource> resources = new ArrayList<>(SlimefunPlugin.getRegistry().getGEOResources().values());
Collections.sort(resources, (a, b) -> a.getName(p).toLowerCase(Locale.ROOT).compareTo(b.getName(p).toLowerCase(Locale.ROOT)));
resources.sort(Comparator.comparing(a -> a.getName(p).toLowerCase(Locale.ROOT)));
int index = 10;
int pages = (resources.size() - 1) / 36 + 1;

View File

@ -31,4 +31,6 @@ public enum SlimefunGuideLayout {
*/
CHEAT_SHEET;
public static final SlimefunGuideLayout[] values = values();
}

View File

@ -101,7 +101,7 @@ class GuideLayoutOption implements SlimefunGuideOption<SlimefunGuideLayout> {
@Override
public Optional<SlimefunGuideLayout> getSelectedOption(Player p, ItemStack guide) {
for (SlimefunGuideLayout layout : SlimefunGuideLayout.values()) {
for (SlimefunGuideLayout layout : SlimefunGuideLayout.values) {
if (SlimefunUtils.isItemSimilar(guide, SlimefunGuide.getItem(layout), true, false)) {
return Optional.of(layout);
}

View File

@ -85,7 +85,7 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
protected abstract void addLanguage(@Nonnull String id, @Nonnull String texture);
protected void loadEmbeddedLanguages() {
for (SupportedLanguage lang : SupportedLanguage.values()) {
for (SupportedLanguage lang : SupportedLanguage.values) {
if (lang.isReadyForRelease() || SlimefunPlugin.getUpdater().getBranch() != SlimefunBranch.STABLE) {
addLanguage(lang.getLanguageId(), lang.getTexture());
}

View File

@ -58,6 +58,8 @@ enum SupportedLanguage {
MACEDONIAN("mk", false, "a0e0b0b5d87a855466980a101a757bcdb5f77d9f7287889f3efa998ee0472fc0"),
TAGALOG("tl", true, "9306c0c1ce6a9c61bb42a572c49e6d0ed20e0e6b3d122cc64c339cbf78e9e937");
public static final SupportedLanguage[] values = values();
private final String id;
private final boolean releaseReady;
private final String textureHash;

View File

@ -31,6 +31,8 @@ public enum PerformanceRating implements Predicate<Float> {
HURTFUL(ChatColor.DARK_RED, 500),
BAD(ChatColor.DARK_RED, Float.MAX_VALUE);
public static final PerformanceRating[] values = values();
private final ChatColor color;
private final float threshold;

View File

@ -297,7 +297,7 @@ public class SlimefunProfiler {
public PerformanceRating getPerformance() {
float percentage = getPercentageOfTick();
for (PerformanceRating rating : PerformanceRating.values()) {
for (PerformanceRating rating : PerformanceRating.values) {
if (rating.test(percentage)) {
return rating;
}

View File

@ -313,7 +313,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
String currentVersion = ReflectionUtils.getVersion();
if (currentVersion.startsWith("v")) {
for (MinecraftVersion version : MinecraftVersion.values()) {
for (MinecraftVersion version : MinecraftVersion.values) {
if (version.matches(currentVersion)) {
minecraftVersion = version;
return false;
@ -340,7 +340,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
private Collection<String> getSupportedVersions() {
List<String> list = new ArrayList<>();
for (MinecraftVersion version : MinecraftVersion.values()) {
for (MinecraftVersion version : MinecraftVersion.values) {
if (version != MinecraftVersion.UNKNOWN) {
list.add(version.getName());
}

View File

@ -1,5 +1,10 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.androids;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import org.apache.commons.lang.Validate;
@ -140,10 +145,20 @@ enum Instruction {
android.refuel(inv, target);
});
private static final Map<String, Instruction> cacheMap = 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) {
cacheMap.put(instruction.name(), instruction);
}
}
Instruction(AndroidType type, HeadTexture head, AndroidAction method) {
this.type = type;
this.item = SlimefunUtils.getCustomHead(head.getTexture());
@ -162,9 +177,23 @@ enum Instruction {
return type;
}
@ParametersAreNonnullByDefault
public void execute(ProgrammableAndroid android, Block b, BlockMenu inventory, BlockFace face) {
Validate.notNull(method, "Instruction '" + name() + "' must be executed manually!");
method.perform(android, b, inventory, face);
}
/**
* 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 Instruction} or null if it does not exist.
*/
@Nullable
public static Instruction getFromCache(@Nonnull String value) {
return cacheMap.get(value);
}
}

View File

@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
@ -254,8 +255,17 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
});
}
else {
ItemStack stack = Instruction.valueOf(script[i]).getItem();
menu.addItem(i, new CustomItem(stack, SlimefunPlugin.getLocalization().getMessage(p, "android.scripts.instructions." + Instruction.valueOf(script[i]).name()), "", "&7\u21E8 &eLeft Click &7to edit", "&7\u21E8 &eRight Click &7to delete", "&7\u21E8 &eShift + Right Click &7to duplicate"));
Instruction instruction = Instruction.getFromCache(script[i]);
if (instruction == null) {
SlimefunPlugin.instance().getLogger().log(Level.WARNING,
"Failed to get instruction '{0}', maybe your server is out of date?",
script[i]
);
return;
}
ItemStack stack = instruction.getItem();
menu.addItem(i, new CustomItem(stack, SlimefunPlugin.getLocalization().getMessage(p, "android.scripts.instructions." + instruction.name()), "", "&7\u21E8 &eLeft Click &7to edit", "&7\u21E8 &eRight Click &7to delete", "&7\u21E8 &eShift + Right Click &7to duplicate"));
menu.addMenuClickHandler(i, (pl, slot, item, action) -> {
if (action.isRightClicked() && action.isShiftClicked()) {
if (script.length == 54) {
@ -285,7 +295,7 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
private String addInstruction(String[] script, int index, Instruction instruction) {
int i = 0;
StringBuilder builder = new StringBuilder(Instruction.START + "-");
StringBuilder builder = new StringBuilder(Instruction.START.name() + '-');
for (String current : script) {
if (i > 0) {
@ -494,7 +504,7 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
protected List<Instruction> getValidScriptInstructions() {
List<Instruction> list = new ArrayList<>();
for (Instruction part : Instruction.values()) {
for (Instruction part : Instruction.values) {
if (part == Instruction.START || part == Instruction.REPEAT) {
continue;
}
@ -647,7 +657,14 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
}
BlockStorage.addBlockInfo(b, "fuel", String.valueOf(fuel - 1));
Instruction instruction = Instruction.valueOf(script[index]);
Instruction instruction = Instruction.getFromCache(script[index]);
if (instruction == null) {
SlimefunPlugin.instance().getLogger().log(Level.WARNING,
"Failed to get instruction '{0}', maybe your server is out of date?",
script[index]
);
return;
}
executeInstruction(instruction, b, menu, data, index);
}
}

View File

@ -109,6 +109,8 @@ public enum HeadTexture {
IRON_GOLEM("89091d79ea0f59ef7ef94d7bba6e5f17f2f7d4572c44f90f76c4819a714"),
PIGLIN_HEAD("2882af1294a74023e6919a31d1a027310f2e142afb4667d230d155e7f21dbb41");
public static final HeadTexture[] values = values();
private final String texture;
HeadTexture(@Nonnull String texture) {

View File

@ -16,7 +16,7 @@ class TestHeadTextures {
void testForDuplicates() {
Set<String> textures = new HashSet<>();
for (HeadTexture head : HeadTexture.values()) {
for (HeadTexture head : HeadTexture.values) {
String texture = head.getTexture();
Assertions.assertNotNull(texture);