removed offset, fixed hitbox

This commit is contained in:
Lars Neuhaus 2024-10-19 15:24:05 +02:00
parent 744f1dd6e2
commit 8a6577afa0
5 changed files with 48 additions and 59 deletions

View File

@ -28,10 +28,6 @@ public final class Main extends JavaPlugin {
public final static List<PixelBlock> pixelBlocks = new ArrayList<>(); public final static List<PixelBlock> pixelBlocks = new ArrayList<>();
public static <T> TaskChain<T> chain() {
return taskFactory.newChain();
}
public static <T> TaskChain<T> sharedChain(String name) { public static <T> TaskChain<T> sharedChain(String name) {
return taskFactory.newSharedChain(name); return taskFactory.newSharedChain(name);
} }
@ -46,7 +42,6 @@ public final class Main extends JavaPlugin {
Main.configuration = new PixelBlockConfiguration( Main.configuration = new PixelBlockConfiguration(
config.getInt(PixelBlockConfiguration.Keys.PixelsPerBlock.getKey()), config.getInt(PixelBlockConfiguration.Keys.PixelsPerBlock.getKey()),
config.getDouble(PixelBlockConfiguration.Keys.HitboxOffset.getKey()),
config.getBoolean(PixelBlockConfiguration.Keys.OnlyBreakableByOwners.getKey()), config.getBoolean(PixelBlockConfiguration.Keys.OnlyBreakableByOwners.getKey()),
config.getBoolean(PixelBlockConfiguration.Keys.OnlyEditableByOwners.getKey()) config.getBoolean(PixelBlockConfiguration.Keys.OnlyEditableByOwners.getKey())
); );

View File

@ -5,13 +5,11 @@ import org.jetbrains.annotations.NotNull;
public record PixelBlockConfiguration( public record PixelBlockConfiguration(
int pixelsPerBlock, int pixelsPerBlock,
double hitboxOffset,
boolean onlyBreakableByOwner, boolean onlyBreakableByOwner,
boolean onlyEditableByOwner boolean onlyEditableByOwner
) { ) {
public static void setDefaults(FileConfiguration config) { public static void setDefaults(FileConfiguration config) {
config.addDefault(Keys.PixelsPerBlock.key, 16); config.addDefault(Keys.PixelsPerBlock.key, 16);
config.addDefault(Keys.HitboxOffset.key, 0.005);
config.addDefault(Keys.OnlyBreakableByOwners.key, false); config.addDefault(Keys.OnlyBreakableByOwners.key, false);
config.addDefault(Keys.OnlyEditableByOwners.key, true); config.addDefault(Keys.OnlyEditableByOwners.key, true);
config.options().copyDefaults(true); config.options().copyDefaults(true);
@ -19,7 +17,6 @@ public record PixelBlockConfiguration(
public enum Keys { public enum Keys {
PixelsPerBlock("pixelsPerBlock"), PixelsPerBlock("pixelsPerBlock"),
HitboxOffset("hitboxOffset"),
OnlyBreakableByOwners("onlyBreakableByOwners"), OnlyBreakableByOwners("onlyBreakableByOwners"),
OnlyEditableByOwners("onlyEditableByOwners"); OnlyEditableByOwners("onlyEditableByOwners");

View File

@ -1,6 +1,5 @@
package eu.mhsl.minecraft.pixelblocks.listeners; package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import org.bukkit.Location; import org.bukkit.Location;
@ -13,7 +12,7 @@ public class BreakPixelBlockListener implements Listener {
public void destroyPixelBlock(PrePlayerAttackEntityEvent event) { public void destroyPixelBlock(PrePlayerAttackEntityEvent event) {
if(!(event.getAttacked() instanceof Interaction)) return; if(!(event.getAttacked() instanceof Interaction)) return;
Location blockLocation = event.getAttacked().getLocation().add(0, Main.configuration().hitboxOffset(), 0).toBlockLocation(); Location blockLocation = event.getAttacked().getLocation().toBlockLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation); PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation);
if(pixelBlock == null) return; if(pixelBlock == null) return;
pixelBlock.destroy(event.getPlayer()); pixelBlock.destroy(event.getPlayer());

View File

@ -1,6 +1,5 @@
package eu.mhsl.minecraft.pixelblocks.listeners; package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Interaction; import org.bukkit.entity.Interaction;
@ -16,7 +15,6 @@ public class EnterPixelBlockListener implements Listener {
Location interactionLocation = event Location interactionLocation = event
.getRightClicked() .getRightClicked()
.getLocation() .getLocation()
.add(0, Main.configuration().hitboxOffset(), 0)
.toBlockLocation(); .toBlockLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(interactionLocation); PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(interactionLocation);
if(pixelBlock == null) return; if(pixelBlock == null) return;

View File

@ -1,6 +1,7 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock; package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.Main; import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -23,62 +24,61 @@ public class PixelBlockHitbox {
public void spawn() { public void spawn() {
Location absoluteLocation = this.parentBlock.getPixelBlockLocation(); Location absoluteLocation = this.parentBlock.getPixelBlockLocation();
List<PixelBlockWorld.PixelData> pixels = this.parentBlock.getPixelData(); List<PixelBlockWorld.PixelData> pixels = this.parentBlock.getPixelData();
float offset = (float) Main.configuration().hitboxOffset();
int pixelsPerBlock = Main.configuration().pixelsPerBlock(); int pixelsPerBlock = Main.configuration().pixelsPerBlock();
Interaction interaction; Interaction interaction;
if (pixels.size() <= 5 || true) { if (pixels.size() <= 5) {
interaction = (Interaction) absoluteLocation.getWorld().spawnEntity( interaction = (Interaction) absoluteLocation.getWorld().spawnEntity(
absoluteLocation.clone().add(0.5, -offset, 0.5), absoluteLocation.clone().add(0.5, -0, 0.5),
EntityType.INTERACTION EntityType.INTERACTION
); );
interaction.setInteractionHeight(1 + 2 * offset); interaction.setInteractionHeight(1);
interaction.setInteractionWidth(1 + 2 * offset); interaction.setInteractionWidth(1);
} else { } else {
// double startingX = absoluteLocation.x() + MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getX()); double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getX());
// double startingY = absoluteLocation.y() + MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getY()); double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getY());
// double startingZ = absoluteLocation.z() + MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getZ()); double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getZ());
//
// double endingX = absoluteLocation.x() + MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getX());
// double endingY = absoluteLocation.y() + MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getY());
// double endingZ = absoluteLocation.z() + MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getZ());
//
// Location spawnLocation = absoluteLocation.clone().add(
// ((startingX+endingX+1)/2)/pixelsPerBlock,
// ((startingY+endingY+1)/2)/pixelsPerBlock,
// ((startingZ+endingZ+1)/2)/pixelsPerBlock
// );
// float height = (float) (endingY-startingY+1)/pixelsPerBlock + 2*offset; double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getX());
// double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getY());
// float width; double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getZ());
// if((endingX-startingX) > (endingZ-startingZ)) {
// width = (float) (endingX-startingX+1)/pixelsPerBlock + 2*offset;
// } else {
// width = (float) (endingZ-startingZ+1)/pixelsPerBlock + 2*offset;
// }
//
// 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));
// }
// interaction = (Interaction) absoluteLocation.getWorld().spawnEntity( Location spawnLocation = absoluteLocation.clone().add(
// spawnLocation, ((startingX+endingX)/2)/pixelsPerBlock,
// EntityType.INTERACTION (startingY/pixelsPerBlock)-0,
// ); ((startingZ+endingZ)/2)/pixelsPerBlock
// );
// interaction.setInteractionHeight(0.5f);
// interaction.setInteractionWidth(0.5f); float height = (float) (endingY-startingY+1)/pixelsPerBlock;
float width;
if((endingX-startingX) > (endingZ-startingZ)) {
width = (float) (endingX-startingX+1)/pixelsPerBlock;
} else {
width = (float) (endingZ-startingZ+1)/pixelsPerBlock;
}
if(spawnLocation.getX()+width/2 > absoluteLocation.getX()+1) {
spawnLocation.subtract((spawnLocation.getX()+width/2)-(absoluteLocation.getX()+1), 0, 0);
}
if(spawnLocation.getX()-width/2 < absoluteLocation.getX()) {
spawnLocation.add(absoluteLocation.getX()-(spawnLocation.getX()-width/2), 0, 0);
}
if(spawnLocation.getZ()+width/2 > absoluteLocation.getZ()+1) {
spawnLocation.subtract(0, 0, (spawnLocation.getZ()+width/2)-(absoluteLocation.getZ()+1));
}
if(spawnLocation.getZ()-width/2 < absoluteLocation.getZ()) {
spawnLocation.add(0, 0, absoluteLocation.getZ()-(spawnLocation.getZ()-width/2));
}
interaction = (Interaction) absoluteLocation.getWorld().spawnEntity(
spawnLocation,
EntityType.INTERACTION
);
interaction.setInteractionHeight(height);
interaction.setInteractionWidth(width);
} }
interaction.getPersistentDataContainer() interaction.getPersistentDataContainer()