2020-08-16 03:01:24 +00:00
|
|
|
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;
|
2020-08-16 15:24:03 +00:00
|
|
|
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
|
2020-08-16 03:01:24 +00:00
|
|
|
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;
|
2020-08-16 15:24:03 +00:00
|
|
|
import net.minecraft.util.Identifier;
|
2020-08-16 03:01:24 +00:00
|
|
|
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) {
|
2020-08-16 13:43:06 +00:00
|
|
|
ActionResult result = super.onUse(state, world, pos, player, hand, hit);
|
|
|
|
if (result != ActionResult.PASS) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
BlockEntity entity = world.getBlockEntity(pos);
|
2020-08-16 15:24:03 +00:00
|
|
|
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")));
|
2020-08-16 03:01:24 +00:00
|
|
|
}
|
2020-08-16 15:24:03 +00:00
|
|
|
return ActionResult.SUCCESS;
|
2020-08-16 03:01:24 +00:00
|
|
|
} else {
|
2020-08-16 15:24:03 +00:00
|
|
|
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());
|
|
|
|
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;
|
2020-08-16 13:43:06 +00:00
|
|
|
}
|
2020-08-16 03:01:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 15:24:03 +00:00
|
|
|
return ActionResult.PASS;
|
|
|
|
});
|
2020-08-16 03:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean hasInventory() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|