diff --git a/changelog.md b/changelog.md index 2a431685c..866674989 100644 --- a/changelog.md +++ b/changelog.md @@ -76,6 +76,8 @@ - Fixed console removal running the interaction twice and causing the configurator to change its console theme - Fixed console changing not updating control positions and sizes - Fixed various screens hard crashing the server +- Fixed BulkHeadDoor deleting blocks around it when placed, you can no longer place it unless there is enough room + ## API #### Additions diff --git a/common/src/main/java/whocraft/tardis_refined/api/event/TardisEvents.java b/common/src/main/java/whocraft/tardis_refined/api/event/TardisEvents.java index 77d90544c..15f925281 100644 --- a/common/src/main/java/whocraft/tardis_refined/api/event/TardisEvents.java +++ b/common/src/main/java/whocraft/tardis_refined/api/event/TardisEvents.java @@ -8,8 +8,13 @@ import net.minecraft.world.level.LevelAccessor; import whocraft.tardis_refined.common.capability.TardisLevelOperator; import whocraft.tardis_refined.common.capability.upgrades.Upgrade; +import whocraft.tardis_refined.common.entity.ControlEntity; import whocraft.tardis_refined.common.tardis.ExteriorShell; import whocraft.tardis_refined.common.tardis.TardisNavLocation; +import whocraft.tardis_refined.common.tardis.control.Control; + +import java.util.List; +import java.util.function.Function; public class TardisEvents { @@ -57,6 +62,35 @@ public class TardisEvents { } })); + + /** + * Represents an event that allows checking whether player control can be used. + */ + public static final Event PLAYER_CONTROL_INTERACT = new Event<>(CanControlBeUsed.class, listeners -> ((tardisLevelOperator, control, controlEntity) -> { + for (CanControlBeUsed listener : listeners) { + return listener.canControlBeUsed(tardisLevelOperator, control, controlEntity); + } + return true; + })); + + /** + * Functional interface to define the conditions for using player control. + */ + @FunctionalInterface + public interface CanControlBeUsed { + + /** + * Checks whether player control can be used based on specified parameters. + * + * @param tardisLevelOperator The Tardis level operator. + * @param control The control to be used. + * @param controlEntity The entity associated with the control. + * @return True if control can be used, false otherwise. + */ + boolean canControlBeUsed(TardisLevelOperator tardisLevelOperator, Control control, ControlEntity controlEntity); + } + + /** * An event that is triggered when a TARDIS takes off. */ diff --git a/common/src/main/java/whocraft/tardis_refined/common/entity/ControlEntity.java b/common/src/main/java/whocraft/tardis_refined/common/entity/ControlEntity.java index 9e47d056b..b95a14063 100644 --- a/common/src/main/java/whocraft/tardis_refined/common/entity/ControlEntity.java +++ b/common/src/main/java/whocraft/tardis_refined/common/entity/ControlEntity.java @@ -265,10 +265,10 @@ private void handleControlSizeAndPositionAdjustment(Player player){ } } - private boolean isDesktopWaitingToGenerate(TardisLevelOperator operator, ServerLevel serverLevel){ + public boolean isDesktopWaitingToGenerate(TardisLevelOperator operator){ if (!(this.controlSpecification.control().getControl() instanceof MonitorControl)) { if (operator.getInteriorManager().isWaitingToGenerate()) { - serverLevel.playSound(null, this.blockPosition(), SoundEvents.NOTE_BLOCK_BIT.value(), SoundSource.BLOCKS, 100F, (float) (0.1 + (serverLevel.getRandom().nextFloat() * 0.5))); + operator.getLevel().playSound(null, this.blockPosition(), SoundEvents.NOTE_BLOCK_BIT.value(), SoundSource.BLOCKS, 100F, (float) (0.1 + (level().getRandom().nextFloat() * 0.5))); return true; } } @@ -278,11 +278,12 @@ private boolean isDesktopWaitingToGenerate(TardisLevelOperator operator, ServerL private void handleLeftClick(Player player, ServerLevel serverLevel){ TardisLevelOperator.get(serverLevel).ifPresent(cap -> { - if (isDesktopWaitingToGenerate(cap, serverLevel)) + if (controlSpecification.control().getControl().canUseControl(cap, controlSpecification.control().getControl(), this)) return; if (!interactWaitingControl(cap)) { Control control = this.controlSpecification.control().getControl(); + boolean successfulUse = control.onLeftClick(cap, consoleTheme, this, player); PitchedSound playedSound = successfulUse ? control.getSuccessSound(cap, this.consoleTheme, true) : control.getFailSound(cap, this.consoleTheme, true); control.playControlPitchedSound(cap, this, playedSound); @@ -309,7 +310,7 @@ private void handleRightClick(Player player, ServerLevel serverLevel, Interactio return; } - if (isDesktopWaitingToGenerate(cap, serverLevel)) + if (controlSpecification.control().getControl().canUseControl(cap, controlSpecification.control().getControl(), this)) return; if (!interactWaitingControl(cap)) { diff --git a/common/src/main/java/whocraft/tardis_refined/common/tardis/control/Control.java b/common/src/main/java/whocraft/tardis_refined/common/tardis/control/Control.java index 0315883d4..630056da0 100644 --- a/common/src/main/java/whocraft/tardis_refined/common/tardis/control/Control.java +++ b/common/src/main/java/whocraft/tardis_refined/common/tardis/control/Control.java @@ -3,6 +3,7 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.entity.player.Player; +import whocraft.tardis_refined.api.event.TardisEvents; import whocraft.tardis_refined.common.capability.TardisLevelOperator; import whocraft.tardis_refined.common.entity.ControlEntity; import whocraft.tardis_refined.common.tardis.themes.ConsoleTheme; @@ -58,4 +59,9 @@ public void playControlPitchedSound(TardisLevelOperator operator, ControlEntity this.playControlPitchedSound(operator, controlEntity, pitchedSound, SoundSource.BLOCKS, 1F, 1F, false); } + public boolean canUseControl(TardisLevelOperator tardisLevelOperator, Control control, ControlEntity controlEntity){ + boolean isDeskopWaiting = controlEntity.isDesktopWaitingToGenerate(tardisLevelOperator); + return !isDeskopWaiting && TardisEvents.PLAYER_CONTROL_INTERACT.invoker().canControlBeUsed(tardisLevelOperator, control, controlEntity); + } + }