Use classes instead of interfaces for BlockHandler type

This commit is contained in:
TheMode 2021-06-25 19:00:50 +02:00
parent 5d8c9cbd23
commit 74073f13b5
5 changed files with 123 additions and 176 deletions

View File

@ -618,7 +618,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
// checks that we are actually in the block, and not just here because of a rounding error // checks that we are actually in the block, and not just here because of a rounding error
if (boundingBox.intersect(tmpPosition)) { if (boundingBox.intersect(tmpPosition)) {
// TODO: replace with check with custom block bounding box // TODO: replace with check with custom block bounding box
handler.onTouch(BlockHandler.Touch.from(block, instance, tmpPosition, this)); handler.onTouch(new BlockHandler.Touch(block, instance, tmpPosition, this));
} }
} }
} }

View File

@ -99,7 +99,7 @@ public class DynamicChunk extends Chunk {
final BlockPosition blockPosition = new BlockPosition(x, y, z); final BlockPosition blockPosition = new BlockPosition(x, y, z);
final Block block = getBlock(blockPosition); final Block block = getBlock(blockPosition);
entry.getValue().tick(BlockHandler.Tick.from(block, instance, blockPosition)); entry.getValue().tick(new BlockHandler.Tick(block, instance, blockPosition));
} }
} }

View File

@ -163,14 +163,14 @@ public class InstanceContainer extends Instance {
if (previousHandler != null) { if (previousHandler != null) {
// Previous destroy // Previous destroy
previousHandler.onDestroy(Objects.requireNonNullElseGet(destroy, previousHandler.onDestroy(Objects.requireNonNullElseGet(destroy,
() -> BlockHandler.Destroy.from(previousBlock, this, blockPosition))); () -> new BlockHandler.Destroy(previousBlock, this, blockPosition)));
} }
final BlockHandler handler = block.handler(); final BlockHandler handler = block.handler();
if (handler != null) { if (handler != null) {
// New placement // New placement
final Block finalBlock = block; final Block finalBlock = block;
handler.onPlace(Objects.requireNonNullElseGet(placement, handler.onPlace(Objects.requireNonNullElseGet(placement,
() -> BlockHandler.Placement.from(finalBlock, this, blockPosition))); () -> new BlockHandler.Placement(finalBlock, this, blockPosition)));
} }
} }
} }

View File

