Skip to content

Commit

Permalink
Added /api/assets.zip endpoint, added event.respond(text) to basic co…
Browse files Browse the repository at this point in the history
…mmand event
  • Loading branch information
LatvianModder committed Dec 19, 2024
1 parent c982918 commit 7163a1a
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 62 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod_version=2101.7.2
neoforge_version=21.1.83
parchment_mc_version=1.21
parchment_mapping_version=2024.11.10
rhino_version=2101.2.6-build.56
rhino_version=2101.2.6-build.58
tiny_server_version=1.0.0-build.25
gif_lib_version=1.7

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(RegistryPredicate.class, RegistryPredicate::of);

// Java / Minecraft //
registry.register(String.class, String::valueOf);
registry.register(CharSequence.class, String::valueOf);
// registry.register(String.class, String::valueOf);
// registry.register(CharSequence.class, String::valueOf);
registry.register(UUID.class, UUIDWrapper::fromString);
registry.register(Pattern.class, RegExpKJS::wrap);
registry.register(JsonObject.class, JsonUtils::objectOf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.ResourceKeyArgument;
import net.minecraft.commands.arguments.ResourceLocationArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.ClickEvent;
Expand Down Expand Up @@ -293,7 +292,7 @@ private static int help(CommandSourceStack source) {

private static int customCommand(TargetedEventHandler<String> event, CommandSourceStack source, String id, String input) {
if (event.hasListeners(id)) {
var result = event.post(new BasicCommandKubeEvent(source.getLevel(), source.getEntity(), BlockPos.containing(source.getPosition()), id, input.trim()), id);
var result = event.post(new BasicCommandKubeEvent(source, id, input.trim()), id);

if (result.value() instanceof Throwable ex) {
source.sendFailure(Component.literal(ex.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public interface MinecraftEnvironmentKJS extends MessageSenderKJS {
}

default ScheduledEvents.ScheduledEvent kjs$scheduleInTicks(long ticks, ScheduledEvents.Callback callback) {
return kjs$getScheduledEvents().schedule(new TickDuration(ticks), false, callback);
return kjs$getScheduledEvents().schedule(TickDuration.of(ticks), false, callback);
}

default ScheduledEvents.ScheduledEvent kjs$scheduleRepeating(TemporalAmount timer, ScheduledEvents.Callback callback) {
return kjs$getScheduledEvents().schedule(timer, false, callback);
}

default ScheduledEvents.ScheduledEvent kjs$scheduleRepeatingInTicks(long ticks, ScheduledEvents.Callback callback) {
return kjs$getScheduledEvents().schedule(new TickDuration(ticks), true, callback);
return kjs$getScheduledEvents().schedule(TickDuration.of(ticks), true, callback);
}
}
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/mods/kubejs/net/KubeJSNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface Kubedex {

@SubscribeEvent
static void register(RegisterPayloadHandlersEvent event) {
var reg = event.registrar("1");
var reg = event.registrar("1").optional();

reg.playToClient(WEB_SERVER_JSON_UPDATE, WebServerUpdateJSONPayload.STREAM_CODEC, WebServerUpdateJSONPayload::handle);
reg.playToClient(WEB_SERVER_NBT_UPDATE, WebServerUpdateNBTPayload.STREAM_CODEC, WebServerUpdateNBTPayload::handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mojang.serialization.Codec;
import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.kubejs.util.TimeJS;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;

Expand Down Expand Up @@ -32,9 +31,9 @@ public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) {
@Override
public TickDuration wrap(Context cx, KubeRecipe recipe, Object from) {
if (from instanceof Number n) {
return new TickDuration((long) (n.doubleValue() * scale));
return TickDuration.of((long) (n.doubleValue() * scale));
} else {
return new TickDuration(TimeJS.wrapDuration(from).toMillis() / 50L);
return TickDuration.wrap(from);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.script.data;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import dev.latvian.mods.kubejs.KubeJS;
Expand All @@ -22,7 +23,11 @@ public record GeneratedData(ResourceLocation id, Supplier<byte[]> data) implemen
var json = new JsonObject();
var pack = new JsonObject();
pack.addProperty("description", "KubeJS Pack");
pack.addProperty("pack_format", 15);
pack.addProperty("pack_format", 8);
var arr = new JsonArray();
arr.add(8);
arr.add(99);
pack.add("supported_formats", arr);
json.add("pack", pack);
return json.toString().getBytes(StandardCharsets.UTF_8);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@

import dev.latvian.mods.kubejs.entity.KubeEntityEvent;
import dev.latvian.mods.kubejs.level.LevelBlock;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

public class BasicCommandKubeEvent implements KubeEntityEvent {
private final CommandSourceStack source;
private final Level level;
private final Entity entity;
private final ServerPlayer serverPlayer;
private final BlockPos pos;
public final String id;
public final String input;

public BasicCommandKubeEvent(Level level, @Nullable Entity entity, BlockPos pos, String id, String input) {
this.level = level;
this.entity = entity;
public BasicCommandKubeEvent(CommandSourceStack source, String id, String input) {
this.source = source;
this.level = source.getLevel();
this.entity = source.getEntity();
this.serverPlayer = entity instanceof ServerPlayer p ? p : null;
this.pos = pos;
this.pos = BlockPos.containing(source.getPosition());
this.id = id;
this.input = input;
}
Expand Down Expand Up @@ -49,4 +55,12 @@ public ServerPlayer getPlayer() {
public LevelBlock getBlock() {
return this.getLevel().kjs$getBlock(pos);
}

public void respondLazily(Supplier<Component> text, boolean informAdmins) {
source.sendSuccess(text, informAdmins);
}

public void respond(Component text) {
respondLazily(() -> text, false);
}
}
3 changes: 2 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/util/ID.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonPrimitive;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.core.RegistryObjectKJS;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import net.minecraft.ResourceLocationException;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -91,7 +92,7 @@ static ResourceLocation of(@Nullable Object o, boolean preferKJS) {
try {
return ResourceLocation.parse(s);
} catch (ResourceLocationException ex) {
throw new IllegalArgumentException("Could not create ID from '%s'!".formatted(s));
throw new KubeRuntimeException("Could not create ID from '%s'!".formatted(s));
}
}

Expand Down
30 changes: 21 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/util/TickDuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.util;

import com.google.gson.JsonPrimitive;
import com.mojang.serialization.Codec;
import dev.latvian.mods.rhino.type.TypeInfo;

Expand All @@ -11,19 +12,25 @@
public record TickDuration(long ticks) implements TemporalAmount {
public static final TickDuration ZERO = new TickDuration(0L);
private static final List<TemporalUnit> UNITS = List.of(TickTemporalUnit.INSTANCE);
public static final Codec<TickDuration> CODEC = Codec.LONG.xmap(TickDuration::new, TickDuration::ticks);
public static final Codec<TickDuration> SECONDS_CODEC = Codec.DOUBLE.xmap(l -> new TickDuration((long) (l * 20D)), t -> t.ticks() / 20D);
public static final Codec<TickDuration> MINUTES_CODEC = Codec.DOUBLE.xmap(l -> new TickDuration((long) (l * 1200L)), t -> t.ticks() / 1200D);
public static final Codec<TickDuration> HOURS_CODEC = Codec.DOUBLE.xmap(l -> new TickDuration((long) (l * 72000L)), t -> t.ticks() / 72000D);
public static final Codec<TickDuration> CODEC = Codec.LONG.xmap(TickDuration::of, TickDuration::ticks);
public static final Codec<TickDuration> SECONDS_CODEC = Codec.DOUBLE.xmap(l -> TickDuration.of((long) (l * 20D)), t -> t.ticks() / 20D);
public static final Codec<TickDuration> MINUTES_CODEC = Codec.DOUBLE.xmap(l -> TickDuration.of((long) (l * 1200D)), t -> t.ticks() / 1200D);
public static final Codec<TickDuration> HOURS_CODEC = Codec.DOUBLE.xmap(l -> TickDuration.of((long) (l * 72000D)), t -> t.ticks() / 72000D);

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

public static TickDuration of(long ticks) {
return ticks == 0L ? ZERO : new TickDuration(ticks);
}

public static TickDuration wrap(Object from) {
if (from instanceof Number n) {
return new TickDuration(n.longValue());
} else {
return new TickDuration(TimeJS.wrapDuration(from).toMillis() / 50L);
}
return switch (from) {
case null -> ZERO;
case TickDuration d -> d;
case Number n -> of(n.longValue());
case JsonPrimitive json -> of(json.getAsLong());
default -> of(TimeJS.wrapDuration(from).toMillis() / 50L);
};
}

@Override
Expand Down Expand Up @@ -57,4 +64,9 @@ public Temporal subtractFrom(Temporal temporal) {

return temporal;
}

@Override
public String toString() {
return ticks + " ticks";
}
}
53 changes: 17 additions & 36 deletions src/main/java/dev/latvian/mods/kubejs/util/TimeJS.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.latvian.mods.kubejs.util;

import com.google.gson.JsonPrimitive;
import com.mojang.serialization.Codec;
import dev.latvian.mods.kubejs.KubeJSCodecs;
import io.netty.buffer.ByteBuf;
Expand All @@ -25,63 +24,45 @@ static TemporalAmount wrapTemporalAmount(Object o) {
} else if (o instanceof CharSequence) {
var matcher = TEMPORAL_AMOUNT_PATTERN.matcher(o.toString());

var millis = 0L;
var nanos = 0L;
var ticks = -1L;
var millis = 0D;
var nanos = 0D;
var ticks = Double.NaN;

while (matcher.find()) {
var amount = Double.parseDouble(matcher.group(1));

switch (matcher.group(2)) {
case "t" -> {
if (ticks == -1L) {
ticks = 0L;
if (Double.isNaN(ticks)) {
ticks = 0D;
}

ticks += amount;
}

case "ns" -> nanos += (long) amount;
case "ms" -> millis += (long) amount;
case "s" -> millis = (long) (amount * 1000D);
case "m" -> millis = (long) (amount * 60000D);
case "h" -> millis = (long) (amount * 60000D) * 60L;
case "d" -> millis = (long) (amount * 24D * 86400L) * 1000L;
case "w" -> millis = (long) (amount * 24D * 86400L) * 7000L;
case "M" -> millis = (long) (amount * 31556952D / 12D) * 1000L;
case "y" -> millis = (long) (amount * 31556952D) * 1000L;
case "ns" -> nanos += amount;
case "ms" -> millis += amount;
case "s" -> millis = amount * 1000D;
case "m" -> millis = amount * 60000D;
case "h" -> millis = amount * 60000D * 60L;
case "d" -> millis = amount * 24D * 86400L * 1000L;
case "w" -> millis = amount * 24D * 86400L * 7000L;
case "M" -> millis = amount * 31556952D / 12D * 1000L;
case "y" -> millis = amount * 31556952D * 1000L;
default -> throw new IllegalArgumentException("Invalid temporal unit: " + matcher.group(2));
}
}

if (ticks != -1L) {
return new TickDuration(ticks + millis / 50L);
if (!Double.isNaN(ticks)) {
return TickDuration.of((long) (ticks + millis / 50D));
}

return Duration.ofMillis(millis).plusNanos(nanos);
return Duration.ofMillis((long) millis).plusNanos((long) nanos);
} else {
throw new IllegalArgumentException("Invalid temporal amount: " + o);
}
}

static long tickDurationOf(Object o) {
if (o instanceof Number n) {
return n.longValue();
} else if (o instanceof JsonPrimitive json) {
return json.getAsLong();
}

var t = wrapTemporalAmount(o);

if (t instanceof TickDuration(long ticks)) {
return ticks;
} else if (t instanceof Duration d) {
return d.toMillis() / 50L;
} else {
return 0L;
}
}

static Duration wrapDuration(Object o) {
var t = wrapTemporalAmount(o);

Expand Down
Loading

0 comments on commit 7163a1a

Please sign in to comment.