Skip to content

Commit

Permalink
salepoint redstone output
Browse files Browse the repository at this point in the history
  • Loading branch information
techno-sam committed Aug 6, 2024
1 parent 0e9c747 commit f9adbab
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
import dev.ithundxr.createnumismatics.registry.NumismaticsBlockEntities;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
Expand All @@ -39,13 +43,33 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class SalepointBlock extends Block implements IBE<SalepointBlockEntity>, TrustedBlock, IWrenchable, NotifyFailedBreak {

public static final BooleanProperty POWERED = BlockStateProperties.POWERED;

public SalepointBlock(Properties properties) {
super(properties);
this.registerDefaultState(defaultBlockState()
.setValue(POWERED, false));
}

@Nullable
@Override
public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
return defaultBlockState()
.setValue(POWERED, false);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(POWERED);
}

@Override
Expand All @@ -58,6 +82,69 @@ public BlockEntityType<? extends SalepointBlockEntity> getBlockEntityType() {
return NumismaticsBlockEntities.SALEPOINT.get();
}

void startSignal(LevelAccessor level, BlockPos pos) {
if (!level.isClientSide() && !level.getBlockTicks().hasScheduledTick(pos, this)) {
level.scheduleTick(pos, this, 2);
}
}

@Override
@SuppressWarnings("deprecation")
public void tick(BlockState state, @NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull RandomSource random) {
if (state.getValue(POWERED)) {
level.setBlock(pos, state.setValue(POWERED, false), 2);
} else {
level.setBlock(pos, state.setValue(POWERED, true), 2);
level.scheduleTick(pos, this, 2);
}
this.updateNeighbors(level, pos, state);
}

protected void updateNeighbors(Level level, BlockPos pos, BlockState state) {
//Direction direction = state.getValue(FACING);
for (Direction direction : Direction.values()) {
updateNeighborsInDirection(level, pos, direction);
}
}

protected void updateNeighborsInDirection(Level level, BlockPos pos, Direction direction) {
BlockPos blockPos = pos.relative(direction.getOpposite());
level.neighborChanged(blockPos, this, pos);
level.updateNeighborsAtExceptFromFacing(blockPos, this, direction);
}

@Override
@SuppressWarnings("deprecation")
public boolean isSignalSource(@NotNull BlockState state) {
return true;
}

@Override
@SuppressWarnings("deprecation")
public int getDirectSignal(@NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull Direction direction) {
return state.getSignal(level, pos, direction);
}

@Override
@SuppressWarnings("deprecation")
public int getSignal(@NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull Direction direction) {
return state.getValue(POWERED) ? 15 : 0;
}

@Override
@SuppressWarnings("deprecation")
public boolean hasAnalogOutputSignal(@NotNull BlockState state) {
return true;
}

@Override
@SuppressWarnings("deprecation")
public int getAnalogOutputSignal(@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos) {
if (level.getBlockEntity(pos) instanceof SalepointBlockEntity be)
return be.getTargetAnalogOutput();
return 0;
}

@Override
public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, @Nullable LivingEntity placer, @NotNull ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
Expand All @@ -71,6 +158,18 @@ public void notifyFailedBreak(LevelAccessor level, BlockPos pos, BlockState stat
withBlockEntityDo(level, pos, SalepointBlockEntity::notifyDelayedDataSync);
}

@Override
@SuppressWarnings("deprecation")
public void onPlace(BlockState state, @NotNull Level level, @NotNull BlockPos pos, BlockState oldState, boolean movedByPiston) {
if (!state.is(oldState.getBlock())) {
if (!level.isClientSide() && state.getValue(POWERED) && !level.getBlockTicks().hasScheduledTick(pos, this)) {
BlockState blockState = state.setValue(POWERED, false);
level.setBlock(pos, blockState, 18);
this.updateNeighbors(level, pos, blockState);
}
}
}

