Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions src/main/java/dev/dubhe/anvilcraft/block/PulseGeneratorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,37 @@ public void update(Level level, BlockPos pos, Supplier<BlockState> stateGetter)
boolean lastInputting = generator.isInputtingSignal();
boolean nowInputting = PulseGeneratorBlock.getInputSignal(level, pos, stateGetter.get()) > 0;
generator.setInputtingSignal(nowInputting);

boolean canStart = switch (generator.getStartMode()) {
case RISING_EDGE -> !lastInputting && nowInputting;
case FALLING_EDGE -> lastInputting && !nowInputting;
case LOOP -> !generator.isDeadlock() && generator.getState() == PulseGeneratorBlockEntity.State.DEFAULT;
case LOOP -> !generator.isLocked();
} && !generator.isProcessing();

if (canStart) {
this.startWaiting(level, pos, stateGetter, generator);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
}

this.checkIsDeadlock(level, pos, stateGetter, generator);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
if (generator.getStartMode() == PulseGeneratorBlockEntity.Mode.LOOP) {
if (generator.isLocked() && !generator.isInputtingSignal()) {
this.startWaiting(level, pos, stateGetter, generator);
generator.setLocked(false);
} else {
generator.setLocked(generator.isInputtingSignal());
}
}
if (generator.isLocked()) {
generator.setState(PulseGeneratorBlockEntity.State.DEFAULT);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
}
}