@ -85,46 +85,40 @@ public interface BlockHandler {
* and record pattern for the implementations (https://openjdk.java.net/jeps/405). * and record pattern for the implementations (https://openjdk.java.net/jeps/405).
*/ */
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
interface Placement { class Placement {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
static @NotNull Placement from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return new Placement() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
};
}
}
final class PlayerPlacement implements Placement {
private final Block block; private final Block block;
private final Instance instance; private final Instance instance;
private final BlockPosition blockPosition; private final BlockPosition blockPosition;
@ApiStatus.Internal
public Placement(Block block, Instance instance, BlockPosition blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
}
public @NotNull Block getBlock() {
return block;
}
public @NotNull Instance getInstance() {
return instance;
}
public @NotNull BlockPosition getBlockPosition() {
return blockPosition;
}
}
final class PlayerPlacement extends Placement {
private final Player player; private final Player player;
private final BlockFace blockFace; private final BlockFace blockFace;
private final float cursorX, cursorY, cursorZ; private final float cursorX, cursorY, cursorZ;
public PlayerPlacement(Block block, Instance instance, BlockPosition blockPosition, Player player, @ApiStatus.Internal
BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { public PlayerPlacement(Block block, Instance instance, BlockPosition blockPosition,
this.block = block; Player player, BlockFace blockFace, float cursorX, float cursorY, float cursorZ) {
this.instance = instance; super(block, instance, blockPosition);
this.blockPosition = blockPosition;
this.player = player; this.player = player;
this.blockFace = blockFace; this.blockFace = blockFace;
this.cursorX = cursorX; this.cursorX = cursorX;
@ -132,207 +126,160 @@ public interface BlockHandler {
this.cursorZ = cursorZ; this.cursorZ = cursorZ;
} }
@Override public @NotNull Player getPlayer() {
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
public @NotNull Player player() {
return player; return player;
} }
public @NotNull BlockFace blockFace() { public @NotNull BlockFace getBlockFace() {
return blockFace; return blockFace;
} }
public float cursorX() { public float getCursorX() {
return cursorX; return cursorX;
} }
public float cursorY() { public float getCursorY() {
return cursorY; return cursorY;
} }
public float cursorZ() { public float getCursorZ() {
return cursorZ; return cursorZ;
} }
} }
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
interface Destroy { class Destroy {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
static @NotNull Destroy from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return new Destroy() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
};
}
}
final class PlayerDestroy implements Destroy {
private final Block block; private final Block block;
private final Instance instance; private final Instance instance;
private final BlockPosition blockPosition; private final BlockPosition blockPosition;
private final Player player;
public PlayerDestroy(Block block, Instance instance, BlockPosition blockPosition, Player player) { @ApiStatus.Internal
public Destroy(Block block, Instance instance, BlockPosition blockPosition) {
this.block = block; this.block = block;
this.instance = instance; this.instance = instance;
this.blockPosition = blockPosition; this.blockPosition = blockPosition;
this.player = player;
} }
@Override public @NotNull Block getBlock() {
public @NotNull Block block() {
return block; return block;
} }
@Override public @NotNull Instance getInstance() {
public @NotNull Instance instance() {
return instance; return instance;
} }
@Override public @NotNull BlockPosition getBlockPosition() {
public @NotNull BlockPosition blockPosition() {
return blockPosition; return blockPosition;
} }
}
public @NotNull Player player() { final class PlayerDestroy extends Destroy {
private final Player player;
@ApiStatus.Internal
public PlayerDestroy(Block block, Instance instance, BlockPosition blockPosition, Player player) {
super(block, instance, blockPosition);
this.player = player;
}
public @NotNull Player getPlayer() {
return player; return player;
} }
} }
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
interface Interaction { class Interaction {
@NotNull Block block(); private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Player player;
private final Player.Hand hand;
@NotNull Instance instance(); @ApiStatus.Internal
public Interaction(Block block, Instance instance, BlockPosition blockPosition, Player player, Player.Hand hand) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
this.player = player;
this.hand = hand;
}
@NotNull BlockPosition blockPosition(); public @NotNull Block getBlock() {
return block;
}
@NotNull Player player(); public @NotNull Instance getInstance() {
return instance;
}
@NotNull Player.Hand hand(); public @NotNull BlockPosition getBlockPosition() {
return blockPosition;
}
static @NotNull Interaction from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition, public @NotNull Player getPlayer() {
@NotNull Player player, @NotNull Player.Hand hand) { return player;
return new Interaction() { }
@Override
public @NotNull Block block() {
return block;
}
@Override public @NotNull Player.Hand getHand() {
public @NotNull Instance instance() { return hand;
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
@Override
public @NotNull Player player() {
return player;
}
@Override
public @NotNull Player.Hand hand() {
return hand;
}
};
} }
} }
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
interface Touch { class Touch {
@NotNull Block block(); private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Entity touching;
@NotNull Instance instance(); @ApiStatus.Internal
public Touch(Block block, Instance instance, BlockPosition blockPosition, Entity touching) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
this.touching = touching;
}
@NotNull BlockPosition blockPosition(); public @NotNull Block getBlock() {
return block;
}
@NotNull Entity touching(); public @NotNull Instance getInstance() {
return instance;
}
static @NotNull Touch from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition, public @NotNull BlockPosition getBlockPosition() {
@NotNull Entity touching) { return blockPosition;
return new Touch() { }
@Override
public @NotNull Block block() {
return block;
}
@Override public @NotNull Entity getTouching() {
public @NotNull Instance instance() { return touching;
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
@Override
public @NotNull Entity touching() {
return touching;
}
};
} }
} }
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
interface Tick { class Tick {
@NotNull Block block(); private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
@NotNull Instance instance(); @ApiStatus.Internal
public Tick(Block block, Instance instance, BlockPosition blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
}
@NotNull BlockPosition blockPosition(); public @NotNull Block getBlock() {
return block;
}
static @NotNull Tick from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition) { public @NotNull Instance getInstance() {
return new Tick() { return instance;
@Override }
public @NotNull Block block() {
return block;
}
@Override public @NotNull BlockPosition getBlockPosition() {
public @NotNull Instance instance() { return blockPosition;
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
};
} }
} }
} }

View File

@ -59,7 +59,7 @@ public class BlockPlacementListener {
if (!playerBlockInteractEvent.isCancelled()) { if (!playerBlockInteractEvent.isCancelled()) {
final var handler = interactedBlock.handler(); final var handler = interactedBlock.handler();
if (handler != null) { if (handler != null) {
handler.onInteract(BlockHandler.Interaction.from(interactedBlock, instance, blockPosition, player, hand)); handler.onInteract(new BlockHandler.Interaction(interactedBlock, instance, blockPosition, player, hand));
} }
} }
if (playerBlockInteractEvent.isBlockingItemUse()) { if (playerBlockInteractEvent.isBlockingItemUse()) {