Skip to content

Commit

Permalink
Fixed .burnTime() method in item builder and .burnTime = property in …
Browse files Browse the repository at this point in the history
…item modification event
  • Loading branch information
LatvianModder committed Aug 29, 2024
1 parent bc86c79 commit 7f7818b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
import dev.latvian.mods.kubejs.util.RotationAxis;
import dev.latvian.mods.kubejs.util.ScheduledEvents;
import dev.latvian.mods.kubejs.util.SlotFilter;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.kubejs.util.TimeJS;
import dev.latvian.mods.kubejs.util.Tristate;
import dev.latvian.mods.kubejs.util.UtilsJS;
Expand Down Expand Up @@ -547,6 +548,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(File.class, KubeJSTypeWrappers::fileOf);
registry.register(TemporalAmount.class, TimeJS::temporalAmountOf);
registry.register(Duration.class, TimeJS::durationOf);
registry.register(TickDuration.class, TickDuration::wrap);

registry.register(ResourceLocation.class, ID::mc);
registry.register(KubeResourceLocation.class, KubeResourceLocation::wrap);
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidStack;
Expand Down Expand Up @@ -185,9 +184,9 @@ static FluidStack read(DynamicOps<Tag> registryOps, StringReader reader) throws
return FluidStack.EMPTY;
}

int amount = readFluidAmount(reader);
long amount = readFluidAmount(reader);
var fluidId = ResourceLocation.read(reader);
var fluidStack = new FluidStack(BuiltInRegistries.FLUID.get(fluidId), amount);
var fluidStack = new FluidStack(BuiltInRegistries.FLUID.get(fluidId), (int) amount);

var next = reader.canRead() ? reader.peek() : 0;

Expand Down Expand Up @@ -278,11 +277,11 @@ static SizedFluidIngredient readSizedIngredient(DynamicOps<Tag> registryOps, Str
return EMPTY_SIZED;
}

int amount = readFluidAmount(reader);
return new SizedFluidIngredient(readIngredient(registryOps, reader), amount);
long amount = readFluidAmount(reader);
return new SizedFluidIngredient(readIngredient(registryOps, reader), (int) amount);
}

