refactored hitbox logic to external class

This commit is contained in:
Elias Müller 2024-10-05 23:13:13 +02:00
parent 32e6eb259e
commit 8d70c4b7b3
2 changed files with 74 additions and 59 deletions

View File

@ -4,7 +4,6 @@ import co.aikar.taskchain.TaskChain;
import eu.mhsl.minecraft.pixelblocks.PixelBlockItem;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.utils.Direction;
import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.*;
@ -29,7 +28,7 @@ public class PixelBlock {
public Direction facingDirection;
public List<Pixel> pixels = new ArrayList<>();
public Interaction hitbox;
public PixelBlockHitbox hitbox;
public List<ItemDisplay> placeholderIcon = new ArrayList<>();
public Location lastEntryLocation;
@ -113,59 +112,7 @@ public class PixelBlock {
}
public void spawnInteraction(boolean fullBlock) {
if(fullBlock) {
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION
);
hitbox.setInteractionHeight(1 + 2*hitboxOffset);
hitbox.setInteractionWidth(1 + 2*hitboxOffset);
} else {
double startingX = MinMaxUtil.getMinProperty(this.pixels, pixel -> pixel.relativeLocation.getX());
double startingY = MinMaxUtil.getMinProperty(this.pixels, pixel -> pixel.relativeLocation.getY());
double startingZ = MinMaxUtil.getMinProperty(this.pixels, pixel -> pixel.relativeLocation.getZ());
double endingX = MinMaxUtil.getMaxProperty(this.pixels, pixel -> pixel.relativeLocation.getX());
double endingY = MinMaxUtil.getMaxProperty(this.pixels, pixel -> pixel.relativeLocation.getY());
double endingZ = MinMaxUtil.getMaxProperty(this.pixels, pixel -> pixel.relativeLocation.getZ());
Location spawnLocation = pixelBlockLocation.clone().add(
((startingX+endingX+1)/2)/pixelsPerBlock,
(startingY/pixelsPerBlock)-hitboxOffset,
((startingZ+endingZ+1)/2)/pixelsPerBlock
);
float height = (float) (endingY-startingY+1)/pixelsPerBlock + 2*hitboxOffset;
float width;
if((endingX-startingX) > (endingZ-startingZ)) {
width = (float) (endingX-startingX+1)/pixelsPerBlock + 2*hitboxOffset;
} else {
width = (float) (endingZ-startingZ+1)/pixelsPerBlock + 2*hitboxOffset;
}
if(spawnLocation.getX()+width/2 > this.pixelBlockLocation.getX()+1) {
spawnLocation.subtract((spawnLocation.getX()+width/2)-(this.pixelBlockLocation.getX()+1), 0, 0);
}
if(spawnLocation.getX()-width/2 < this.pixelBlockLocation.getX()) {
spawnLocation.add(this.pixelBlockLocation.getX()-(spawnLocation.getX()-width/2), 0, 0);
}
if(spawnLocation.getZ()+width/2 > this.pixelBlockLocation.getZ()+1) {
spawnLocation.subtract(0, 0, (spawnLocation.getZ()+width/2)-(this.pixelBlockLocation.getZ()+1));
}
if(spawnLocation.getZ()-width/2 < this.pixelBlockLocation.getZ()) {
spawnLocation.add(0, 0, this.pixelBlockLocation.getZ()-(spawnLocation.getZ()-width/2));
}
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
spawnLocation,
EntityType.INTERACTION
);
hitbox.setInteractionHeight(height);
hitbox.setInteractionWidth(width);
}
this.hitbox = new PixelBlockHitbox(fullBlock, pixelBlockLocation, hitboxOffset, this.pixels, pixelsPerBlock);
}
public void updateEntities() {
@ -317,8 +264,4 @@ public class PixelBlock {
public @NotNull PixelBlockWorld getPixelWorld() {
return pixelWorld;
}
public int getPixelsPerBlock() {
return pixelsPerBlock;
}
}

View File

@ -0,0 +1,72 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;
import java.util.List;
public class PixelBlockHitbox {
private final Interaction hitbox;
public PixelBlockHitbox(boolean fullBlock, Location pixelBlockLocation, float hitboxOffset, List<Pixel> pixels, int pixelsPerBlock) {
if(fullBlock) {
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION
);
hitbox.setInteractionHeight(1 + 2*hitboxOffset);
hitbox.setInteractionWidth(1 + 2*hitboxOffset);
} else {
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation.getX());
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation.getY());
double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation.getZ());
double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation.getX());
double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation.getY());
double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation.getZ());
Location spawnLocation = pixelBlockLocation.clone().add(
((startingX+endingX+1)/2)/pixelsPerBlock,
(startingY/pixelsPerBlock)-hitboxOffset,
((startingZ+endingZ+1)/2)/pixelsPerBlock
);
float height = (float) (endingY-startingY+1)/pixelsPerBlock + 2*hitboxOffset;
float width;
if((endingX-startingX) > (endingZ-startingZ)) {
width = (float) (endingX-startingX+1)/pixelsPerBlock + 2*hitboxOffset;
} else {
width = (float) (endingZ-startingZ+1)/pixelsPerBlock + 2*hitboxOffset;
}
if(spawnLocation.getX()+width/2 > pixelBlockLocation.getX()+1) {
spawnLocation.subtract((spawnLocation.getX()+width/2)-(pixelBlockLocation.getX()+1), 0, 0);
}
if(spawnLocation.getX()-width/2 < pixelBlockLocation.getX()) {
spawnLocation.add(pixelBlockLocation.getX()-(spawnLocation.getX()-width/2), 0, 0);
}
if(spawnLocation.getZ()+width/2 > pixelBlockLocation.getZ()+1) {
spawnLocation.subtract(0, 0, (spawnLocation.getZ()+width/2)-(pixelBlockLocation.getZ()+1));
}
if(spawnLocation.getZ()-width/2 < pixelBlockLocation.getZ()) {
spawnLocation.add(0, 0, pixelBlockLocation.getZ()-(spawnLocation.getZ()-width/2));
}
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
spawnLocation,
EntityType.INTERACTION
);
hitbox.setInteractionHeight(height);
hitbox.setInteractionWidth(width);
}
}
public void remove() {
this.hitbox.remove();
}
}