Skip to content

Commit

Permalink
Added IUnbreakble interface, to easier make a block unbreakable
Browse files Browse the repository at this point in the history
  • Loading branch information
GirafiStudios committed Dec 25, 2020
1 parent ab6ae39 commit 2573cf3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 42 deletions.
24 changes: 5 additions & 19 deletions src/main/java/com/teammetallurgy/atum/blocks/QuandaryBlock.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.teammetallurgy.atum.blocks;

import com.teammetallurgy.atum.Atum;
import com.teammetallurgy.atum.api.God;
import com.teammetallurgy.atum.blocks.base.IUnbreakable;
import com.teammetallurgy.atum.blocks.lighting.INebuTorch;
import com.teammetallurgy.atum.blocks.stone.limestone.LimestoneBrickBlock;
import com.teammetallurgy.atum.blocks.stone.limestone.chest.tileentity.SarcophagusTileEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
Expand All @@ -22,29 +21,21 @@
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import javax.annotation.Nonnull;

@Mod.EventBusSubscriber(modid = Atum.MOD_ID)
public class QuandaryBlock extends Block {
public class QuandaryBlock extends Block implements IUnbreakable {
public static final DirectionProperty FACING = DirectionalBlock.FACING;
private static final BooleanProperty ACTIVATED = BooleanProperty.create("activated");
public static final BooleanProperty UNBREAKABLE = BooleanProperty.create("unbreakable");

public QuandaryBlock() {
super(Block.Properties.create(Material.ROCK, MaterialColor.SAND).hardnessAndResistance(1.5F, 8.0F).setRequiresTool().harvestTool(ToolType.PICKAXE).harvestLevel(1));
this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH).with(ACTIVATED, false).with(UNBREAKABLE, false));
}