@Override
@SuppressWarnings("deprecation")
public void onRemove(@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState newState, boolean movedByPiston) {
Expand All @@ -81,6 +180,10 @@ public void onRemove(@NotNull BlockState state, @NotNull Level level, @NotNull B

level.updateNeighbourForOutputSignal(pos, this);
}

if (!level.isClientSide && state.getValue(POWERED) && level.getBlockTicks().hasScheduledTick(pos, this)) {
this.updateNeighbors(level, pos, state.setValue(POWERED, false));
}
}

IBE.onRemove(state, level, pos, newState);
Expand All @@ -101,7 +204,7 @@ public InteractionResult onSneakWrenched(BlockState state, UseOnContext context)
if (level.isClientSide)
return InteractionResult.SUCCESS;

boolean crouching = player.isCrouching();
boolean crouching = player.isShiftKeyDown();
if (crouching) {
if (isTrusted(player, level, pos)) {
withBlockEntityDo(level, pos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void setChanged() {
protected final MenuProvider configMenuProvider = new ConfigMenuProvider();
protected final MenuProvider purchaseMenuProvider = new PurchaseMenuProvider();
private boolean delayedDataSync = false;
private int lastAnalogOutput = -1;

private SliderStylePriceBehaviour price;

Expand Down Expand Up @@ -307,6 +308,7 @@ private void attemptTransaction() {
if (transactionResult.shouldStop) {
if (transactionResult == TransactionResult.SUCCESS) {
justCompletedMultiplier = transaction.multiplier();
((SalepointBlock) getBlockState().getBlock()).startSignal(level, getBlockPos());
}
this.transaction = null;
}
Expand All @@ -315,12 +317,28 @@ private void attemptTransaction() {
notifyUpdate();
}

int getTargetAnalogOutput() {
if (transaction == null)
return 0;

return Math.max(1, Math.min(transaction.progress() * 15 / transaction.multiplier(), 15));
}

@Override
public void tick() {
super.tick();

if (soundCooldown > 0)
soundCooldown--;

if (level == null || level.isClientSide)
return;

int analogOutput = getTargetAnalogOutput();
if (analogOutput != lastAnalogOutput) {
lastAnalogOutput = analogOutput;
level.updateNeighbourForOutputSignal(getBlockPos(), getBlockState().getBlock());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public InteractionResult onSneakWrenched(BlockState state, UseOnContext context)
if (level.isClientSide)
return InteractionResult.SUCCESS;

boolean crouching = player.isCrouching();
boolean crouching = player.isShiftKeyDown();
if (crouching) {
if (isTrusted(player, level, pos)) {
withBlockEntityDo(level, pos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import dev.ithundxr.createnumismatics.multiloader.CommonTags;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour.Properties;
import net.minecraft.world.level.material.MapColor;
Expand Down Expand Up @@ -141,6 +142,7 @@ public class NumismaticsBlocks {
.initialProperties(SharedProperties::softMetal)
.properties(p -> p.strength(1.0F, 3600000.0F)) // Unexplodable
.properties(Properties::requiresCorrectToolForDrops)
.properties(p -> p.isRedstoneConductor(Blocks::never))
.transform(pickaxeOnly())
.lang("Salepoint")
//.transform(BuilderTransformers.salepoint())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ protected boolean hasBufferFluidForPurchase() {
@Override
protected List<MultiloaderFluidStack> removeBufferFluidForPurchase() {
try (Transaction transaction = Transaction.openOuter()) {
FluidStack out = buffer.getFluid().copy();

long amount = buffer.extract(((MultiloaderFluidStackImpl) getFilter()).getType(), getFilter().getAmount(), transaction);
transaction.commit();

return List.of(
new MultiloaderFluidStackImpl(buffer.getFluid().copy().setAmount(amount))
new MultiloaderFluidStackImpl(out.setAmount(amount))
);
}
}
Expand Down

0 comments on commit f9adbab

Please sign in to comment.