static int readFluidAmount(StringReader reader) throws CommandSyntaxException {
static long readFluidAmount(StringReader reader) throws CommandSyntaxException {
if (reader.canRead() && StringReader.isAllowedNumber(reader.peek())) {
var amountd = reader.readDouble();
reader.skipWhitespace();
Expand All @@ -299,11 +298,11 @@ static int readFluidAmount(StringReader reader) throws CommandSyntaxException {
amountd = amountd / reader.readDouble();
}

int amount = Mth.ceil(amountd);
long amount = (long) amountd;
reader.expect('x');
reader.skipWhitespace();

if (amount < 1) {
if (amount < 1L) {
throw new IllegalArgumentException("Fluid amount smaller than 1 is not allowed!");
}

Expand Down
14 changes: 5 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.latvian.mods.kubejs.registry.BuilderBase;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
Expand Down Expand Up @@ -50,7 +51,7 @@ public record HurtEnemyContext(ItemStack getItem, LivingEntity getTarget, Living
public transient Map<Object, Object> components;
public transient int maxStackSize;
public transient int maxDamage;
public transient int burnTime;
public transient long burnTime;
private ResourceLocation containerItem;
public transient Function<ItemStack, Collection<ItemStack>> subtypes;
public transient Rarity rarity;
Expand Down Expand Up @@ -85,7 +86,7 @@ public ItemBuilder(ResourceLocation i) {
super(i);
maxStackSize = -1;
maxDamage = 0;
burnTime = 0;
burnTime = 0L;
containerItem = null;
subtypes = null;
rarity = null;
Expand Down Expand Up @@ -116,11 +117,6 @@ public Item createObject() {
@Override
public Item transformObject(Item obj) {
obj.kjs$setItemBuilder(this);

if (burnTime > 0) {
// FIXME: FuelRegistry.register(burnTime, obj);
}

return obj;
}

Expand Down Expand Up @@ -173,8 +169,8 @@ public ItemBuilder maxDamage(int v) {
}

@Info("Sets the item's burn time. Default is 0 (Not a fuel).")
public ItemBuilder burnTime(int v) {
burnTime = v;
public ItemBuilder burnTime(TickDuration v) {
burnTime = v.ticks();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import dev.latvian.mods.kubejs.component.ComponentFunctions;
import dev.latvian.mods.kubejs.component.ItemComponentFunctions;
import dev.latvian.mods.kubejs.event.KubeEvent;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.rhino.util.HideFromJS;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.Util;
Expand All @@ -14,13 +14,14 @@
import net.minecraft.world.item.TieredItem;
import org.jetbrains.annotations.Nullable;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Consumer;

@Info("""
Invoked after all items are registered to modify them.
""")
public class ItemModificationKubeEvent implements KubeEvent {

@Info("""
Modifies items matching the given ingredient.
Expand All @@ -32,6 +33,9 @@ public void modify(ItemPredicate in, Consumer<ItemModifications> c) {

@RemapPrefixForJS("kjs$")
public record ItemModifications(Item item) implements ItemComponentFunctions {
@HideFromJS
public static final Map<Item, Long> BURN_TIME_OVERRIDES = new IdentityHashMap<>();

@Override
public DataComponentMap kjs$getComponentMap() {
return item.components();
Expand All @@ -44,9 +48,8 @@ public record ItemModifications(Item item) implements ItemComponentFunctions {
return this;
}

public void setBurnTime(int i) {
ConsoleJS.STARTUP.error("Setting item burn time is currently not supported");
// FuelRegistry.register(i, (Item) this);
public void setBurnTime(TickDuration i) {
BURN_TIME_OVERRIDES.put(item, i.ticks());
}

public void setCraftingRemainder(Item item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.latvian.mods.kubejs.server;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
import dev.latvian.mods.kubejs.core.RecipeManagerKJS;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.item.ItemBuilder;
import dev.latvian.mods.kubejs.item.ItemModificationKubeEvent;
import dev.latvian.mods.kubejs.net.KubeServerData;
import dev.latvian.mods.kubejs.net.SyncServerDataPayload;
import dev.latvian.mods.kubejs.plugin.KubeJSPlugin;
Expand Down Expand Up @@ -142,6 +145,30 @@ public void loadAdditional() {

KubeJSPlugins.forEachPlugin(internalDataPack, KubeJSPlugin::generateData);

var furnaceFuelsJson = new JsonObject();

for (var entry : ItemModificationKubeEvent.ItemModifications.BURN_TIME_OVERRIDES.entrySet()) {
var json = new JsonObject();
json.addProperty("burn_time", entry.getValue());
furnaceFuelsJson.add(entry.getKey().kjs$getId(), json);
}

for (var item : RegistryObjectStorage.ITEM) {
long b = ((ItemBuilder) item).burnTime;

if (b > 0L) {
var json = new JsonObject();
json.addProperty("burn_time", b);
furnaceFuelsJson.add(item.id.toString(), json);
}
}

if (!furnaceFuelsJson.isEmpty()) {
var json = new JsonObject();
json.add("values", furnaceFuelsJson);
internalDataPack.json(ResourceLocation.fromNamespaceAndPath("neoforge", "data_maps/item/furnace_fuels.json"), json);
}

if (firstLoad) {
firstLoad = false;

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/util/TickDuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public record TickDuration(long ticks) implements TemporalAmount {

public static final TypeInfo TYPE_INFO = TypeInfo.of(TickDuration.class); // TypeInfo.NUMBER.or(TypeInfo.STRING)

public static TickDuration wrap(Object from) {
if (from instanceof Number n) {
return new TickDuration(n.longValue());
} else {
return new TickDuration(TimeJS.durationOf(from).toMillis() / 50L);
}
}

@Override
public long get(TemporalUnit unit) {
if (unit == TickTemporalUnit.INSTANCE) {
Expand Down

0 comments on commit 7f7818b

Please sign in to comment.