Skip to content

Commit

Permalink
Part I of Version 7.2: Moved around some level and entity mixins, cha…
Browse files Browse the repository at this point in the history
…nged BlockContainerJS class to LevelBlock interface
  • Loading branch information
LatvianModder committed Nov 14, 2024
1 parent 661243d commit 63f4fd4
Show file tree
Hide file tree
Showing 61 changed files with 1,021 additions and 766 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ modrinth_id=umyGl7zF

minecraft_version=1.21.1
supported_versions=1.21.1, 1.21
mod_version=2101.7.1
mod_version=2101.7.2

neoforge_version=21.1.42
parchment_mc_version=1.21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {

registry.register(BlockPos.class, KubeJSTypeWrappers::blockPosOf);
registry.register(Vec3.class, KubeJSTypeWrappers::vec3Of);
registry.register(Vec3i.class, KubeJSTypeWrappers::blockPosOf);

registry.register(Item.class, ItemStackJS::getRawItem);
registry.register(ItemLike.class, ItemStackJS::getRawItem);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.MapCodec;
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.type.EnumTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
import io.netty.buffer.ByteBuf;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.Utf8String;
import net.minecraft.network.codec.ByteBufCodecs;
Expand All @@ -22,6 +24,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.StringRepresentable;
import net.minecraft.util.valueproviders.IntProvider;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.storage.loot.providers.number.NumberProvider;
import net.minecraft.world.level.storage.loot.providers.number.NumberProviders;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -89,6 +92,8 @@ public void encode(ByteBuf buffer, @Nullable JsonElement value) {

Codec<ResourceKey<? extends Registry<?>>> REGISTRY_KEY = ResourceLocation.CODEC.xmap(ResourceKey::createRegistryKey, ResourceKey::location);

MapCodec<EntityType<?>> ENTITY_TYPE_FIELD_CODEC = BuiltInRegistries.ENTITY_TYPE.byNameCodec().fieldOf("id");

static <E> Codec<E> stringResolverCodec(Function<E, String> toStringFunction, Function<String, E> fromStringFunction) {
return Codec.STRING.flatXmap(str -> Optional.ofNullable(fromStringFunction.apply(str))
.map(DataResult::success)
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.util.NBTUtils;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.UtilsJS;
Expand Down Expand Up @@ -133,8 +133,8 @@ static Vec3 vec3Of(@Nullable Object o) {
return new Vec3(UtilsJS.parseDouble(list.get(0), 0), UtilsJS.parseDouble(list.get(1), 0), UtilsJS.parseDouble(list.get(2), 0));
} else if (o instanceof BlockPos pos) {
return new Vec3(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D);
} else if (o instanceof BlockContainerJS block) {
return new Vec3(block.getPos().getX() + 0.5D, block.getPos().getY() + 0.5D, block.getPos().getZ() + 0.5D);
} else if (o instanceof LevelBlock block) {
return new Vec3(block.getCenterX(), block.getCenterY(), block.getCenterZ());
}

return Vec3.ZERO;
Expand All @@ -145,7 +145,7 @@ static BlockPos blockPosOf(@Nullable Object o) {
return pos;
} else if (o instanceof List<?> list && list.size() >= 3) {
return new BlockPos(UtilsJS.parseInt(list.get(0), 0), UtilsJS.parseInt(list.get(1), 0), UtilsJS.parseInt(list.get(2), 0));
} else if (o instanceof BlockContainerJS block) {
} else if (o instanceof LevelBlock block) {
return block.getPos();
} else if (o instanceof Vec3 vec) {
return BlockPos.containing(vec.x, vec.y, vec.z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import com.google.gson.JsonObject;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.serialization.MapCodec;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.JsonUtils;
import net.minecraft.Util;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
Expand All @@ -30,8 +28,6 @@

@Info("Various item related helper methods")
public interface ItemWrapper {
MapCodec<EntityType<?>> ENTITY_TYPE_FIELD_CODEC = BuiltInRegistries.ENTITY_TYPE.byNameCodec().fieldOf("id");

@Info("Returns an ItemStack of the input")
static ItemStack of(ItemStack in) {
return in;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.player.KubePlayerEvent;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.world.entity.player.Player;
Expand All @@ -24,9 +24,7 @@ public Player getEntity() {
}

@Info("The block that was broken.")
public BlockContainerJS getBlock() {
var block = new BlockContainerJS((Level) event.getLevel(), event.getPos());
block.cachedState = event.getState();
return block;
public LevelBlock getBlock() {
return ((Level) event.getLevel()).kjs$getBlock(event.getPos()).cache(event.getState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.latvian.mods.kubejs.entity.KubeEntityEvent;
import dev.latvian.mods.kubejs.item.ItemPredicate;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
Expand Down Expand Up @@ -37,11 +37,8 @@ public Entity getEntity() {
}

@Info("The block that was broken.")
public BlockContainerJS getBlock() {
var block = new BlockContainerJS(event.getLevel(), event.getPos());
block.cachedState = event.getState();
block.cachedEntity = event.getBlockEntity();
return block;
public LevelBlock getBlock() {
return event.getLevel().kjs$getBlock(event.getPos()).cache(event.getState()).cache(event.getBlockEntity());
}

@Info("The experience dropped by the block.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.player.KubePlayerEvent;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.core.Direction;
Expand All @@ -26,8 +26,8 @@ public Player getEntity() {
}

@Info("The block that was left clicked.")
public BlockContainerJS getBlock() {
return new BlockContainerJS(event.getLevel(), event.getPos());
public LevelBlock getBlock() {
return event.getLevel().kjs$getBlock(event.getPos());
}

@Info("The item that was used to left click the block.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.entity.KubeEntityEvent;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -30,9 +30,7 @@ public Entity getEntity() {
}

@Info("The block that is placed.")
public BlockContainerJS getBlock() {
var block = new BlockContainerJS((Level) event.getLevel(), event.getPos());
block.cachedState = event.getPlacedBlock();
return block;
public LevelBlock getBlock() {
return ((Level) event.getLevel()).kjs$getBlock(event.getPos()).cache(event.getPlacedBlock());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.player.KubePlayerEvent;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.core.BlockPos;
Expand All @@ -21,7 +21,7 @@ public class BlockRightClickedKubeEvent implements KubePlayerEvent {
private final BlockPos pos;
private final Direction direction;
private final BlockHitResult hitResult;
private BlockContainerJS block;
private LevelBlock block;

public BlockRightClickedKubeEvent(ItemStack item, Player player, InteractionHand hand, BlockPos pos, Direction direction, @Nullable BlockHitResult hitResult) {
this.item = item;
Expand All @@ -39,9 +39,9 @@ public Player getEntity() {
}

@Info("The block that was right clicked.")
public BlockContainerJS getBlock() {
public LevelBlock getBlock() {
if (block == null) {
block = new BlockContainerJS(player.level(), pos);
block = player.level().kjs$getBlock(pos);
}

return block;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.entity.KubeEntityEvent;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
Expand All @@ -13,12 +13,11 @@
Invoked when a falling block starts to fall.
""")
public class BlockStartedFallingKubeEvent implements KubeEntityEvent {
private final BlockContainerJS block;
private final LevelBlock block;
private final FallingBlockEntity entity;

public BlockStartedFallingKubeEvent(Level level, BlockPos pos, BlockState state, FallingBlockEntity entity) {
this.block = new BlockContainerJS(level, pos);
this.block.cachedState = state;
this.block = level.kjs$getBlock(pos).cache(state);
this.entity = entity;
}

Expand All @@ -32,7 +31,7 @@ public Entity getEntity() {
return entity;
}

public BlockContainerJS getBlock() {
public LevelBlock getBlock() {
return block;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.entity.KubeEntityEvent;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
Expand All @@ -13,18 +13,16 @@
Invoked when a falling block finishes falling.
""")
public class BlockStoppedFallingKubeEvent implements KubeEntityEvent {
public final BlockContainerJS block;
public final LevelBlock block;
private final FallingBlockEntity entity;
public final double fallSpeed;
public final BlockContainerJS replacedBlock;
public final LevelBlock replacedBlock;

public BlockStoppedFallingKubeEvent(Level level, BlockPos pos, BlockState state, FallingBlockEntity entity, double fallSpeed, BlockState replacedState) {
this.block = new BlockContainerJS(level, pos);
this.block.cachedState = state;
this.block = level.kjs$getBlock(pos).cache(state);
this.entity = entity;
this.fallSpeed = fallSpeed;
this.replacedBlock = new BlockContainerJS(level, pos);
this.replacedBlock.cachedState = replacedState;
this.replacedBlock = level.kjs$getBlock(pos).cache(replacedState);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.KubeLevelEvent;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
Expand All @@ -14,16 +14,14 @@
public class DetectorBlockKubeEvent implements KubeLevelEvent {
private final String detectorId;
private final Level level;
private final BlockPos pos;
private final boolean powered;
private final BlockContainerJS block;
private final LevelBlock block;

public DetectorBlockKubeEvent(String i, Level l, BlockPos p, boolean pow) {
detectorId = i;
level = l;
pos = p;
powered = pow;
block = new BlockContainerJS(level, pos);
block = level.kjs$getBlock(p);
}

@Info("The id of the detector block when it was registered.")
Expand All @@ -43,7 +41,7 @@ public boolean isPowered() {
}

@Info("The detector block.")
public BlockContainerJS getBlock() {
public LevelBlock getBlock() {
return block;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.entity.KubeEntityEvent;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
Expand All @@ -12,12 +12,11 @@
""")
public class FarmlandTrampledKubeEvent implements KubeEntityEvent {
private final BlockEvent.FarmlandTrampleEvent event;
private final BlockContainerJS block;
private final LevelBlock block;

public FarmlandTrampledKubeEvent(BlockEvent.FarmlandTrampleEvent event) {
this.event = event;
this.block = new BlockContainerJS((Level) event.getLevel(), event.getPos());
this.block.cachedState = event.getState();
this.block = ((Level) event.getLevel()).kjs$getBlock(event.getPos()).cache(event.getState());
}

@Info("The distance of the entity from the block.")
Expand All @@ -38,7 +37,7 @@ public Level getLevel() {
}

@Info("The farmland block.")
public BlockContainerJS getBlock() {
public LevelBlock getBlock() {
return block;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelBlock;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;

public class RandomTickCallbackJS {
public BlockContainerJS block;
public RandomSource random;
public final LevelBlock block;
public final RandomSource random;

public RandomTickCallbackJS(BlockContainerJS containerJS, RandomSource random) {
this.block = containerJS;
public RandomTickCallbackJS(LevelBlock block, RandomSource random) {
this.block = block;
this.random = random;
}

Expand Down
Loading

0 comments on commit 63f4fd4

Please sign in to comment.