refactored hitbox and placeholders

This commit is contained in:
Elias Müller 2024-10-12 01:00:55 +02:00
parent ab58b291c8
commit 503c596616
6 changed files with 162 additions and 82 deletions

View File

@ -79,7 +79,7 @@ public class PixelBlockDatabase {
this.insertOrReplacePixelBlock.setDouble(5, pixelBlock.getPixelBlockLocation().getY()); this.insertOrReplacePixelBlock.setDouble(5, pixelBlock.getPixelBlockLocation().getY());
this.insertOrReplacePixelBlock.setDouble(6, pixelBlock.getPixelBlockLocation().getZ()); this.insertOrReplacePixelBlock.setDouble(6, pixelBlock.getPixelBlockLocation().getZ());
if (pixelBlock.getLastEntryLocation() != null) { if (pixelBlock.hasLastEntryLocation()) {
this.insertOrReplacePixelBlock.setString(7, pixelBlock.getLastEntryLocation().getWorld().getName()); this.insertOrReplacePixelBlock.setString(7, pixelBlock.getLastEntryLocation().getWorld().getName());
this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.getLastEntryLocation().getX()); this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.getLastEntryLocation().getX());
this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.getLastEntryLocation().getY()); this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.getLastEntryLocation().getY());

View File

@ -16,7 +16,7 @@ import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class Pixel { public class Pixel {
private static final NamespacedKey parentBlockTag = new NamespacedKey(Main.plugin, "parent"); private static final NamespacedKey pixelOfTag = new NamespacedKey(Main.plugin, "pixel_of");
private final @NotNull PixelBlock parentBlock; private final @NotNull PixelBlock parentBlock;
private final @NotNull BlockDisplay entity; private final @NotNull BlockDisplay entity;
@ -28,8 +28,8 @@ public class Pixel {
public static Pixel existingPixel(PixelBlock parentBlock, BlockDisplay entity) { public static Pixel existingPixel(PixelBlock parentBlock, BlockDisplay entity) {
PersistentDataContainer dataContainer = entity.getPersistentDataContainer(); PersistentDataContainer dataContainer = entity.getPersistentDataContainer();
if(!dataContainer.has(parentBlockTag)) throw new IllegalArgumentException("Entity is missing the parent tag in the DataContainer"); if(!dataContainer.has(pixelOfTag)) throw new IllegalArgumentException("Entity is missing the parent tag in the DataContainer");
UUID expectedParentUuid = UUID.fromString(Objects.requireNonNull(dataContainer.get(parentBlockTag, PersistentDataType.STRING))); UUID expectedParentUuid = UUID.fromString(Objects.requireNonNull(dataContainer.get(pixelOfTag, PersistentDataType.STRING)));
if(!parentBlock.getBlockUUID().equals(expectedParentUuid)) throw new IllegalArgumentException("Pixel is from a different parent block"); if(!parentBlock.getBlockUUID().equals(expectedParentUuid)) throw new IllegalArgumentException("Pixel is from a different parent block");
return new Pixel(parentBlock, entity); return new Pixel(parentBlock, entity);
} }
@ -57,7 +57,7 @@ public class Pixel {
Transformation transform = this.entity.getTransformation(); Transformation transform = this.entity.getTransformation();
transform.getScale().set(this.scale); transform.getScale().set(this.scale);
this.entity.setTransformation(transform); this.entity.setTransformation(transform);
this.entity.getPersistentDataContainer().set(parentBlockTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString()); this.entity.getPersistentDataContainer().set(pixelOfTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString());
} }
public void destroy() { public void destroy() {

View File

@ -9,8 +9,6 @@ import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Transformation;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -23,7 +21,6 @@ import java.util.stream.IntStream;
public class PixelBlock { public class PixelBlock {
private final PixelBlockWorld pixelWorld; private final PixelBlockWorld pixelWorld;
private final float hitboxOffset = (float) Main.configuration.hitboxOffset();
private final int pixelsPerBlock = Main.configuration.pixelsPerBlock(); private final int pixelsPerBlock = Main.configuration.pixelsPerBlock();
private Location pixelBlockLocation; private Location pixelBlockLocation;
@ -31,7 +28,7 @@ public class PixelBlock {
private final List<Pixel> pixels = new ArrayList<>(); private final List<Pixel> pixels = new ArrayList<>();
private PixelBlockHitbox hitbox; private PixelBlockHitbox hitbox;
private final List<ItemDisplay> placeholderIcon = new ArrayList<>(); private PixelBlockPlaceholder placeholder;
private Location lastEntryLocation; private Location lastEntryLocation;
private final UUID ownerUUID; private final UUID ownerUUID;
@ -88,6 +85,11 @@ public class PixelBlock {
this.pixels.add(Pixel.existingPixel(this, blockDisplay)); this.pixels.add(Pixel.existingPixel(this, blockDisplay));
} catch(IllegalArgumentException ignored) {} } catch(IllegalArgumentException ignored) {}
}); });
this.placeholder = PixelBlockPlaceholder.fromExisting(this);
try {
this.hitbox = PixelBlockHitbox.fromExisting(this);
} catch(NoSuchElementException ignored) {}
} }
public <T> TaskChain<T> getBlockTaskChain() { public <T> TaskChain<T> getBlockTaskChain() {
@ -119,10 +121,6 @@ public class PixelBlock {
this.lastEntryLocation = lastEntryLocation; this.lastEntryLocation = lastEntryLocation;
} }
public void spawnInteraction(boolean fullBlock) {
this.hitbox = new PixelBlockHitbox(fullBlock, pixelBlockLocation, hitboxOffset, this.pixels, pixelsPerBlock);
}
public void updateEntities() { public void updateEntities() {
record PixelData(PixelBlock parent, Vector relativeLocation, BlockData block, double scale) { record PixelData(PixelBlock parent, Vector relativeLocation, BlockData block, double scale) {
Pixel create() { Pixel create() {
@ -170,43 +168,7 @@ public class PixelBlock {
this.getBlockTaskChain() this.getBlockTaskChain()
.sync(() -> { .sync(() -> {
if(this.pixels.size() < 5) { this.hitbox = PixelBlockHitbox.newHitbox(this);
// Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0);
// BlockData block = Material.GRAY_STAINED_GLASS.createBlockData();
// Pixel newPixel = new Pixel(relativeLocation, block, 1);
// pixels.add(newPixel);
// newPixel.place(this.pixelBlockLocation);
Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5);
for(int i = 0; i <= 90; i += 90) {
ItemDisplay verticalEntity = (ItemDisplay) this.pixelBlockLocation.getWorld().spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
verticalEntity.setRotation(i, 0);
this.placeholderIcon.add(verticalEntity);
}
ItemDisplay horizontalEntity = (ItemDisplay) this.pixelBlockLocation.getWorld().spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
horizontalEntity.setRotation(0, 90);
this.placeholderIcon.add(horizontalEntity);
this.placeholderIcon.forEach(itemDisplay -> {
itemDisplay.setItemStack(ItemStack.of(Material.END_CRYSTAL));
Transformation transform = itemDisplay.getTransformation();
transform.getScale().set(0.5);
itemDisplay.setTransformation(transform);
});
spawnInteraction(true);
} else {
spawnInteraction(false);
}
Main.plugin.getLogger().info(String.format("Placed %d entities for PixelBlock '%s'", this.pixels.size(), this.blockUUID)); Main.plugin.getLogger().info(String.format("Placed %d entities for PixelBlock '%s'", this.pixels.size(), this.blockUUID));
}) })
.execute(); .execute();
@ -225,6 +187,8 @@ public class PixelBlock {
this.facingDirection = direction; this.facingDirection = direction;
updateEntities(); updateEntities();
Main.database.savePixelBlock(this); Main.database.savePixelBlock(this);
this.placeholder = PixelBlockPlaceholder.newPlaceholder(this);
} }
public void destroy(Player destroyedBy) { public void destroy(Player destroyedBy) {
@ -267,10 +231,7 @@ public class PixelBlock {
this.hitbox = null; this.hitbox = null;
} }
if(!placeholderIcon.isEmpty()) { this.placeholder.destroy();
this.placeholderIcon.forEach(Entity::remove);
this.placeholderIcon.clear();
}
// this.pixelBlockLocation.getWorld().getEntities().stream() // this.pixelBlockLocation.getWorld().getEntities().stream()
// .filter(this::isPixelBlockComponent) // .filter(this::isPixelBlockComponent)
@ -300,18 +261,14 @@ public class PixelBlock {
return facingDirection; return facingDirection;
} }
public boolean hasLastEntryLocation() {
return this.lastEntryLocation != null;
}
public List<Pixel> getPixels() { public List<Pixel> getPixels() {
return pixels; return pixels;
} }
public PixelBlockHitbox getHitbox() {
return hitbox;
}
public List<ItemDisplay> getPlaceholderIcon() {
return placeholderIcon;
}
public Location getLastEntryLocation() { public Location getLastEntryLocation() {
return lastEntryLocation.clone(); return lastEntryLocation.clone();
} }

View File

@ -1,23 +1,57 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock; package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil; import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction; import org.bukkit.entity.Interaction;
import org.bukkit.persistence.PersistentDataType;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID;
public class PixelBlockHitbox { public class PixelBlockHitbox {
private final Interaction hitbox; private static final NamespacedKey hitboxOfTag = new NamespacedKey(Main.plugin, "hitbox_of");
public PixelBlockHitbox(boolean fullBlock, Location pixelBlockLocation, float hitboxOffset, List<Pixel> pixels, int pixelsPerBlock) { private final Interaction interaction;
if(fullBlock || true) {
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity( public static PixelBlockHitbox fromExisting(PixelBlock parentBlock) {
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5), Interaction hitBox = parentBlock.getPixelBlockLocation().getNearbyEntitiesByType(Interaction.class, 1)
.stream()
.filter(interaction -> interaction.getPersistentDataContainer().has(hitboxOfTag))
.filter(interaction -> Objects.equals(
interaction.getPersistentDataContainer().get(hitboxOfTag, PersistentDataType.STRING),
parentBlock.getBlockUUID().toString()
))
.reduce((a, b) -> {
throw new IllegalStateException(String.format("Mehrere hitboxen für PixelBlock '%s' gefunden!", parentBlock.getBlockUUID()));
})
.orElseThrow();
return new PixelBlockHitbox(hitBox);
}
public static PixelBlockHitbox newHitbox(PixelBlock parentBlock) {
return new PixelBlockHitbox(parentBlock.getPixelBlockLocation(), parentBlock.getPixels(), parentBlock.getBlockUUID());
}
private PixelBlockHitbox(Interaction interaction) {
this.interaction = interaction;
}
private PixelBlockHitbox(Location pixelBlockLocation, List<Pixel> pixels, UUID parentBlockUUID) {
float offset = (float) Main.configuration.hitboxOffset();
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
if(pixels.size() > 5 || true) {
interaction = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -offset, 0.5),
EntityType.INTERACTION EntityType.INTERACTION
); );
hitbox.setInteractionHeight(1 + 2*hitboxOffset); interaction.setInteractionHeight(1 + 2*offset);
hitbox.setInteractionWidth(1 + 2*hitboxOffset); interaction.setInteractionWidth(1 + 2*offset);
} else { } else {
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getX()); double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getX());
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getY()); double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getY());
@ -29,17 +63,17 @@ public class PixelBlockHitbox {
Location spawnLocation = pixelBlockLocation.clone().add( Location spawnLocation = pixelBlockLocation.clone().add(
((startingX+endingX+1)/2)/pixelsPerBlock, ((startingX+endingX+1)/2)/pixelsPerBlock,
(startingY/pixelsPerBlock)-hitboxOffset, (startingY/pixelsPerBlock)-offset,
((startingZ+endingZ+1)/2)/pixelsPerBlock ((startingZ+endingZ+1)/2)/pixelsPerBlock
); );
float height = (float) (endingY-startingY+1)/pixelsPerBlock + 2*hitboxOffset; float height = (float) (endingY-startingY+1)/pixelsPerBlock + 2*offset;
float width; float width;
if((endingX-startingX) > (endingZ-startingZ)) { if((endingX-startingX) > (endingZ-startingZ)) {
width = (float) (endingX-startingX+1)/pixelsPerBlock + 2*hitboxOffset; width = (float) (endingX-startingX+1)/pixelsPerBlock + 2*offset;
} else { } else {
width = (float) (endingZ-startingZ+1)/pixelsPerBlock + 2*hitboxOffset; width = (float) (endingZ-startingZ+1)/pixelsPerBlock + 2*offset;
} }
if(spawnLocation.getX()+width/2 > pixelBlockLocation.getX()+1) { if(spawnLocation.getX()+width/2 > pixelBlockLocation.getX()+1) {
@ -56,17 +90,20 @@ public class PixelBlockHitbox {
spawnLocation.add(0, 0, pixelBlockLocation.getZ()-(spawnLocation.getZ()-width/2)); spawnLocation.add(0, 0, pixelBlockLocation.getZ()-(spawnLocation.getZ()-width/2));
} }
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity( interaction = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
spawnLocation, spawnLocation,
EntityType.INTERACTION EntityType.INTERACTION
); );
hitbox.setInteractionHeight(height); interaction.setInteractionHeight(height);
hitbox.setInteractionWidth(width); interaction.setInteractionWidth(width);
} }
this.interaction.getPersistentDataContainer()
.set(hitboxOfTag, PersistentDataType.STRING, parentBlockUUID.toString());
} }
public void remove() { public void remove() {
this.hitbox.remove(); this.interaction.remove();
} }
} }

