ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockColoredLamp.java

101 lines
4.3 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockColoredLamp.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
2015-06-28 21:28:58 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockBase;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
2024-03-03 01:20:53 +01:00
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.world.level.material.MapColor;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.phys.BlockHitResult;
2015-06-28 21:28:58 +02:00
2021-02-28 15:47:54 +01:00
import java.util.HashMap;
import java.util.function.Supplier;
2021-02-26 22:15:48 +01:00
2019-05-02 09:10:29 +02:00
public class BlockColoredLamp extends BlockBase {
2021-02-28 15:47:54 +01:00
private static final HashMap<DyeColor, Supplier<Block>> COLOR_TO_LAMP = new HashMap<DyeColor, Supplier<Block>>() {{
this.put(DyeColor.WHITE, ActuallyBlocks.LAMP_WHITE);
this.put(DyeColor.ORANGE, ActuallyBlocks.LAMP_ORANGE);
this.put(DyeColor.MAGENTA, ActuallyBlocks.LAMP_MAGENTA);
this.put(DyeColor.LIGHT_BLUE, ActuallyBlocks.LAMP_LIGHT_BLUE);
this.put(DyeColor.YELLOW, ActuallyBlocks.LAMP_YELLOW);
this.put(DyeColor.LIME, ActuallyBlocks.LAMP_LIME);
this.put(DyeColor.PINK, ActuallyBlocks.LAMP_PINK);
this.put(DyeColor.GRAY, ActuallyBlocks.LAMP_GRAY);
this.put(DyeColor.LIGHT_GRAY, ActuallyBlocks.LAMP_LIGHT_GRAY);
this.put(DyeColor.CYAN, ActuallyBlocks.LAMP_CYAN);
this.put(DyeColor.PURPLE, ActuallyBlocks.LAMP_PURPLE);
this.put(DyeColor.BLUE, ActuallyBlocks.LAMP_BLUE);
this.put(DyeColor.BROWN, ActuallyBlocks.LAMP_BROWN);
this.put(DyeColor.GREEN, ActuallyBlocks.LAMP_GREEN);
this.put(DyeColor.RED, ActuallyBlocks.LAMP_RED);
this.put(DyeColor.BLACK, ActuallyBlocks.LAMP_BLACK);
}};
private static final BooleanProperty LIT = BlockStateProperties.LIT;
public BlockColoredLamp() {
2024-03-03 01:20:53 +01:00
super(Properties.of().mapColor(MapColor.NONE).instrument(NoteBlockInstrument.HAT).strength(0.5F, 3.0F).requiresCorrectToolForDrops());
this.registerDefaultState(this.stateDefinition.any().setValue(LIT, false));
2015-10-03 10:19:40 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
2021-02-28 15:47:54 +01:00
builder.add(LIT);
2015-06-28 21:28:58 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
ItemStack stack = player.getItemInHand(hand);
2015-06-28 21:28:58 +02:00
//Turning On
2024-03-02 21:23:08 +01:00
if (hand == InteractionHand.MAIN_HAND && stack.isEmpty()) {
world.setBlock(pos, this.defaultBlockState().setValue(LIT, !state.getValue(LIT)), Block.UPDATE_INVISIBLE);
return InteractionResult.PASS;
2015-06-28 21:28:58 +02:00
}
2021-02-28 15:47:54 +01:00
if (StackUtil.isValid(stack) && stack.getItem() instanceof DyeItem) {
DyeColor color = DyeColor.getColor(stack);
if (color == null) {
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
2015-06-28 21:28:58 +02:00
}
2021-02-28 15:47:54 +01:00
Block newColor = COLOR_TO_LAMP.get(color).get();
if (!world.isClientSide) {
world.setBlock(pos, newColor.defaultBlockState().setValue(LIT, state.getValue(LIT)), 2);
2021-02-28 15:47:54 +01:00
if (!player.isCreative()) {
2024-03-02 21:23:08 +01:00
player.getInventory().removeItem(player.getInventory().selected, 1);
2021-02-28 15:47:54 +01:00
}
}
2015-06-28 21:28:58 +02:00
}
return super.use(state, world, pos, player, hand, hit);
2015-06-28 21:28:58 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public int getLightEmission(BlockState state, BlockGetter level, BlockPos pos) {
return state.getValue(LIT)
2021-02-26 22:15:48 +01:00
? 15
: 0;
2015-10-03 10:19:40 +02:00
}
}