This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/PhaseShifterBlock.java

132 lines
5.5 KiB
Java

package com.thebrokenrail.energonrelics.block;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.api.block.energy.EnergyBlock;
import com.thebrokenrail.energonrelics.block.entity.shifter.PhaseShifterBlockEntity;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.HopperScreenHandler;
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.Random;
import java.util.function.Function;
@SuppressWarnings("deprecation")
public class PhaseShifterBlock extends EnergyBlock {
public static final BooleanProperty POWERED = Properties.POWERED;
public static final BooleanProperty IS_OUTPUT = BooleanProperty.of("is_output");
public static final EnumProperty<DyeColor> COLOR = EnumProperty.of("color", DyeColor.class);
public PhaseShifterBlock() {
super(FabricBlockSettings.copy(Blocks.IRON_BLOCK).lightLevel(state -> state.get(POWERED) ? 13 : 0).emissiveLighting((state, world, pos) -> state.get(POWERED)));
setDefaultState(getDefaultState().with(POWERED, false).with(IS_OUTPUT, false).with(COLOR, DyeColor.WHITE));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(POWERED, IS_OUTPUT, COLOR);
}
@Override
protected Function<BlockEntityType<BlockEntity>, BlockEntity> getFactory() {
return PhaseShifterBlockEntity::new;
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return getDefaultState().with(IS_OUTPUT, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos()));
}
@Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
if (!world.isClient()) {
boolean bl = state.get(IS_OUTPUT);
if (bl != world.isReceivingRedstonePower(pos)) {
if (bl) {
world.getBlockTickScheduler().schedule(pos, this, 4);
} else {
world.setBlockState(pos, state.cycle(IS_OUTPUT), 2);
}
}
}
}
@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
if (state.get(IS_OUTPUT) && !world.isReceivingRedstonePower(pos)) {
world.setBlockState(pos, state.cycle(IS_OUTPUT), 2);
}
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult result = super.onUse(state, world, pos, player, hand, hit);
if (result != ActionResult.PASS) {
return result;
} else {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof Inventory) {
if (!world.isClient()) {
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((i, inv, player2) -> new HopperScreenHandler(i, inv, (Inventory) entity), new TranslatableText("block." + EnergonRelics.NAMESPACE + ".phase_shifter")));
}
return ActionResult.SUCCESS;
} else {
return ActionResult.FAIL;
}
}
}
@Override
public void register(Identifier id) {
super.register(id);
UseBlockCallback.EVENT.register((player, world, hand, hit) -> {
if (!player.isSpectator() && player.shouldCancelInteraction()) {
BlockState state = world.getBlockState(hit.getBlockPos());
if (state.getBlock() == PhaseShifterBlock.this) {
ItemStack stack = player.getStackInHand(hand);
if (stack.getItem() instanceof DyeItem) {
DyeColor newColor = ((DyeItem) stack.getItem()).getColor();
if (state.get(PhaseShifterBlock.COLOR) != newColor) {
world.setBlockState(hit.getBlockPos(), state.with(PhaseShifterBlock.COLOR, newColor));
if (!player.isCreative()) {
stack.decrement(1);
}
return ActionResult.SUCCESS;
}
}
}
}
return ActionResult.PASS;
});
}
@Override
protected boolean hasInventory() {
return true;
}
}