Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Use different method for detecting dupes, closes #2
Browse files Browse the repository at this point in the history
thank you for help from lynxplay and sulu from Paper.
I do not think I could have fixed this issue without.

Co-authored-by: Bjarne Koll <LynxPlay101@gmail.com>
Co-authored-by: sulu5890 <sulu@sulu.me>
  • Loading branch information
3 people committed Feb 13, 2022
1 parent 3e1a6a3 commit d92581e
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/main/java/dev/thoughtcrime/gravitycontrol/GravityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;

import java.util.EnumSet;
Expand All @@ -18,7 +19,8 @@
import static com.destroystokyo.paper.MaterialTags.CONCRETE_POWDER;

public class GravityListener implements Listener {
private static final EnumSet<BlockFace> DUPE_ALLOWED_PORTAL_FACES = EnumSet.of(
private static final Vector ZERO = new Vector(0, 0, 0);
private static final EnumSet<BlockFace> BLOCK_FACES = EnumSet.of(
BlockFace.NORTH,
BlockFace.EAST,
BlockFace.SOUTH,
Expand All @@ -34,39 +36,41 @@ public GravityListener(final GravityControl plugin) {
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityChangeBlock(final EntityChangeBlockEvent event) {
if (!(event.getEntity() instanceof final FallingBlock falling) || event.getTo() == Material.AIR) return;
final Vector velocity = falling.getVelocity().clone();
final BoundingBox boundingBox = falling.getBoundingBox().expand(-0.01D);

for (BlockFace face : DUPE_ALLOWED_PORTAL_FACES) {
for (BlockFace face : BLOCK_FACES) {
final Block relative = event.getBlock().getRelative(face);
if (relative.getType() != Material.END_PORTAL) continue;

if ((face == BlockFace.NORTH && velocity.getZ() < 0D)
|| (face == BlockFace.EAST && velocity.getX() > 0D)
|| (face == BlockFace.SOUTH && velocity.getZ() > 0D)
|| (face == BlockFace.WEST && velocity.getX() < 0D)) {
this.dupe(falling, velocity);
return;
if (relative.getBoundingBox().overlaps(boundingBox)) {
this.dupe(falling, face.getDirection());
}
}
}

private void dupe(final FallingBlock falling, final Vector velocity) {
private void dupe(final FallingBlock falling, final Vector portalRelativeVector) {
final Location location = falling.getLocation();
final World world = location.getWorld();
if (!this.plugin.config.worlds.contains("*") && !this.plugin.config.worlds.contains(world.getName()))
return;

final Material m = falling.getBlockData().getMaterial();
if (
(m == Material.SAND && !this.plugin.config.blocks.sand)
if ((m == Material.SAND && !this.plugin.config.blocks.sand)
|| (m == Material.RED_SAND && !this.plugin.config.blocks.redSand)
|| (ANVIL.isTagged(m) && !this.plugin.config.blocks.anvil)
|| (m == Material.DRAGON_EGG && !this.plugin.config.blocks.dragonEgg)
|| (m == Material.GRAVEL && !this.plugin.config.blocks.gravel)
|| (CONCRETE_POWDER.isTagged(m) && !this.plugin.config.blocks.concretePowder)
) return;

world.spawnFallingBlock(falling.getLocation().add(velocity.clone().multiply(0.25)), falling.getBlockData())
final Vector velocity = falling.getVelocity();

if (velocity.equals(ZERO)) {
world.spawnFallingBlock(falling.getLocation().add(portalRelativeVector.multiply(0.25D).setY(0.05D)), falling.getBlockData());
return;
}

world.spawnFallingBlock(location.add(velocity.clone().multiply(0.25)), falling.getBlockData())
.setVelocity(velocity.clone().multiply(this.plugin.config.horizontalCoefficient).setY(velocity.getY() * this.plugin.config.verticalCoefficient));
}
}

0 comments on commit d92581e

Please sign in to comment.