@SubscribeEvent
public static void onBlockBreak(BlockEvent.BreakEvent event) {
BlockState state = event.getState();
if (state.getBlock() instanceof LimestoneBrickBlock && state.get(LimestoneBrickBlock.UNBREAKABLE) && !event.getPlayer().isCreative()) {
event.setCanceled(true);
}
@Override
public float getExplosionResistance(BlockState state, IBlockReader world, BlockPos pos, Explosion explosion) {
return world.getBlockState(pos).get(UNBREAKABLE) ? 6000000.0F : super.getExplosionResistance(state, world, pos, explosion);
}

@Override
Expand All @@ -54,11 +45,6 @@ public void neighborChanged(@Nonnull BlockState state, @Nonnull World world, @No
world.setBlockState(pos, state.with(ACTIVATED, facingBlock instanceof INebuTorch && ((INebuTorch) facingBlock).isNebuTorch()), 2);
}

@Override
public float getExplosionResistance(BlockState state, IBlockReader world, BlockPos pos, Explosion explosion) {
return world.getBlockState(pos).get(UNBREAKABLE) ? 6000000.0F : super.getExplosionResistance(state, world, pos, explosion);
}

@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
return this.getDefaultState().with(FACING, context.getNearestLookingDirection().getOpposite());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.teammetallurgy.atum.blocks.base;

import com.teammetallurgy.atum.Atum;
import net.minecraft.block.BlockState;
import net.minecraft.state.BooleanProperty;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = Atum.MOD_ID)
public interface IUnbreakable {
BooleanProperty UNBREAKABLE = BooleanProperty.create("unbreakable");

@SubscribeEvent
public static void onBlockBreak(BlockEvent.BreakEvent event) {
BlockState state = event.getState();
if (state.getBlock() instanceof IUnbreakable && state.get(IUnbreakable.UNBREAKABLE) && !event.getPlayer().isCreative()) {
event.setCanceled(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.teammetallurgy.atum.blocks.stone.limestone;

import com.teammetallurgy.atum.Atum;
import com.teammetallurgy.atum.blocks.base.IUnbreakable;
import com.teammetallurgy.atum.blocks.machines.KilnBlock;
import com.teammetallurgy.atum.init.AtumBlocks;
import net.minecraft.block.Block;
Expand All @@ -23,16 +23,11 @@
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import javax.annotation.Nonnull;
import java.util.Random;

@Mod.EventBusSubscriber(modid = Atum.MOD_ID)
public class LimestoneBrickBlock extends FallingBlock {
public static final BooleanProperty UNBREAKABLE = BooleanProperty.create("unbreakable");
public class LimestoneBrickBlock extends FallingBlock implements IUnbreakable {
public static final BooleanProperty CAN_FALL = BooleanProperty.create("can_fall");

public LimestoneBrickBlock() {
Expand Down Expand Up @@ -60,14 +55,6 @@ public void onBlockPlacedBy(@Nonnull World world, @Nonnull BlockPos pos, @Nonnul
}
}

@SubscribeEvent
public static void onBlockBreak(BlockEvent.BreakEvent event) {
BlockState state = event.getState();
if (state.getBlock() instanceof LimestoneBrickBlock && state.get(LimestoneBrickBlock.UNBREAKABLE) && !event.getPlayer().isCreative()) {
event.setCanceled(true);
}
}

@Override
public float getExplosionResistance(BlockState state, IBlockReader world, BlockPos pos, Explosion explosion) {
return world.getBlockState(pos).get(UNBREAKABLE) ? 6000000.0F : super.getExplosionResistance(state, world, pos, explosion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.Explosion;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.*;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -75,15 +72,15 @@ public ActionResultType onBlockActivated(BlockState state, World world, @Nonnull
TileEntity tileLeft = world.getTileEntity(posLeft);
if (world.getBlockState(posLeft).getBlock() == this && tileLeft instanceof SarcophagusTileEntity) {
SarcophagusTileEntity sarcophagus = (SarcophagusTileEntity) tileLeft;
if (!sarcophagus.hasSpawned) {
if (world.getDifficulty() != Difficulty.PEACEFUL && !sarcophagus.hasSpawned) {
this.onBlockActivated(state, world, pos.offset(facing.rotateY()), player, hand, hit);
return ActionResultType.PASS;
}
}

if (tileEntity instanceof SarcophagusTileEntity) {
SarcophagusTileEntity sarcophagus = (SarcophagusTileEntity) tileEntity;
if (!sarcophagus.hasSpawned) {
if (world.getDifficulty() != Difficulty.PEACEFUL && !sarcophagus.hasSpawned) {
if (QuandaryBlock.Helper.canSpawnPharaoh(world, pos, facing, player, sarcophagus)) {
return ActionResultType.PASS;
} else if (!sarcophagus.isOpenable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.serialization.Codec;
import com.teammetallurgy.atum.api.event.PharaohBeatenEvent;
import com.teammetallurgy.atum.blocks.base.IUnbreakable;
import com.teammetallurgy.atum.blocks.stone.limestone.LimestoneBrickBlock;
import com.teammetallurgy.atum.blocks.trap.TrapBlock;
import com.teammetallurgy.atum.init.AtumBiomes;
Expand Down Expand Up @@ -143,15 +144,15 @@ public void changePyramidBlocks(ServerWorld world) {
BlockPos pos = new BlockPos(x, y, z);
if (!world.isAirBlock(pos)) {
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof LimestoneBrickBlock && state.get(LimestoneBrickBlock.UNBREAKABLE)) {
if (state.getBlock() instanceof IUnbreakable && state.get(IUnbreakable.UNBREAKABLE)) {
if (state.getBlock() == AtumBlocks.LIMESTONE_BRICK_LARGE && world.rand.nextDouble() <= 0.08D) {
if (world.isAirBlock(pos.down())) {
world.setBlockState(pos, AtumBlocks.LIMESTONE_BRICK_CRACKED_BRICK.getDefaultState().with(LimestoneBrickBlock.CAN_FALL, true), 2);
} else {
world.setBlockState(pos, AtumBlocks.LIMESTONE_BRICK_CRACKED_BRICK.getDefaultState(), 2);
}
} else {
world.setBlockState(pos, state.with(LimestoneBrickBlock.UNBREAKABLE, false), 2);
world.setBlockState(pos, state.with(IUnbreakable.UNBREAKABLE, false), 2);
}
} else if (state.getBlock() instanceof TrapBlock) {
world.setBlockState(pos, AtumBlocks.LIMESTONE_BRICK_CARVED.getDefaultState(), 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "minecraft:item",
"name": "atum:quandary_block"
}
],
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
]
}
]
}

0 comments on commit 2573cf3

Please sign in to comment.