Skip to content

Commit

Permalink
Adds Event for allowing/disallowing players the use of controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeryn99 committed Dec 19, 2023
1 parent ca72e07 commit 651c5bd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -57,6 +62,35 @@ public class TardisEvents {
}
}));


/**
* Represents an event that allows checking whether player control can be used.
*/
public static final Event<CanControlBeUsed> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 651c5bd

Please sign in to comment.