-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/dev/enjarai/minitardis/block/console/ConsoleToggleButtonBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.enjarai.minitardis.block.console; | ||
|
||
import dev.enjarai.minitardis.component.TardisControl; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockSetType; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.sound.SoundEvents; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.random.Random; | ||
import net.minecraft.world.World; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
@SuppressWarnings("deprecation") | ||
public class ConsoleToggleButtonBlock extends ConsoleButtonBlock { | ||
private final BiFunction<TardisControl, Boolean, Boolean> controlInput; | ||
|
||
public ConsoleToggleButtonBlock(Settings settings, BlockSetType buttonType, Block polymerBlock, boolean wooden, BiFunction<TardisControl, Boolean, Boolean> controlInput) { | ||
super(settings, buttonType, polymerBlock, wooden, null); | ||
this.controlInput = controlInput; | ||
} | ||
|
||
@Override | ||
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
var isPowered = !state.get(POWERED); | ||
world.setBlockState(pos, state.with(POWERED, isPowered)); | ||
|
||
if (!getTardis(world).map(tardis -> controlInput.apply(tardis.getControls(), isPowered)).orElse(false)) { | ||
inputFailure(world, pos, SoundEvents.BLOCK_IRON_TRAPDOOR_OPEN, 0); | ||
} | ||
|
||
return ActionResult.SUCCESS; | ||
} | ||
|
||
@Override | ||
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { | ||
getTardis(world).ifPresent(tardis -> { | ||
var locked = tardis.getControls().isDestinationLocked(); | ||
if (state.get(POWERED) != locked) { | ||
world.setBlockState(pos, state.with(POWERED, locked)); | ||
} | ||
}); | ||
world.scheduleBlockTick(pos, this, 10); | ||
} | ||
|
||
@Override | ||
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) { | ||
world.scheduleBlockTick(pos, this, 10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters