Skip to content

Commit

Permalink
Implement BlockDisplay and ItemDisplay (#959)
Browse files Browse the repository at this point in the history
* Implement DisplayMock with tests

* Implement ItemDisplay and BlockDisplay

* Fix test failures

More context: floating point precision issues

* Spawn BlockDisplay + ItemDisplay

* Fix merge issues
  • Loading branch information
Thorinwasher committed Feb 22, 2024
1 parent a5920be commit b9b7e32
Show file tree
Hide file tree
Showing 8 changed files with 686 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/be/seeseemelk/mockbukkit/WorldMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import be.seeseemelk.mockbukkit.entity.BatMock;
import be.seeseemelk.mockbukkit.entity.BeeMock;
import be.seeseemelk.mockbukkit.entity.BlazeMock;
import be.seeseemelk.mockbukkit.entity.BlockDisplayMock;
import be.seeseemelk.mockbukkit.entity.BoatMock;
import be.seeseemelk.mockbukkit.entity.CamelMock;
import be.seeseemelk.mockbukkit.entity.CatMock;
Expand Down Expand Up @@ -42,6 +43,7 @@
import be.seeseemelk.mockbukkit.entity.GuardianMock;
import be.seeseemelk.mockbukkit.entity.HopperMinecartMock;
import be.seeseemelk.mockbukkit.entity.HorseMock;
import be.seeseemelk.mockbukkit.entity.ItemDisplayMock;
import be.seeseemelk.mockbukkit.entity.ItemEntityMock;
import be.seeseemelk.mockbukkit.entity.LargeFireballMock;
import be.seeseemelk.mockbukkit.entity.LeashHitchMock;
Expand Down Expand Up @@ -140,6 +142,7 @@
import org.bukkit.entity.Bat;
import org.bukkit.entity.Bee;
import org.bukkit.entity.Blaze;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Camel;
import org.bukkit.entity.Cat;
Expand Down Expand Up @@ -174,6 +177,7 @@
import org.bukkit.entity.Hanging;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Item;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LargeFireball;
import org.bukkit.entity.LeashHitch;
Expand Down Expand Up @@ -1419,6 +1423,14 @@ else if (clazz == PigZombie.class)
{
return new PigZombieMock(server, UUID.randomUUID());
}
else if (clazz == BlockDisplay.class)
{
return new BlockDisplayMock(server, UUID.randomUUID());
}
else if (clazz == ItemDisplay.class)
{
return new ItemDisplayMock(server, UUID.randomUUID());
}
else if (clazz == Arrow.class)
{
return new ArrowMock(server, UUID.randomUUID());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package be.seeseemelk.mockbukkit.entity;

import be.seeseemelk.mockbukkit.ServerMock;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class BlockDisplayMock extends DisplayMock implements BlockDisplay
{

private BlockData blockData = Material.AIR.createBlockData();

/**
* Constructs a new EntityMock on the provided {@link ServerMock} with a specified {@link UUID}.
*
* @param server The server to create the entity on.
* @param uuid The UUID of the entity.
*/
public BlockDisplayMock(@NotNull ServerMock server, @NotNull UUID uuid)
{
super(server, uuid);
}

@Override
public @NotNull BlockData getBlock()
{
return blockData.clone();
}

@Override
public void setBlock(@NotNull BlockData block)
{
this.blockData = block.clone();
}

@Override
public EntityType getType()
{
return EntityType.BLOCK_DISPLAY;
}

}
232 changes: 232 additions & 0 deletions src/main/java/be/seeseemelk/mockbukkit/entity/DisplayMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package be.seeseemelk.mockbukkit.entity;

import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.UnimplementedOperationException;
import com.google.common.base.Preconditions;
import org.bukkit.Color;
import org.bukkit.entity.Display;
import org.bukkit.util.Transformation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;

import java.util.UUID;

public class DisplayMock extends EntityMock implements Display
{

private Matrix4f transformationMatrix;
private int interpolationDuration = 0;
private int teleportDuration = 0;
private float viewRange = 1.0f;
private float shadowRadius = 0.0f;
private float shadowStrength = 1.0f;
private float width = 0.0f;
private float height = 0.0f;
private int interpolationDelay = 0;
private int color = -1;
private Brightness brightness = null;


/**
* Constructs a new EntityMock on the provided {@link ServerMock} with a specified {@link UUID}.
*
* @param server The server to create the entity on.
* @param uuid The UUID of the entity.
*/
protected DisplayMock(@NotNull ServerMock server, @NotNull UUID uuid)
{
super(server, uuid);
this.transformationMatrix = new Matrix4f();
this.transformationMatrix.scale(new Vector3f(1.0f, 1.0f, 1.0f));
}

@Override
public @NotNull Transformation getTransformation()
{
float f = 1.0F / this.transformationMatrix.m33();
Vector3f translation = this.transformationMatrix.getTranslation(new Vector3f()).mul(f);
Quaternionf leftRotation = this.transformationMatrix.getUnnormalizedRotation(new Quaternionf());
Vector3f scale = transformationMatrix.getScale(new Vector3f());
Quaternionf rightRotation = leftRotation.conjugate();
return new Transformation(translation, leftRotation, scale, rightRotation);
}

@Override
public void setTransformation(@NotNull Transformation transformation)
{
Preconditions.checkArgument(transformation != null, "Transformation cannot be null");
Matrix4f matrix4f = new Matrix4f();
matrix4f.translation(transformation.getTranslation());
matrix4f.rotate(transformation.getLeftRotation());
matrix4f.scale(transformation.getScale());
matrix4f.rotate(transformation.getRightRotation());
this.transformationMatrix = matrix4f;
}

@Override
public void setTransformationMatrix(@NotNull Matrix4f transformationMatrix)
{
Preconditions.checkArgument(transformationMatrix != null, "Transformation matrix cannot be null");
try
{
this.transformationMatrix = (Matrix4f) transformationMatrix.clone();
}
catch (CloneNotSupportedException e)
{
throw new IllegalStateException("Could not clone transformation matrix");
}
}

@Override
public int getInterpolationDuration()
{
return this.interpolationDuration;
}

@Override
public void setInterpolationDuration(int duration)
{
this.interpolationDuration = duration;
}

@Override
public int getTeleportDuration()
{
return this.teleportDuration;
}

@Override
public void setTeleportDuration(int duration)
{
Preconditions.checkArgument(duration >= 0 && duration <= 59, "duration (%s) cannot be lower than 0 or higher than 59", duration);
this.teleportDuration = duration;
}

@Override
public float getViewRange()
{
return this.viewRange;
}

@Override
public void setViewRange(float range)
{
this.viewRange = range;
}

@Override
public float getShadowRadius()
{
return this.shadowRadius;
}

@Override
public void setShadowRadius(float radius)
{
this.shadowRadius = radius;
}

@Override
public float getShadowStrength()
{
return this.shadowStrength;
}

@Override
public void setShadowStrength(float strength)
{
this.shadowStrength = strength;
}

@Override
public float getDisplayWidth()
{
return this.width;
}

@Override
public void setDisplayWidth(float width)
{
this.width = width;
}

@Override
public float getDisplayHeight()
{
return this.height;
}

@Override
public void setDisplayHeight(float height)
{
this.height = height;
}

@Override
public int getInterpolationDelay()
{
return this.interpolationDelay;
}

@Override
public void setInterpolationDelay(int ticks)
{
this.interpolationDelay = ticks;
}

@Override
public @NotNull Billboard getBillboard()
{
throw new UnimplementedOperationException();
}

@Override
public void setBillboard(@NotNull Billboard billboard)
{
throw new UnimplementedOperationException();
}

@Override
public @Nullable Color getGlowColorOverride()
{
return (this.color == -1) ? null : Color.fromARGB(color);
}

@Override
public void setGlowColorOverride(@Nullable Color color)
{
if (color == null)
{
this.color = -1;
}
else
{
this.color = color.asARGB();
}
}

@Override
public @Nullable Brightness getBrightness()
{
return cloneBrightness(this.brightness);
}

@Override
public void setBrightness(@Nullable Brightness brightness)
{
this.brightness = cloneBrightness(brightness);
}

private @Nullable Brightness cloneBrightness(@Nullable Brightness inputBrightness)
{
if (inputBrightness == null)
{
return null;
}
return new Brightness(inputBrightness.getBlockLight(), inputBrightness.getSkyLight());
}

}
62 changes: 62 additions & 0 deletions src/main/java/be/seeseemelk/mockbukkit/entity/ItemDisplayMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package be.seeseemelk.mockbukkit.entity;

import be.seeseemelk.mockbukkit.ServerMock;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public class ItemDisplayMock extends DisplayMock implements ItemDisplay
{

private ItemStack itemStack = ItemStack.empty();
private ItemDisplayTransform itemDisplayTransform = ItemDisplayTransform.NONE;

/**
* Constructs a new EntityMock on the provided {@link ServerMock} with a specified {@link UUID}.
*
* @param server The server to create the entity on.
* @param uuid The UUID of the entity.
*/
public ItemDisplayMock(@NotNull ServerMock server, @NotNull UUID uuid)
{
super(server, uuid);
}

@Override
public @Nullable ItemStack getItemStack()
{
return itemStack.clone();
}

@Override
public void setItemStack(@Nullable ItemStack item)
{
if(item == null){
this.itemStack = ItemStack.empty();
} else
{
this.itemStack = item.clone();
}
}

@Override
public @NotNull ItemDisplayTransform getItemDisplayTransform()
{
return this.itemDisplayTransform;
}

@Override
public void setItemDisplayTransform(@NotNull ItemDisplayTransform display)
{
this.itemDisplayTransform = display;
}

@Override
public EntityType getType(){
return EntityType.ITEM_DISPLAY;
}
}

0 comments on commit b9b7e32

Please sign in to comment.