@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
Optional<PulseGeneratorBlockEntity> generatorOp = level.getBlockEntity(pos, ModBlockEntities.PULSE_GENERATOR.get());
if (generatorOp.isEmpty()) return;
PulseGeneratorBlockEntity generator = generatorOp.get();
if (!generator.isDeadlock()) {
if (!generator.isLocked()) {
switch (generator.getState()) {
case WAITING -> this.startOutputting(level, pos, () -> state, generator);
case OUTPUTTING -> this.checkOnSignalEnd(level, pos, () -> state, generator);
Expand All @@ -167,23 +178,15 @@ protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSou
}
}

protected void checkIsDeadlock(Level level, BlockPos pos, Supplier<BlockState> stateGetter, PulseGeneratorBlockEntity generator) {
if (generator.getStartMode() == PulseGeneratorBlockEntity.Mode.LOOP) {
if (generator.isDeadlock() && !generator.isInputtingSignal()) {
this.startWaiting(level, pos, stateGetter, generator);
generator.setDeadlock(false);
} else {
generator.setDeadlock(generator.isInputtingSignal());
}
}
if (generator.isDeadlock()) {
generator.setState(PulseGeneratorBlockEntity.State.DEFAULT);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
}
}

public void startWaiting(Level level, BlockPos pos, Supplier<BlockState> stateGetter, PulseGeneratorBlockEntity generator) {
generator.setState(PulseGeneratorBlockEntity.State.WAITING);
if (generator.getWaitingTime() == 1
&& generator.getSignalDuration() == 0) {
generator.setState(PulseGeneratorBlockEntity.State.OUTPUTTING);
level.scheduleTick(pos, this, 1, TickPriority.LOW);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
return;
}
if (generator.getWaitingTime() != 0) {
level.scheduleTick(pos, this, generator.getWaitingTime(), TickPriority.LOW);
} else {
Expand All @@ -194,10 +197,10 @@ public void startWaiting(Level level, BlockPos pos, Supplier<BlockState> stateGe
protected void startOutputting(Level level, BlockPos pos, Supplier<BlockState> stateGetter, PulseGeneratorBlockEntity generator) {
generator.setState(PulseGeneratorBlockEntity.State.OUTPUTTING);
if (generator.getSignalDuration() != 0) {
level.scheduleTick(pos, this, generator.getSignalDuration(), TickPriority.VERY_LOW);
level.scheduleTick(pos, this, generator.getSignalDuration(), TickPriority.LOW);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
} else {
this.updateBlockAndNeighbours(level, pos, generator::getBlockState, generator);
this.updateBlockAndNeighbours(level, pos, stateGetter, generator);
this.checkOnSignalEnd(level, pos, stateGetter, generator);
}
}
Expand Down Expand Up @@ -274,10 +277,12 @@ protected InteractionResult useWithoutItem(
BlockEntity be = level.getBlockEntity(pos);
if (be instanceof PulseGeneratorBlockEntity blockEntity && player instanceof ServerPlayer sp) {
if (sp.gameMode.getGameModeForPlayer() == GameType.SPECTATOR) return InteractionResult.PASS;
sp.openMenu(blockEntity, buf -> {
buf.writeBlockPos(pos);
buf.writeNbt(blockEntity.constructDataNbt());
});
sp.openMenu(
blockEntity, buf -> {
buf.writeBlockPos(pos);
buf.writeNbt(blockEntity.constructDataNbt());
}
);
return InteractionResult.SUCCESS;
}
return InteractionResult.FAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class PulseGeneratorBlockEntity extends BlockEntity implements MenuProvid
protected State state = State.DEFAULT;

protected boolean isInputtingSignal = false;
protected boolean isDeadlock = false;
protected boolean isLocked = false;

public PulseGeneratorBlockEntity(BlockPos pos, BlockState blockState) {
super(ModBlockEntities.PULSE_GENERATOR.get(), pos, blockState);
Expand Down Expand Up @@ -134,7 +134,7 @@ public void setState(State state) {
public void setStartMode(int mode) {
this.startMode = Mode.fromIndex(mode % 3);
if (this.startMode != Mode.LOOP) {
this.isDeadlock = false;
this.isLocked = false;
} else if (!this.isInputtingSignal && this.level != null) {
Util.castSafely(this.getBlockState().getBlock(), PulseGeneratorBlock.class)
.ifPresent(block -> block.update(this.level, this.getBlockPos(), this::getBlockState));
Expand Down Expand Up @@ -172,7 +172,7 @@ public boolean isProcessing() {
}

public boolean isOutputting() {
if (this.isDeadlock) return this.outputInvert;
if (this.isLocked) return this.outputInvert;
return (this.state == State.OUTPUTTING) != this.outputInvert;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ public void applyMoveData(Level level, BlockPos pos, BlockState state, CompoundT
}
}
level.setBlock(pos, state.setValue(PulseGeneratorBlock.POWERED, this.isOutputting()), 3);
this.isDeadlock = false;
this.isLocked = false;

Util.<PulseGeneratorBlock>cast(this.getBlockState().getBlock()).update(level, pos, () -> state);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.dubhe.anvilcraft.block.heatable;

import dev.dubhe.anvilcraft.api.heat.HeatRecorder;
import dev.dubhe.anvilcraft.api.heat.HeatTier;
import dev.dubhe.anvilcraft.api.heat.HeaterManager;
import dev.dubhe.anvilcraft.block.entity.heatable.HeatableBlockEntity;
import dev.dubhe.anvilcraft.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
Expand All @@ -26,8 +28,17 @@ protected HeatableBlock(Properties properties) {
}

@Override
@SuppressWarnings("deprecation")
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
super.onPlace(state, level, pos, oldState, movedByPiston);
Direction[] directions = Direction.values();
if (HeatRecorder.getTier(level, pos, state).orElse(HeatTier.NORMAL) == HeatTier.NORMAL) return;
for (Direction direction : directions) {
if (level.getBlockState(pos.relative(direction)).is(Blocks.TNT)) {
TntBlock.explode(level, pos.relative(direction));
level.removeBlock(pos.relative(direction), false);
}
}
HeaterManager.addHeatableBlock(pos, level);
}

Expand Down Expand Up @@ -85,7 +96,9 @@ protected void neighborChanged(
BlockPos neighborPos,
boolean movedByPiston
) {
if (level.getBlockState(neighborPos).is(Blocks.TNT)) {
if (level.getBlockState(neighborPos).is(Blocks.TNT)
&& HeatRecorder.getTier(level, pos, state).orElse(HeatTier.NORMAL) != HeatTier.NORMAL
) {
TntBlock.explode(level, neighborPos);
level.removeBlock(neighborPos, false);
}
Expand Down
58 changes: 30 additions & 28 deletions src/main/java/dev/dubhe/anvilcraft/init/block/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,18 @@ public class ModBlocks {
.define('B', ModItems.EMBER_METAL_INGOT)
.define('C', ModBlocks.NEGATIVE_MATTER_BLOCK)
.unlockedBy(AnvilCraftDatagen.hasItem(ModItems.NEUTRONIUM_INGOT), AnvilCraftDatagen.has(ModItems.NEUTRONIUM_INGOT))
.unlockedBy(AnvilCraftDatagen.hasItem(ModItems.CHARGED_NEUTRONIUM_INGOT),
AnvilCraftDatagen.has(ModItems.CHARGED_NEUTRONIUM_INGOT))
.unlockedBy(AnvilCraftDatagen.hasItem(ModItems.STABLE_NEUTRONIUM_INGOT),
AnvilCraftDatagen.has(ModItems.STABLE_NEUTRONIUM_INGOT))
.unlockedBy(AnvilCraftDatagen.hasItem(ModBlocks.NEGATIVE_MATTER_BLOCK),
AnvilCraftDatagen.has(ModBlocks.NEGATIVE_MATTER_BLOCK))
.unlockedBy(
AnvilCraftDatagen.hasItem(ModItems.CHARGED_NEUTRONIUM_INGOT),
AnvilCraftDatagen.has(ModItems.CHARGED_NEUTRONIUM_INGOT)
)
.unlockedBy(
AnvilCraftDatagen.hasItem(ModItems.STABLE_NEUTRONIUM_INGOT),
AnvilCraftDatagen.has(ModItems.STABLE_NEUTRONIUM_INGOT)
)
.unlockedBy(
AnvilCraftDatagen.hasItem(ModBlocks.NEGATIVE_MATTER_BLOCK),
AnvilCraftDatagen.has(ModBlocks.NEGATIVE_MATTER_BLOCK)
)
.unlockedBy(AnvilCraftDatagen.hasItem(ModItems.EMBER_METAL_INGOT), AnvilCraftDatagen.has(ModItems.EMBER_METAL_INGOT))
.save(provider);
})
Expand Down Expand Up @@ -513,8 +519,10 @@ public class ModBlocks {
.define('A', ModBlocks.CAKE_BLOCK)
.define('B', ModFoodItems.CREAMY_BREAD_ROLL)
.unlockedBy(AnvilCraftDatagen.hasItem(ModBlocks.CAKE_BLOCK), AnvilCraftDatagen.has(ModBlocks.CAKE_BLOCK))
.unlockedBy(AnvilCraftDatagen.hasItem(ModFoodItems.CREAMY_BREAD_ROLL),
AnvilCraftDatagen.has(ModFoodItems.CREAMY_BREAD_ROLL))
.unlockedBy(
AnvilCraftDatagen.hasItem(ModFoodItems.CREAMY_BREAD_ROLL),
AnvilCraftDatagen.has(ModFoodItems.CREAMY_BREAD_ROLL)
)
.save(provider);
})
.register();
Expand Down Expand Up @@ -575,8 +583,6 @@ public class ModBlocks {
})
.initialProperties(() -> Blocks.ANVIL)
.tag(
BlockTags.WITHER_IMMUNE,
BlockTags.DRAGON_IMMUNE,
BlockTags.ANVIL,
ModBlockTags.CANT_BROKEN_ANVIL,
BlockTags.MINEABLE_WITH_PICKAXE,
Expand Down Expand Up @@ -608,7 +614,10 @@ public class ModBlocks {
.unlocks("hasitem", AnvilCraftDatagen.has(ModBlocks.FROST_METAL_BLOCK))
.save(provider, AnvilCraft.of("smithing/frost_grindstone"));
})
.tag(BlockTags.WITHER_IMMUNE, BlockTags.DRAGON_IMMUNE, BlockTags.MINEABLE_WITH_PICKAXE, BlockTags.NEEDS_DIAMOND_TOOL)
.tag(
BlockTags.MINEABLE_WITH_PICKAXE,
BlockTags.NEEDS_DIAMOND_TOOL
)
.initialProperties(() -> Blocks.NETHERITE_BLOCK)
.properties(properties -> properties.lightLevel(state -> 9).noOcclusion().emissiveRendering(ModBlocks::always))
.blockstate(DataGenUtil::noExtraModelOrState)
Expand All @@ -628,7 +637,10 @@ public class ModBlocks {
.unlocks("hasitem", AnvilCraftDatagen.has(ModBlocks.FROST_METAL_BLOCK))
.save(provider, AnvilCraft.of("smithing/frost_smithing_table"));
})
.tag(BlockTags.WITHER_IMMUNE, BlockTags.DRAGON_IMMUNE, BlockTags.MINEABLE_WITH_PICKAXE, BlockTags.NEEDS_DIAMOND_TOOL)
.tag(
BlockTags.MINEABLE_WITH_PICKAXE,
BlockTags.NEEDS_DIAMOND_TOOL
)
.initialProperties(() -> Blocks.NETHERITE_BLOCK)
.properties(properties -> properties.lightLevel(state -> 9).noOcclusion().emissiveRendering(ModBlocks::always))
.blockstate(DataGenUtil::noExtraModelOrState)
Expand Down Expand Up @@ -2221,9 +2233,7 @@ public class ModBlocks {
BlockTags.NEEDS_IRON_TOOL,
ModBlockTags.OVERSEER_BASE,
Tags.Blocks.STORAGE_BLOCKS,
ModBlockTags.STORAGE_BLOCKS_FROST_METAL,
BlockTags.WITHER_IMMUNE,
BlockTags.DRAGON_IMMUNE
ModBlockTags.STORAGE_BLOCKS_FROST_METAL
)
.blockstate((context, provider) -> provider.simpleBlock(
context.get(),
Expand Down Expand Up @@ -2251,9 +2261,7 @@ public class ModBlocks {
.tag(
BlockTags.MINEABLE_WITH_PICKAXE,
BlockTags.NEEDS_IRON_TOOL,
ModBlockTags.OVERSEER_BASE,
BlockTags.WITHER_IMMUNE,
BlockTags.DRAGON_IMMUNE
ModBlockTags.OVERSEER_BASE
)
.initialProperties(() -> Blocks.IRON_BLOCK)
.properties(
Expand Down Expand Up @@ -2285,9 +2293,7 @@ public class ModBlocks {
.tag(
BlockTags.MINEABLE_WITH_PICKAXE,
BlockTags.NEEDS_IRON_TOOL,
ModBlockTags.OVERSEER_BASE,
BlockTags.WITHER_IMMUNE,
BlockTags.DRAGON_IMMUNE
ModBlockTags.OVERSEER_BASE
)
.initialProperties(() -> Blocks.IRON_BLOCK)
.properties(
Expand Down Expand Up @@ -2320,9 +2326,7 @@ public class ModBlocks {
BlockTags.MINEABLE_WITH_PICKAXE,
BlockTags.NEEDS_IRON_TOOL,
BlockTags.SLABS,
ModBlockTags.OVERSEER_BASE,
BlockTags.WITHER_IMMUNE,
BlockTags.DRAGON_IMMUNE
ModBlockTags.OVERSEER_BASE
)
.initialProperties(() -> Blocks.IRON_BLOCK)
.properties(
Expand Down Expand Up @@ -2360,9 +2364,7 @@ public class ModBlocks {
BlockTags.MINEABLE_WITH_PICKAXE,
BlockTags.NEEDS_IRON_TOOL,
BlockTags.STAIRS,
ModBlockTags.OVERSEER_BASE,
BlockTags.WITHER_IMMUNE,
BlockTags.DRAGON_IMMUNE
ModBlockTags.OVERSEER_BASE
)
.initialProperties(() -> Blocks.IRON_BLOCK)
.properties(
Expand Down Expand Up @@ -4664,7 +4666,7 @@ private static BlockEntry<? extends PowerLevelPressurePlateBlock> registerPressu
.register();

public static final BlockEntry<LiquidBlock> OIL = REGISTRUM.block(
"oil", p -> new LiquidBlock(ModFluids.OIL.get(), p))
"oil", p -> new LiquidBlock(ModFluids.OIL.get(), p))
.properties(it -> it.mapColor(MapColor.TERRACOTTA_BLACK)
.replaceable()
.noCollission()
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/dev/dubhe/anvilcraft/item/AnvilHammerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,17 @@ public static Property<?> findModifyableProperty(BlockState state) {

public static boolean dropAnvil(@Nullable Player player, Level level, BlockPos blockPos) {
if (player == null || level.isClientSide) return false;
ItemStack itemStack = player.getItemInHand(player.getUsedItemHand());
ItemStack itemStack = player.getMainHandItem();
Item item = itemStack.getItem();
if (!(item instanceof AnvilHammerItem anvilHammerItem)) return false;
if (player.getCooldowns().isOnCooldown(anvilHammerItem)) {
return false;
}
if (player.getCooldowns().isOnCooldown(anvilHammerItem)) return false;
player.getCooldowns().addCooldown(itemStack.getItem(), 5);
FallingBlockEntity dummyAnvilEntity = new FallingBlockEntity(EntityType.FALLING_BLOCK, level);
dummyAnvilEntity.blockState = anvilHammerItem.getAnvil().defaultBlockState();
AnvilEvent.OnLand event = new AnvilEvent.OnLand(level, blockPos.above(), dummyAnvilEntity, player.fallDistance);
NeoForge.EVENT_BUS.post(event);
level.playSound(null, blockPos, SoundEvents.ANVIL_LAND, SoundSource.BLOCKS, 1f, 1f);
itemStack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(player.getUsedItemHand()));
itemStack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(InteractionHand.MAIN_HAND));
TriggerUtil.anvilHammerClickBlock(level, blockPos, "left_click");
Comment thread
Vo1dExr marked this conversation as resolved.
return true;
}
Expand Down
Loading