Created
July 26, 2022 14:11
-
-
Save Lanse505/4d68b6fd69fad0afb5747c82cb26e9b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.teamacronymcoders.matteroverdrive.block; | |
import com.teamacronymcoders.matteroverdrive.api.misc.RotationType; | |
import com.teamacronymcoders.matteroverdrive.block.extendable.block.MOMultiBlock; | |
import com.teamacronymcoders.matteroverdrive.block.extendable.block.MORotatableBlock; | |
import com.teamacronymcoders.matteroverdrive.block.tile.ChargingStationTile; | |
import com.teamacronymcoders.matteroverdrive.util.MOBlockUtil; | |
import net.minecraft.SharedConstants; | |
import net.minecraft.core.BlockPos; | |
import net.minecraft.core.Direction; | |
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket; | |
import net.minecraft.world.entity.LivingEntity; | |
import net.minecraft.world.item.BlockItem; | |
import net.minecraft.world.item.ItemStack; | |
import net.minecraft.world.item.context.BlockPlaceContext; | |
import net.minecraft.world.level.Level; | |
import net.minecraft.world.level.block.Block; | |
import net.minecraft.world.level.block.Blocks; | |
import net.minecraft.world.level.block.entity.BlockEntityType; | |
import net.minecraft.world.level.block.state.BlockState; | |
import javax.annotation.Nonnull; | |
import javax.annotation.Nullable; | |
public class ChargingStationBlock extends MOMultiBlock<ChargingStationTile> { | |
public ChargingStationBlock() { | |
super(Properties.copy(Blocks.IRON_BLOCK).noOcclusion(), ChargingStationTile.class, true, BlockPos.ZERO); | |
} | |
@Override | |
public BlockEntityType.BlockEntitySupplier<?> getBlockEntityFactory() { | |
return ChargingStationTile::new; | |
} | |
@Nonnull | |
@Override | |
public RotationType getRotationType() { | |
return RotationType.FOUR_WAY; | |
} | |
@Override | |
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity entity, ItemStack stack) { | |
super.setPlacedBy(level, pos, state, entity, stack); | |
this.setParent(true); | |
this.setParentPos(pos); | |
BlockPos offsetPos = pos; | |
for (int i = 0; i < 2; i++) { | |
offsetPos = BlockPos.of(BlockPos.offset(offsetPos.asLong(), Direction.UP)); | |
level.setBlockAndUpdate(offsetPos, this.defaultBlockState()); | |
MOMultiBlock<?> bbBlock = (MOMultiBlock<?>) level.getBlockState(offsetPos).getBlock(); | |
bbBlock.setParent(false); | |
bbBlock.setParentPos(pos); | |
} | |
} | |
public static class Item extends BlockItem { | |
public Item(Block blockIn, Properties builder) { | |
super(blockIn, builder); | |
} | |
@Override | |
protected boolean canPlace(BlockPlaceContext context, BlockState state) { | |
return super.canPlace(context, state) && MOBlockUtil.checkDirectionForState(context.getLevel(), context.getClickedPos(), Direction.UP, Blocks.AIR, 2); | |
} | |
} | |
} |
This file contains hidden or 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
package com.teamacronymcoders.matteroverdrive.block.extendable.block; | |
import com.teamacronymcoders.matteroverdrive.block.extendable.tile.MOBaseTile; | |
import com.teamacronymcoders.matteroverdrive.util.MOTileHelper; | |
import net.minecraft.core.BlockPos; | |
import net.minecraft.core.Direction; | |
import net.minecraft.world.InteractionHand; | |
import net.minecraft.world.InteractionResult; | |
import net.minecraft.world.entity.player.Player; | |
import net.minecraft.world.level.Level; | |
import net.minecraft.world.level.block.Blocks; | |
import net.minecraft.world.level.block.state.BlockState; | |
import net.minecraft.world.phys.BlockHitResult; | |
public abstract class MOMultiBlock<T extends MOBaseTile<T>> extends MORotatableBlock<T>{ | |
private boolean isParent; | |
private BlockPos parentPos; | |
public MOMultiBlock(Properties properties, Class<T> tileClass, boolean isParent, BlockPos parentPos) { | |
super(properties, tileClass); | |
this.isParent = isParent; | |
this.parentPos = parentPos; | |
} | |
@Override | |
public void onPlace(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) { | |
super.onPlace(state, level, pos, newState, isMoving); | |
if (isParent()) parentPos = pos; | |
} | |
private boolean hasFired = false; | |
@Override | |
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) { | |
if (isParent()) { | |
BlockPos offsetPos = pos; | |
for (int i = 0; i < 2; i++) { | |
offsetPos = BlockPos.of(BlockPos.offset(offsetPos.asLong(), Direction.UP)); | |
if (level.getBlockState(offsetPos).getBlock() instanceof MOMultiBlock<?>) { | |
level.setBlock(offsetPos, Blocks.AIR.defaultBlockState(), 3); | |
} | |
} | |
} | |
if (!isParent() && !hasFired) { | |
level.getBlockState(getParentPos()).onRemove(level, getParentPos(), newState, isMoving); // Call onRemove on Parent | |
level.destroyBlock(getParentPos(), true); // Kill the parent block | |
hasFired = true; | |
} | |
super.onRemove(state, level, pos, newState, isMoving); // Call super | |
} | |
@Override | |
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult ray) { | |
return MOTileHelper.getTileEntity(level, getParentPos(), MOBaseTile.class) | |
.map(tile -> tile.onActivated( | |
player, | |
hand, | |
ray.getDirection(), | |
ray.getBlockPos().getX(), ray.getBlockPos().getY(), ray.getBlockPos().getZ()) | |
).orElse(InteractionResult.PASS); | |
} | |
public void setParentPos(BlockPos parentPos) { | |
this.parentPos = parentPos; | |
} | |
public BlockPos getParentPos() { | |
return this.parentPos; | |
} | |
public void setParent(boolean parent) { | |
this.isParent = parent; | |
} | |
public boolean isParent() { | |
return this.isParent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment