Reduce the amount of values() calls (#2143)

Because apparently it's not just a constant; it makes a new array every time.
Also, GeyserSession#tick() does not need to be public and I made enchantments in commands use the Java list.
This commit is contained in:
Camotoy 2021-04-26 14:15:24 -04:00 committed by GitHub
parent 873e37e1c0
commit 9b39affd28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 71 deletions

View file

@ -188,7 +188,7 @@ public enum EntityType {
static { static {
List<String> allJavaIdentifiers = new ArrayList<>(); List<String> allJavaIdentifiers = new ArrayList<>();
for (EntityType type : values()) { for (EntityType type : VALUES) {
if (type == AGENT || type == BALLOON || type == CHALKBOARD || type == NPC || type == TRIPOD_CAMERA || type == ENDER_DRAGON_PART) { if (type == AGENT || type == BALLOON || type == CHALKBOARD || type == NPC || type == TRIPOD_CAMERA || type == ENDER_DRAGON_PART) {
continue; continue;
} }

View file

@ -850,7 +850,7 @@ public class GeyserSession implements CommandSender {
/** /**
* Called every 50 milliseconds - one Minecraft tick. * Called every 50 milliseconds - one Minecraft tick.
*/ */
public void tick() { protected void tick() {
// Check to see if the player's position needs updating - a position update should be sent once every 3 seconds // Check to see if the player's position needs updating - a position update should be sent once every 3 seconds
if (spawned && (System.currentTimeMillis() - lastMovementTimestamp) > 3000) { if (spawned && (System.currentTimeMillis() - lastMovementTimestamp) > 3000) {
// Recalculate in case something else changed position // Recalculate in case something else changed position

View file

@ -54,7 +54,7 @@ public class BedrockLecternUpdateTranslator extends PacketTranslator<LecternUpda
// Emulate an interact packet // Emulate an interact packet
ClientPlayerPlaceBlockPacket blockPacket = new ClientPlayerPlaceBlockPacket( ClientPlayerPlaceBlockPacket blockPacket = new ClientPlayerPlaceBlockPacket(
new Position(packet.getBlockPosition().getX(), packet.getBlockPosition().getY(), packet.getBlockPosition().getZ()), new Position(packet.getBlockPosition().getX(), packet.getBlockPosition().getY(), packet.getBlockPosition().getZ()),
BlockFace.values()[0], BlockFace.DOWN,
Hand.MAIN_HAND, Hand.MAIN_HAND,
0, 0, 0, // Java doesn't care about these when dealing with a lectern 0, 0, 0, // Java doesn't care about these when dealing with a lectern
false); false);

View file

@ -72,7 +72,7 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
// The Bedrock index might need changed, so let's look it up and see. // The Bedrock index might need changed, so let's look it up and see.
int bedrockIndex = value; int bedrockIndex = value;
if (bedrockIndex != -1) { if (bedrockIndex != -1) {
Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + JavaEnchantment.values()[bedrockIndex].name().toLowerCase()); Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + Enchantment.JavaEnchantment.of(bedrockIndex).name().toLowerCase());
if (enchantment != null) { if (enchantment != null) {
// Convert the Java enchantment index to Bedrock's // Convert the Java enchantment index to Bedrock's
bedrockIndex = enchantment.ordinal(); bedrockIndex = enchantment.ordinal();
@ -170,48 +170,4 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
public Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory) { public Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory) {
return new EnchantingContainer(name, windowId, this.size, windowType, playerInventory); return new EnchantingContainer(name, windowId, this.size, windowType, playerInventory);
} }
/**
* Enchantments classified by their Java index
*/
public enum JavaEnchantment {
PROTECTION,
FIRE_PROTECTION,
FEATHER_FALLING,
BLAST_PROTECTION,
PROJECTILE_PROTECTION,
RESPIRATION,
AQUA_AFFINITY,
THORNS,
DEPTH_STRIDER,
FROST_WALKER,
BINDING_CURSE,
SOUL_SPEED,
SHARPNESS,
SMITE,
BANE_OF_ARTHROPODS,
KNOCKBACK,
FIRE_ASPECT,
LOOTING,
SWEEPING,
EFFICIENCY,
SILK_TOUCH,
UNBREAKING,
FORTUNE,
POWER,
PUNCH,
FLAME,
INFINITY,
LUCK_OF_THE_SEA,
LURE,
LOYALTY,
IMPALING,
RIPTIDE,
CHANNELING,
MULTISHOT,
QUICK_CHARGE,
PIERCING,
MENDING,
VANISHING_CURSE
}
} }

View file

@ -69,17 +69,7 @@ public enum Enchantment {
QUICK_CHARGE, QUICK_CHARGE,
SOUL_SPEED; SOUL_SPEED;
/** private static final Enchantment[] VALUES = values();
* A list of all enchantment Java identifiers for use with command suggestions.
*/
public static final String[] ALL_JAVA_IDENTIFIERS;
static {
ALL_JAVA_IDENTIFIERS = new String[values().length];
for (int i = 0; i < ALL_JAVA_IDENTIFIERS.length; i++) {
ALL_JAVA_IDENTIFIERS[i] = values()[i].javaIdentifier;
}
}
private final String javaIdentifier; private final String javaIdentifier;
@ -88,7 +78,7 @@ public enum Enchantment {
} }
public static Enchantment getByJavaIdentifier(String javaIdentifier) { public static Enchantment getByJavaIdentifier(String javaIdentifier) {
for (Enchantment enchantment : Enchantment.values()) { for (Enchantment enchantment : VALUES) {
if (enchantment.javaIdentifier.equals(javaIdentifier) || enchantment.name().toLowerCase(Locale.ENGLISH).equalsIgnoreCase(javaIdentifier)) { if (enchantment.javaIdentifier.equals(javaIdentifier) || enchantment.name().toLowerCase(Locale.ENGLISH).equalsIgnoreCase(javaIdentifier)) {
return enchantment; return enchantment;
} }
@ -97,9 +87,71 @@ public enum Enchantment {
} }
public static Enchantment getByBedrockId(int bedrockId) { public static Enchantment getByBedrockId(int bedrockId) {
if (bedrockId >= 0 && bedrockId < Enchantment.values().length) { if (bedrockId >= 0 && bedrockId < VALUES.length) {
return Enchantment.values()[bedrockId]; return VALUES[bedrockId];
} }
return null; return null;
} }
/**
* Enchantments classified by their Java index
*/
public enum JavaEnchantment {
PROTECTION,
FIRE_PROTECTION,
FEATHER_FALLING,
BLAST_PROTECTION,
PROJECTILE_PROTECTION,
RESPIRATION,
AQUA_AFFINITY,
THORNS,
DEPTH_STRIDER,
FROST_WALKER,
BINDING_CURSE,
SOUL_SPEED,
SHARPNESS,
SMITE,
BANE_OF_ARTHROPODS,
KNOCKBACK,
FIRE_ASPECT,
LOOTING,
SWEEPING,
EFFICIENCY,
SILK_TOUCH,
UNBREAKING,
FORTUNE,
POWER,
PUNCH,
FLAME,
INFINITY,
LUCK_OF_THE_SEA,
LURE,
LOYALTY,
IMPALING,
RIPTIDE,
CHANNELING,
MULTISHOT,
QUICK_CHARGE,
PIERCING,
MENDING,
VANISHING_CURSE;
private static final JavaEnchantment[] VALUES = JavaEnchantment.values();
public static JavaEnchantment of(int index) {
return VALUES[index];
}
/**
* A list of all enchantment Java identifiers for use with command suggestions.
*/
public static final String[] ALL_JAVA_IDENTIFIERS;
static {
ALL_JAVA_IDENTIFIERS = new String[VALUES.length];
for (int i = 0; i < ALL_JAVA_IDENTIFIERS.length; i++) {
ALL_JAVA_IDENTIFIERS[i] = "minecraft:" + VALUES[i].name().toLowerCase(Locale.ENGLISH);
}
}
}
} }

View file

@ -74,6 +74,8 @@ public enum Potion {
SLOW_FALLING(40), SLOW_FALLING(40),
LONG_SLOW_FALLING(41); LONG_SLOW_FALLING(41);
public static final Potion[] VALUES = values();
private final String javaIdentifier; private final String javaIdentifier;
private final short bedrockId; private final short bedrockId;
@ -83,7 +85,7 @@ public enum Potion {
} }
public static Potion getByJavaIdentifier(String javaIdentifier) { public static Potion getByJavaIdentifier(String javaIdentifier) {
for (Potion potion : Potion.values()) { for (Potion potion : VALUES) {
if (potion.javaIdentifier.equals(javaIdentifier)) { if (potion.javaIdentifier.equals(javaIdentifier)) {
return potion; return potion;
} }
@ -92,7 +94,7 @@ public enum Potion {
} }
public static Potion getByBedrockId(int bedrockId) { public static Potion getByBedrockId(int bedrockId) {
for (Potion potion : Potion.values()) { for (Potion potion : VALUES) {
if (potion.bedrockId == bedrockId) { if (potion.bedrockId == bedrockId) {
return potion; return potion;
} }

View file

@ -80,7 +80,7 @@ public class PotionMixRegistry {
// Add all types of potions as inputs // Add all types of potions as inputs
ItemEntry fillerIngredient = ingredients.get(0); ItemEntry fillerIngredient = ingredients.get(0);
for (ItemEntry input : inputs) { for (ItemEntry input : inputs) {
for (Potion potion : Potion.values()) { for (Potion potion : Potion.VALUES) {
potionMixes.add(new PotionMixData( potionMixes.add(new PotionMixData(
input.getBedrockId(), potion.getBedrockId(), input.getBedrockId(), potion.getBedrockId(),
fillerIngredient.getBedrockId(), fillerIngredient.getBedrockData(), fillerIngredient.getBedrockId(), fillerIngredient.getBedrockData(),

View file

@ -77,6 +77,8 @@ public enum TippedArrowPotion {
SLOW_FALLING(41, ArrowParticleColors.SLOW_FALLING), SLOW_FALLING(41, ArrowParticleColors.SLOW_FALLING),
LONG_SLOW_FALLING(42, ArrowParticleColors.SLOW_FALLING); LONG_SLOW_FALLING(42, ArrowParticleColors.SLOW_FALLING);
private static final TippedArrowPotion[] VALUES = values();
private final String javaIdentifier; private final String javaIdentifier;
private final short bedrockId; private final short bedrockId;
/** /**
@ -92,7 +94,7 @@ public enum TippedArrowPotion {
} }
public static TippedArrowPotion getByJavaIdentifier(String javaIdentifier) { public static TippedArrowPotion getByJavaIdentifier(String javaIdentifier) {
for (TippedArrowPotion potion : TippedArrowPotion.values()) { for (TippedArrowPotion potion : VALUES) {
if (potion.javaIdentifier.equals(javaIdentifier)) { if (potion.javaIdentifier.equals(javaIdentifier)) {
return potion; return potion;
} }
@ -101,7 +103,7 @@ public enum TippedArrowPotion {
} }
public static TippedArrowPotion getByBedrockId(int bedrockId) { public static TippedArrowPotion getByBedrockId(int bedrockId) {
for (TippedArrowPotion potion : TippedArrowPotion.values()) { for (TippedArrowPotion potion : VALUES) {
if (potion.bedrockId == bedrockId) { if (potion.bedrockId == bedrockId) {
return potion; return potion;
} }
@ -114,7 +116,7 @@ public enum TippedArrowPotion {
* @return the tipped arrow potion that most closely resembles that color. * @return the tipped arrow potion that most closely resembles that color.
*/ */
public static TippedArrowPotion getByJavaColor(int color) { public static TippedArrowPotion getByJavaColor(int color) {
for (TippedArrowPotion potion : TippedArrowPotion.values()) { for (TippedArrowPotion potion : VALUES) {
if (potion.javaColor == color) { if (potion.javaColor == color) {
return potion; return potion;
} }

View file

@ -249,7 +249,7 @@ public class JavaDeclareCommandsTranslator extends PacketTranslator<ServerDeclar
return ItemRegistry.ITEM_NAMES; return ItemRegistry.ITEM_NAMES;
case ITEM_ENCHANTMENT: case ITEM_ENCHANTMENT:
return Enchantment.ALL_JAVA_IDENTIFIERS; //TODO: inventory branch use Java enums return Enchantment.JavaEnchantment.ALL_JAVA_IDENTIFIERS;
case ENTITY_SUMMON: case ENTITY_SUMMON:
return EntityType.ALL_JAVA_IDENTIFIERS; return EntityType.ALL_JAVA_IDENTIFIERS;

View file

@ -46,6 +46,7 @@ import java.util.List;
@Translator(packet = ServerJoinGamePacket.class) @Translator(packet = ServerJoinGamePacket.class)
public class JavaJoinGameTranslator extends PacketTranslator<ServerJoinGamePacket> { public class JavaJoinGameTranslator extends PacketTranslator<ServerJoinGamePacket> {
private static final List<SkinPart> SKIN_PART_VALUES = Arrays.asList(SkinPart.values());
@Override @Override
public void translate(ServerJoinGamePacket packet, GeyserSession session) { public void translate(ServerJoinGamePacket packet, GeyserSession session) {
@ -92,8 +93,7 @@ public class JavaJoinGameTranslator extends PacketTranslator<ServerJoinGamePacke
// We need to send our skin parts to the server otherwise java sees us with no hat, jacket etc // We need to send our skin parts to the server otherwise java sees us with no hat, jacket etc
String locale = session.getLocale(); String locale = session.getLocale();
List<SkinPart> skinParts = Arrays.asList(SkinPart.values()); ClientSettingsPacket clientSettingsPacket = new ClientSettingsPacket(locale, (byte) session.getRenderDistance(), ChatVisibility.FULL, true, SKIN_PART_VALUES, HandPreference.RIGHT_HAND);
ClientSettingsPacket clientSettingsPacket = new ClientSettingsPacket(locale, (byte) session.getRenderDistance(), ChatVisibility.FULL, true, skinParts, HandPreference.RIGHT_HAND);
session.sendDownstreamPacket(clientSettingsPacket); session.sendDownstreamPacket(clientSettingsPacket);
session.sendDownstreamPacket(new ClientPluginMessagePacket("minecraft:brand", PluginMessageUtils.getGeyserBrandData())); session.sendDownstreamPacket(new ClientPluginMessagePacket("minecraft:brand", PluginMessageUtils.getGeyserBrandData()));