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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
🐺⁉️🦀
  • Loading branch information
AmarokIce committed Sep 20, 2024
commit 2439f74fbce900901b076f717aa847f2a0added8
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
package band.kessoku.lib.base;

import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.RegisterCommandsEvent;

@Mod(KessokuBase.MOD_ID)
public class KessokuBaseEntrypoint {
Original file line number Diff line number Diff line change
@@ -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());
@@ -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());
@@ -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);
@@ -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];

Original file line number Diff line number Diff line change
@@ -25,21 +25,13 @@ 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;
default -> null;
};
}

@Override
Original file line number Diff line number Diff line change
@@ -178,15 +178,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) {
Original file line number Diff line number Diff line change
@@ -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
@@ -17,12 +17,16 @@

import band.kessoku.lib.registry.api.Registry;
import com.google.auto.service.AutoService;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
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)
@@ -39,12 +43,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;
}
}