View File

@ -1,5 +0,0 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
public class PixelBlockInteraction {
}

View File

@ -0,0 +1,91 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.Main;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataHolder;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Transformation;
import java.util.*;
public class PixelBlockPlaceholder {
private static final NamespacedKey placeholderOfTag = new NamespacedKey(Main.plugin, "placeholder_of");
List<ItemDisplay> placeholders = new ArrayList<>();
public static PixelBlockPlaceholder fromExisting(PixelBlock parentBlock) {
List<ItemDisplay> placeholders = parentBlock.getPixelBlockLocation()
.getNearbyEntitiesByType(ItemDisplay.class, 1)
.stream()
.filter(itemDisplay -> itemDisplay.getPersistentDataContainer().has(placeholderOfTag))
.filter(itemDisplay -> Objects.equals(
itemDisplay.getPersistentDataContainer().get(placeholderOfTag, PersistentDataType.STRING),
parentBlock.getBlockUUID().toString()
))
.toList();
return new PixelBlockPlaceholder(placeholders);
}
public static PixelBlockPlaceholder newPlaceholder(PixelBlock parentBlock) {
return new PixelBlockPlaceholder(parentBlock.getPixelBlockLocation(), parentBlock.getBlockUUID());
}
private PixelBlockPlaceholder(List<ItemDisplay> itemDisplays) {
this.placeholders = itemDisplays;
}
private PixelBlockPlaceholder(Location pixelBlockLocation, UUID parentBlockUUID) {
World pixelBlockWorld = pixelBlockLocation.getWorld();
Location itemDisplayLocation = pixelBlockLocation.add(0.5, 0.5, 0.5);
for(int i = 0; i <= 90; i += 90) {
ItemDisplay verticalCore = (ItemDisplay) pixelBlockWorld.spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
verticalCore.setRotation(i, 0);
this.placeholders.add(verticalCore);
}
ItemDisplay horizontalCore = (ItemDisplay) pixelBlockWorld.spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
horizontalCore.setRotation(0, 90);
this.placeholders.add(horizontalCore);
this.placeholders.forEach(coreDisplay -> {
coreDisplay.setItemStack(ItemStack.of(Material.END_CRYSTAL));
Transformation transform = coreDisplay.getTransformation();
transform.getScale().set(0.5);
coreDisplay.setTransformation(transform);
});
ItemDisplay displayContainer = (ItemDisplay) pixelBlockWorld.spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
displayContainer.setItemStack(ItemStack.of(Material.WHITE_STAINED_GLASS));
this.placeholders.add(displayContainer);
this.setDataTags(parentBlockUUID);
}
private void setDataTags(UUID parentBlockUUID) {
this.placeholders.stream()
.map(PersistentDataHolder::getPersistentDataContainer)
.forEach(container -> container.set(placeholderOfTag, PersistentDataType.STRING, parentBlockUUID.toString()));
}
public void destroy() {
this.placeholders.forEach(Entity::remove);
}
}