mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-09-27 11:21:05 +02:00
131 lines
5.5 KiB
Java
131 lines
5.5 KiB
Java
/*
|
|
* This file ("BlockColoredLamp.java") is part of the Actually Additions mod for Minecraft.
|
|
* It is created and owned by Ellpeck and distributed
|
|
* under the Actually Additions License to be found at
|
|
* http://ellpeck.de/actaddlicense
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
*
|
|
* © 2015-2017 Ellpeck
|
|
*/
|
|
|
|
package de.ellpeck.actuallyadditions.mod.blocks;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockBase;
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.material.Material;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.item.DyeColor;
|
|
import net.minecraft.item.DyeItem;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.state.BooleanProperty;
|
|
import net.minecraft.state.StateContainer;
|
|
import net.minecraft.state.properties.BlockStateProperties;
|
|
import net.minecraft.util.ActionResultType;
|
|
import net.minecraft.util.Hand;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.BlockRayTraceResult;
|
|
import net.minecraft.world.IBlockReader;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.common.ToolType;
|
|
import net.minecraftforge.common.util.Constants;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.function.Supplier;
|
|
|
|
public class BlockColoredLamp extends BlockBase {
|
|
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() {
|
|
super(Properties.create(Material.REDSTONE_LIGHT).hardnessAndResistance(0.5F, 3.0F).harvestTool(ToolType.PICKAXE).harvestLevel(0));
|
|
this.setDefaultState(this.stateContainer.getBaseState().with(LIT, false));
|
|
}
|
|
|
|
@Override
|
|
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
|
|
builder.add(LIT);
|
|
}
|
|
|
|
// TODO: [port][test] validate this rework works
|
|
@Override
|
|
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
|
|
ItemStack stack = player.getHeldItem(hand);
|
|
//Turning On
|
|
if (hand == Hand.MAIN_HAND && stack.isEmpty()) {
|
|
world.setBlockState(pos, this.getDefaultState().with(LIT, !state.get(LIT)), Constants.BlockFlags.NO_RERENDER);
|
|
return ActionResultType.PASS;
|
|
}
|
|
|
|
if (StackUtil.isValid(stack) && stack.getItem() instanceof DyeItem) {
|
|
DyeColor color = DyeColor.getColor(stack);
|
|
if (color == null) {
|
|
return ActionResultType.FAIL;
|
|
}
|
|
|
|
Block newColor = COLOR_TO_LAMP.get(color).get();
|
|
if (!world.isRemote) {
|
|
world.setBlockState(pos, newColor.getDefaultState().with(LIT, state.get(LIT)), 2);
|
|
if (!player.isCreative()) {
|
|
player.inventory.decrStackSize(player.inventory.currentItem, 1);
|
|
}
|
|
}
|
|
}
|
|
return super.onBlockActivated(state, world, pos, player, hand, hit);
|
|
}
|
|
|
|
@Override
|
|
public int getLightValue(BlockState state, IBlockReader world, BlockPos pos) {
|
|
return state.get(LIT)
|
|
? 15
|
|
: 0;
|
|
}
|
|
|
|
// TODO: [port] Add this back correctly
|
|
// public static class TheItemBlock extends ItemBlockBase {
|
|
//
|
|
// public TheItemBlock(Block block) {
|
|
// super(block);
|
|
// this.setHasSubtypes(true);
|
|
// this.setMaxDamage(0);
|
|
// }
|
|
//
|
|
// @Override
|
|
// public String getItemStackDisplayName(ItemStack stack) {
|
|
// if (stack.getItemDamage() >= ALL_LAMP_TYPES.length) {
|
|
// return StringUtil.BUGGED_ITEM_NAME;
|
|
// }
|
|
// if (Util.isClient()) {
|
|
// return super.getItemStackDisplayName(stack) + (((BlockColoredLamp) this.block).isOn
|
|
// ? " (" + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".onSuffix.desc") + ")"
|
|
// : "");
|
|
// } else {
|
|
// return super.getItemStackDisplayName(stack);
|
|
// }
|
|
// }
|
|
//
|
|
// @Override
|
|
// public String getTranslationKey(ItemStack stack) {
|
|
// return ActuallyBlocks.blockColoredLamp.getTranslationKey() + "_" + ALL_LAMP_TYPES[stack.getItemDamage()].regName;
|
|
// }
|
|
// }
|
|
}
|