Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Small tweaks #284

Open
wants to merge 11 commits into
base: minecraft-1.12
Choose a base branch
from
Prev Previous commit
Next Next commit
feat: Entities can now be created at canvas3d
MrNaru300 committed Aug 11, 2021
commit acfe4deb35d0d31d7af5e3fe6ff6d85aebea5e9a
Original file line number Diff line number Diff line change
@@ -2,10 +2,14 @@

import dan200.computercraft.api.lua.LuaException;
import net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import org.squiddev.plethora.api.Injects;
import org.squiddev.plethora.utils.NBTUtilsRecursive;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -93,6 +97,14 @@ public String name() {
}
};

public static final ArgumentType<NBTTagCompound> NBT_TAG_COMPOUND_ARG = TABLE.map(NBTUtilsRecursive::encodeNBTTagCompound);

public static final ArgumentType<EntityEntry> ENTITY_ARG = RESOURCE.map(name -> {
EntityEntry entityEntry = ForgeRegistries.ENTITIES.getValue(name);
if (entityEntry == null || !ForgeRegistries.ENTITIES.containsKey(name)) throw new LuaException("Unknown entity '" + name + "'");
return entityEntry;
});

private ArgumentTypes() {
}
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import dan200.computercraft.api.lua.LuaException;
import net.minecraft.item.Item;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fml.common.registry.EntityEntry;
import org.squiddev.plethora.api.IWorldLocation;
import org.squiddev.plethora.api.method.ContextKeys;
import org.squiddev.plethora.api.method.IContext;
@@ -89,7 +90,7 @@ public static TypedLuaObject<Box> addBox(IContext<Group3D> baked, @FromContext C
public static TypedLuaObject<Line3D> addLine(
IContext<Group3D> baked, @FromContext CanvasServer canvas,
Vec3d start, Vec3d end,
@Optional(defDoub = 1.0f) float thickness, @Optional(defInt = DEFAULT_COLOUR) int colour
@Optional(defDoub = 1.0f) float thickness, @Optional() int colour
) {
Group3D group = baked.getTarget();

@@ -104,6 +105,22 @@ public static TypedLuaObject<Line3D> addLine(
return baked.makeChild(line, canvas.reference(line)).getObject();
}

@PlethoraMethod(doc = "function(position: table, entity: string, scale: number): table-- Create a entity model.", worldThread = false)
public static TypedLuaObject<Entity3D> addEntity(IContext<Group3D> baked, @FromContext CanvasServer canvas,
Vec3d position, EntityEntry entityEntry,
@Optional(defDoub = 1) float scale) {
Group3D group = baked.getTarget();

Entity3D model = new Entity3D(canvas.newObjectId(), group.id());
model.setEntityEntry(entityEntry);
model.setPosition(position);
model.setScale(scale);

canvas.add(model);

return baked.makeChild(model, canvas.reference(model)).getObject();
}

@PlethoraMethod(doc = "-- Create a item model.", worldThread = false)
public static TypedLuaObject<Item3D> addItem(
IContext<ObjectGroup.Group3D> baked, @FromContext CanvasServer canvas,
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.squiddev.plethora.gameplay.modules.glasses.objects;

import net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagCompound;
import org.squiddev.plethora.api.method.MethodResult;
import org.squiddev.plethora.api.method.wrapper.FromTarget;
import org.squiddev.plethora.api.method.wrapper.Optional;
Original file line number Diff line number Diff line change
@@ -22,8 +22,9 @@ public final class ObjectRegistry {
public static final byte BOX_3D = 11;
public static final byte ITEM_3D = 12;
public static final byte LINE_3D = 13;
public static final byte ENTITY = 14;

private static final BaseObject.Factory[] FACTORIES = new BaseObject.Factory[]{
private static final BaseObject.Factory[] FACTORIES = new BaseObject.Factory[]{
Rectangle::new,
Line::new,
Dot::new,
@@ -38,7 +39,8 @@ public final class ObjectRegistry {
ObjectFrame::new,
Box::new,
Item3D::new,
Line3D::new
Line3D::new,
Entity3D::new
};

private ObjectRegistry() {
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package org.squiddev.plethora.gameplay.modules.glasses.objects.object3d;

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.squiddev.plethora.gameplay.modules.glasses.BaseObject;
import org.squiddev.plethora.gameplay.modules.glasses.CanvasClient;
import org.squiddev.plethora.gameplay.modules.glasses.objects.ObjectRegistry;
import org.squiddev.plethora.gameplay.modules.glasses.objects.Scalable;
import org.squiddev.plethora.utils.ByteBufUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;

public class Entity3D extends BaseObject implements EntityObject, Scalable, Positionable3D, DepthTestable, Rotatable3D {
private float scale;
private Vec3d position = Vec3d.ZERO;
private Vec3d rotation = Vec3d.ZERO;
private boolean depthTest = true;


private EntityEntry entityEntry;
private Entity entity;


public Entity3D(int id, int parent) {super(id, parent, ObjectRegistry.ENTITY ); }

@Override
public float getScale() {
return scale;
}

@Override
public void setScale(float scale) {
if (this.scale != scale) {
this.scale = scale;
setDirty();
}
}

@Nonnull
@Override
public Vec3d getPosition() {
return position;
}

@Override
public void setPosition(@Nonnull Vec3d position) {
if (!this.position.equals(position)) {
this.position = position;
setDirty();
}
}

@Override
public boolean hasDepthTest() {
return depthTest;
}

@Override
public void setDepthTest(boolean depthTest) {
if (this.depthTest != depthTest) {
this.depthTest = depthTest;
setDirty();
}
}

@Nonnull
public EntityEntry getEntityEntry() { return entityEntry; }

public void setEntityEntry(@Nonnull EntityEntry entityEntry) {
if (this.entityEntry == null || this.entityEntry != entityEntry) {
this.entityEntry = entityEntry;
entity = null;
setDirty();
}
}

@Nullable
@Override
public Vec3d getRotation() {
return rotation;
}

@Override
public void setRotation(@Nullable Vec3d rotation) {
if (!Objects.equals(this.rotation, rotation)) {
this.rotation = rotation;
setDirty();
}
}

@Override
public void writeInitial(ByteBuf buf) {
ByteBufUtils.writeVec3d(buf, position);
ByteBufUtils.writeOptVec3d(buf, rotation);
buf.writeFloat(scale);
buf.writeBoolean(depthTest);

ByteBufUtils.writeUTF8String(buf, entityEntry.getRegistryName().toString());
}

@Override
public void readInitial(ByteBuf buf) {
position = ByteBufUtils.readVec3d(buf);
rotation = ByteBufUtils.readOptVec3d(buf);
scale = buf.readFloat();
depthTest = buf.readBoolean();

String resourceName = ByteBufUtils.readUTF8String(buf);
ResourceLocation name = new ResourceLocation(resourceName);
entityEntry = ForgeRegistries.ENTITIES.getValue(name);
}

@Override
@SideOnly(Side.CLIENT)
public void draw(CanvasClient canvas) {

Minecraft mc = Minecraft.getMinecraft();

if (entity == null) {
entity = entityEntry.newInstance(mc.world);
}

GlStateManager.pushMatrix();

GlStateManager.translate(position.x, position.y, position.z);
GlStateManager.scale(scale, scale, scale);
if (rotation == null) {
RenderManager renderManager = mc.getRenderManager();

GlStateManager.rotate(180 - renderManager.playerViewY, 0, 1, 0);
GlStateManager.rotate(-renderManager.playerViewX, 1, 0, 0);
} else {
GlStateManager.rotate((float) rotation.x, 1, 0, 0);
GlStateManager.rotate((float) rotation.y, 0, 1, 0);
GlStateManager.rotate((float) rotation.z, 0, 0, 1);
}

GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
GlStateManager.enableTexture2D();
GlStateManager.enableRescaleNormal();

if (depthTest) {
GlStateManager.enableDepth();
} else {
GlStateManager.disableDepth();
}

mc.getRenderManager().renderEntity(entity, position.x, position.y, position.z, 0, 0, true);

GlStateManager.popMatrix();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.squiddev.plethora.gameplay.modules.glasses.objects.object3d;


import net.minecraftforge.fml.common.registry.EntityEntry;
import org.squiddev.plethora.api.method.MethodResult;
import org.squiddev.plethora.api.method.wrapper.FromTarget;
import org.squiddev.plethora.api.method.wrapper.PlethoraMethod;

import javax.annotation.Nonnull;

/**
* An object which contains an entity.
*/
public interface EntityObject {

@Nonnull
EntityEntry getEntityEntry();

void setEntityEntry(@Nonnull EntityEntry entityEntry);

@PlethoraMethod(doc = "function(): string -- Get the entity name for this object.", worldThread = false)
static MethodResult getEntity(@FromTarget Entity3D object) {
return MethodResult.result(object.getEntityEntry().getRegistryName().toString());
}

@PlethoraMethod(doc = "function(string name): -- Set the entity value for this object.", worldThread = false)
static void setEntity(@FromTarget Entity3D object, EntityEntry entityEntry) {
object.setEntityEntry(entityEntry);
}
}
63 changes: 63 additions & 0 deletions src/main/java/org/squiddev/plethora/utils/NBTUtilsRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.squiddev.plethora.utils;

import dan200.computercraft.api.lua.LuaException;
import net.minecraft.nbt.*;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.function.Predicate;

import static dan200.computercraft.api.lua.ArgumentHelper.getTable;

public final class NBTUtilsRecursive {

@Nonnull
public static NBTTagCompound encodeNBTTagCompound(Map<?,?> map) throws LuaException {
NBTTagCompound nbt = new NBTTagCompound();

for (Object key : map.keySet())
nbt.setTag(key.toString(), encodeObject(map.get(key)));

return nbt;
}

@Nonnull
public static NBTTagList encodeNBTTagList(Map<?,?> map) throws LuaException {
NBTTagList nbt = new NBTTagList();

for (Object key : map.keySet())
nbt.appendTag(encodeObject(map.get(key)));
return nbt;
}

@Nonnull
public static NBTBase encodeObject(Object object) throws LuaException {
switch (object.getClass().getSimpleName()) {
case "String":
return new NBTTagString((String) object);
case "Double":
return new NBTTagDouble((Double) object);
case "Boolean":
return new NBTTagByte((byte) ((Boolean) object ? 1 : 0));
case "HashMap":
if (
((Map<?,?>) object)
.keySet().stream().allMatch(
(Predicate<Object>) o -> o instanceof Double // Verify if it is an NBTTagList
)
) {
return encodeNBTTagList((Map<?, ?>) object);
} else {
return encodeNBTTagCompound((Map<?, ?>) object);
}
default:
throw new LuaException("Unknown type " + object.getClass().getSimpleName());
}
}

@Nonnull
public static @NotNull NBTTagCompound encodeObjects(Object[] objects, int index) throws LuaException {
return encodeNBTTagCompound(getTable(objects, index));
}
}