Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize : More opti. #12

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public void reset() {

List<Field> fields = new ArrayList<>();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(ConfigValue.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());
final boolean flag2 = !Modifier.isStatic(declaredField.getModifiers());
Expand All @@ -163,6 +165,7 @@ public ImmutableList<AbstractConfig> getValidCategories() {

List<Field> fields = new ArrayList<>();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(AbstractConfig.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());
Expand All @@ -178,6 +181,7 @@ public ImmutableList<AbstractConfig> getValidCategories() {
private ImmutableList<Field> getValidFields() {
ImmutableList.Builder<Field> builder = ImmutableList.builder();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(AbstractConfig.class);
final boolean flag1 = declaredField.getDeclaringClass().isAssignableFrom(ConfigValue.class);
Expand All @@ -196,6 +200,7 @@ private Map<String, ValueWithComment> serialize() {
ImmutableMap.Builder<String, ValueWithComment> builder = ImmutableMap.builder();
for (Field field : this.getValidFields()) {
field.setAccessible(true);

final String name = field.isAnnotationPresent(Name.class) ? field.getAnnotation(Name.class).value() : field.getName();
final String[] comments = field.isAnnotationPresent(Comments.class) ? (String[]) Arrays.stream(field.getAnnotation(Comments.class).value()).map(Comment::value).toArray() : new String[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

@AutoService(KeyBindRegister.class)
public class KeyBindRegisterImpl implements KeyBindRegister {
private static final List<KeyBinding> KEY_BINDINGS = new ReferenceArrayList<>();
private static boolean processed;

/* Amarok Note:
Maybe Object2IntMap ...?
*/
private static Map<String, Integer> getCategoryMap() {
return KeyBindingAccessor.kessoku$getCategoryMap();
}
Expand All @@ -46,9 +48,8 @@ public boolean addCategory(String categoryTranslationKey) {
return false;
}

Optional<Integer> largest = map.values().stream().max(Integer::compareTo);
int largestInt = largest.orElse(0);
map.put(categoryTranslationKey, largestInt + 1);
final int largest = map.values().stream().max(Integer::compareTo).orElse(0);
map.put(categoryTranslationKey, largest + 1);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,12 @@ public ModDependencyInfoImpl(IModInfo.ModVersion modVersion) {
}
@Override
public DependencyKind getKind() {
switch (value.getType()) {
case OPTIONAL -> {
return DependencyKind.OPTIONAL;
}
case REQUIRED -> {
return DependencyKind.DEPENDS;
}
case DISCOURAGED -> {
return DependencyKind.CONFLICTS;
}
case INCOMPATIBLE -> {
return DependencyKind.BREAKS;
}
}
return null;
return switch (value.getType()) {
case OPTIONAL -> DependencyKind.OPTIONAL;
case REQUIRED -> DependencyKind.DEPENDS;
case DISCOURAGED -> DependencyKind.CONFLICTS;
case INCOMPATIBLE -> DependencyKind.BREAKS;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.minecraft.block.AbstractFurnaceBlock;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.component.ComponentMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
Expand All @@ -28,7 +26,6 @@
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -178,15 +175,11 @@ public static void setFuelTime(ItemConvertible item, int cookTime) {
}

/**
* @param block the block which acts like a furnace
* @return The fuel time.
*/
public static <T extends AbstractFurnaceBlock> int getFuelTime(T block, ItemStack stack) {
BlockEntity entity = block.createBlockEntity((BlockPos) BlockPos.ZERO, block.getDefaultState());
if (!(entity instanceof AbstractFurnaceBlockEntity)) return 0;
int fuelTime = ((AbstractFurnaceBlockEntity) entity).getFuelTime(stack);
entity.markRemoved();
return fuelTime;
public static int getFuelTime(ItemStack stack) {
final var fuelMap = AbstractFurnaceBlockEntity.createFuelTimeMap();
return fuelMap.getOrDefault(stack.getItem(), 0);
}

public record ItemWithData(ComponentMap componentMap, ItemConvertible item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
import net.neoforged.neoforge.event.furnace.FurnaceFuelBurnTimeEvent;
import net.neoforged.neoforge.registries.RegisterEvent;

import java.util.Objects;
import java.util.Optional;

@Mod(KessokuRegistry.MOD_ID)
public class KessokuRegistryEntrypoint {
public KessokuRegistryEntrypoint(IEventBus modEventBus) {
ModUtils.getLogger().info(KessokuRegistry.MARKER, "KessokuLib-Registry is loaded!");
NeoEventUtils.registerEvent(modEventBus, RegisterEvent.class, RegistryImpl::onRegister);
NeoEventUtils.registerEvent(NeoForge.EVENT_BUS, FurnaceFuelBurnTimeEvent.class, event -> {
ItemStack stack = event.getItemStack();
RecipeType<?> recipeType = event.getRecipeType();
Integer burnTime;
if (recipeType != null) burnTime = FuelRegistry.of(recipeType).get(stack);
else burnTime = FuelRegistry.of(RecipeType.SMELTING).get(stack);
if (burnTime != null) event.setBurnTime(burnTime);
final ItemStack stack = event.getItemStack();
final RecipeType<?> recipeType = Objects.requireNonNullElse(event.getRecipeType(), RecipeType.SMELTING);
Optional.ofNullable(FuelRegistry.of(recipeType).get(stack)).ifPresent(event::setBurnTime);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import band.kessoku.lib.registry.api.Registry;
import com.google.auto.service.AutoService;
import com.google.common.collect.Maps;
import net.minecraft.util.Identifier;
import net.neoforged.neoforge.registries.RegisterEvent;
import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@AutoService(Registry.class)
Expand All @@ -39,12 +40,15 @@ public static void onRegister(RegisterEvent event) {

@Override
public <V, T extends V> T registerInternal(net.minecraft.registry.Registry<V> registry, Identifier id, T entry) {
if (registered)
if (registered) {
throw new IllegalStateException("Cannot register new entries after net.neoforged.neoforge.registries.RegisterEvent has been fired.");
if (!registries.containsKey(registry)) registries.put(registry, new HashMap<>());
if (registries.get(registry).putIfAbsent(id, entry) != null) {
}

final var map = Objects.requireNonNull(registries.putIfAbsent(registry, Maps.newHashMap()));
if (map.putIfAbsent(id, entry) != null) {
throw new IllegalArgumentException("Duplicate registration " + id.toString());
}

return entry;
}
}