TechReborn/src/main/java/techreborn/blocks/BlockPlayerDetector.java

190 lines
6.6 KiB
Java
Raw Normal View History

/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2017 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2015-11-18 21:59:08 +01:00
package techreborn.blocks;
2016-05-23 18:22:50 +02:00
import com.google.common.collect.Lists;
2016-03-13 17:08:30 +01:00
import net.minecraft.block.state.BlockStateContainer;
2015-11-23 20:51:40 +01:00
import net.minecraft.block.state.IBlockState;
2015-11-18 21:59:08 +01:00
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
2015-11-22 12:16:54 +01:00
import net.minecraft.entity.player.EntityPlayer;
2015-11-18 21:59:08 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2015-11-22 12:16:54 +01:00
import net.minecraft.tileentity.TileEntity;
2016-03-15 09:13:05 +01:00
import net.minecraft.util.EnumFacing;
2016-03-13 18:38:12 +01:00
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
2016-03-13 17:08:30 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
2016-03-13 17:08:30 +01:00
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
2016-05-22 06:51:26 +02:00
import net.minecraft.util.text.translation.I18n;
2015-11-22 12:16:54 +01:00
import net.minecraft.world.IBlockAccess;
2015-11-18 21:59:08 +01:00
import net.minecraft.world.World;
2015-11-23 20:19:18 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2017-06-14 01:49:52 +02:00
import prospector.shootingstar.ShootingStar;
import prospector.shootingstar.model.ModelCompound;
import reborncore.api.tile.IMachineGuiHandler;
2016-03-23 21:00:00 +01:00
import reborncore.common.blocks.BlockMachineBase;
2016-05-23 18:22:50 +02:00
import reborncore.common.blocks.PropertyString;
import reborncore.common.util.ArrayUtils;
2016-05-22 06:51:26 +02:00
import reborncore.common.util.ChatUtils;
2016-05-23 18:22:50 +02:00
import reborncore.common.util.StringUtils;
import techreborn.client.EGui;
2015-11-18 21:59:08 +01:00
import techreborn.client.TechRebornCreativeTab;
import techreborn.init.ModBlocks;
2016-05-22 06:51:26 +02:00
import techreborn.lib.MessageIDs;
2017-06-14 01:49:52 +02:00
import techreborn.lib.ModInfo;
2015-11-22 12:16:54 +01:00
import techreborn.tiles.TilePlayerDectector;
2015-11-18 21:59:08 +01:00
2016-05-22 06:51:26 +02:00
import java.util.List;
import java.util.Random;
2017-06-14 01:49:52 +02:00
public class BlockPlayerDetector extends BlockMachineBase {
2016-03-25 10:47:34 +01:00
public static final String[] types = new String[] { "all", "others", "you" };
2016-05-23 18:22:50 +02:00
static List<String> typeNamesList = Lists.newArrayList(ArrayUtils.arrayToLowercase(types));
2017-06-14 01:49:52 +02:00
public PropertyString TYPE;
2016-03-25 10:47:34 +01:00
2016-10-08 21:46:16 +02:00
public BlockPlayerDetector() {
2016-05-17 12:41:37 +02:00
super(true);
2016-03-25 10:47:34 +01:00
setCreativeTab(TechRebornCreativeTab.instance);
setHardness(2f);
2016-05-23 18:22:50 +02:00
this.setDefaultState(this.getDefaultState().withProperty(TYPE, "all"));
2017-06-14 01:49:52 +02:00
for (int i = 0; i < types.length; i++) {
ShootingStar.registerModel(new ModelCompound(ModInfo.MOD_ID, this, i, "machines/tier1_machines").setInvVariant("type=" + types[i]));
}
2016-03-25 10:47:34 +01:00
}
@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
return new ItemStack(ModBlocks.PLAYER_DETECTOR, typeNamesList.indexOf(state.getValue(TYPE)));
}
@Override
2016-10-08 21:46:16 +02:00
public Item getItemDropped(IBlockState state, Random rand, int fortune) {
2016-03-25 10:47:34 +01:00
return Item.getItemFromBlock(this);
}
@Override
@SideOnly(Side.CLIENT)
2017-06-11 22:22:07 +02:00
public void getSubBlocks(CreativeTabs creativeTabs, NonNullList<ItemStack> list) {
2016-10-08 21:46:16 +02:00
for (int meta = 0; meta < types.length; meta++) {
2017-06-11 22:22:07 +02:00
list.add(new ItemStack(this, 1, meta));
2016-03-25 10:47:34 +01:00
}
}
@Override
2016-10-08 21:46:16 +02:00
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
2016-03-25 10:47:34 +01:00
return new TilePlayerDectector();
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canConnectRedstone(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) {
2016-03-25 10:47:34 +01:00
return true;
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canProvidePower(IBlockState state) {
2016-03-25 10:47:34 +01:00
return true;
}
@Override
2016-10-08 21:46:16 +02:00
public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
2016-03-25 10:47:34 +01:00
TileEntity entity = blockAccess.getTileEntity(pos);
2016-10-08 21:46:16 +02:00
if (entity instanceof TilePlayerDectector) {
2016-03-25 10:47:34 +01:00
return ((TilePlayerDectector) entity).isProvidingPower() ? 15 : 0;
}
return 0;
}
@Override
2016-10-08 21:46:16 +02:00
public int getStrongPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
2016-03-25 10:47:34 +01:00
TileEntity entity = blockAccess.getTileEntity(pos);
2016-10-08 21:46:16 +02:00
if (entity instanceof TilePlayerDectector) {
2016-03-25 10:47:34 +01:00
return ((TilePlayerDectector) entity).isProvidingPower() ? 15 : 0;
}
return 0;
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
2016-10-08 21:46:16 +02:00
ItemStack stack) {
2016-03-25 10:47:34 +01:00
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
TileEntity tile = worldIn.getTileEntity(pos);
2016-10-08 21:46:16 +02:00
if (tile instanceof TilePlayerDectector) {
2016-03-25 10:47:34 +01:00
((TilePlayerDectector) tile).owenerUdid = placer.getUniqueID().toString();
}
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entityPlayer,
EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
2016-05-23 18:22:50 +02:00
String type = state.getValue(TYPE);
2016-10-08 21:46:16 +02:00
String newType = type;
2016-05-23 18:22:50 +02:00
TextFormatting color = TextFormatting.GREEN;
2016-10-08 21:46:16 +02:00
if (type.equals("all")) {
2016-05-23 18:22:50 +02:00
newType = "others";
color = TextFormatting.RED;
2016-10-08 21:46:16 +02:00
} else if (type.equals("others")) {
2016-05-23 18:22:50 +02:00
newType = "you";
color = TextFormatting.BLUE;
2016-10-08 21:46:16 +02:00
} else if (type.equals("you")) {
2016-05-23 18:22:50 +02:00
newType = "all";
2016-03-25 10:47:34 +01:00
}
2016-05-23 18:22:50 +02:00
world.setBlockState(pos, state.withProperty(TYPE, newType));
if (world.isRemote) {
2016-05-22 06:51:26 +02:00
ChatUtils.sendNoSpamMessages(MessageIDs.playerDetectorID, new TextComponentString(
2016-10-08 21:46:16 +02:00
TextFormatting.GRAY + I18n.translateToLocal("techreborn.message.detects") + " " + color
+ StringUtils.toFirstCapital(newType)));
2016-03-25 10:47:34 +01:00
}
return true;
}
2016-10-08 21:46:16 +02:00
protected BlockStateContainer createBlockState() {
2016-05-23 18:22:50 +02:00
TYPE = new PropertyString("type", types);
return new BlockStateContainer(this, TYPE);
2016-03-25 10:47:34 +01:00
}
@Override
2016-05-23 18:22:50 +02:00
public IBlockState getStateFromMeta(int meta) {
2016-10-08 21:46:16 +02:00
if (meta > types.length) {
2016-05-23 18:22:50 +02:00
meta = 0;
}
return getBlockState().getBaseState().withProperty(TYPE, typeNamesList.get(meta));
}
@Override
public int getMetaFromState(IBlockState state) {
return typeNamesList.indexOf(state.getValue(TYPE));
2016-03-25 10:47:34 +01:00
}
@Override
public IMachineGuiHandler getGui() {
return null;
}
2015-11-18 21:59:08 +01:00
}