TechReborn/src/main/java/techreborn/tiles/TilePlayerDectector.java

130 lines
3.8 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-22 12:16:54 +01:00
package techreborn.tiles;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
2015-11-23 15:45:16 +01:00
import net.minecraft.util.EnumFacing;
import reborncore.api.power.EnumPowerTier;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.powerSystem.TilePowerAcceptor;
2016-03-13 18:38:12 +01:00
import reborncore.common.util.WorldUtils;
2015-11-22 12:16:54 +01:00
import java.util.Iterator;
2016-10-08 21:46:16 +02:00
public class TilePlayerDectector extends TilePowerAcceptor {
2016-03-25 10:47:34 +01:00
public String owenerUdid = "";
boolean redstone = false;
2016-10-08 21:46:16 +02:00
public TilePlayerDectector() {
super(1);
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxPower() {
return 10000;
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canAcceptEnergy(EnumFacing direction) {
return true;
}
2016-03-25 10:47:34 +01:00
@Override
2016-10-08 21:46:16 +02:00
public boolean canProvideEnergy(EnumFacing direction) {
return false;
2016-03-25 10:47:34 +01:00
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxOutput() {
return 0;
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxInput() {
return 32;
}
@Override
2017-04-11 20:12:32 +02:00
public EnumPowerTier getBaseTier() {
2016-03-25 10:47:34 +01:00
return EnumPowerTier.LOW;
}
@Override
2016-10-08 21:46:16 +02:00
public void updateEntity() {
super.updateEntity();
2016-11-19 13:50:08 +01:00
if (!world.isRemote && world.getWorldTime() % 20 == 0) {
2016-03-25 10:47:34 +01:00
boolean lastRedstone = redstone;
redstone = false;
2016-10-08 21:46:16 +02:00
if (canUseEnergy(10)) {
2016-11-19 13:50:08 +01:00
Iterator tIterator = super.world.playerEntities.iterator();
2016-10-08 21:46:16 +02:00
while (tIterator.hasNext()) {
2016-03-25 10:47:34 +01:00
EntityPlayer player = (EntityPlayer) tIterator.next();
if (player.getDistanceSq((double) super.getPos().getX() + 0.5D,
2016-10-08 21:46:16 +02:00
(double) super.getPos().getY() + 0.5D, (double) super.getPos().getZ() + 0.5D) <= 256.0D) {
2016-11-19 13:50:08 +01:00
BlockMachineBase blockMachineBase = (BlockMachineBase) world.getBlockState(pos).getBlock();
int meta = blockMachineBase.getMetaFromState(world.getBlockState(pos));
2016-10-08 21:46:16 +02:00
if (meta == 0) {// ALL
2016-03-25 10:47:34 +01:00
redstone = true;
2016-10-08 21:46:16 +02:00
} else if (meta == 1) {// Others
if (!owenerUdid.isEmpty() && !owenerUdid.equals(player.getUniqueID().toString())) {
2016-03-25 10:47:34 +01:00
redstone = true;
}
2016-10-08 21:46:16 +02:00
} else {// You
if (!owenerUdid.isEmpty() && owenerUdid.equals(player.getUniqueID().toString())) {
2016-03-25 10:47:34 +01:00
redstone = true;
}
}
redstone = true;
}
}
2016-05-23 18:05:10 +02:00
useEnergy(10);
2016-03-25 10:47:34 +01:00
}
2016-10-08 21:46:16 +02:00
if (lastRedstone != redstone) {
2016-11-19 13:50:08 +01:00
WorldUtils.updateBlock(world, getPos());
world.notifyNeighborsOfStateChange(getPos(), world.getBlockState(getPos()).getBlock(), true);
2016-03-25 10:47:34 +01:00
}
}
}
2016-10-08 21:46:16 +02:00
public boolean isProvidingPower() {
2016-03-25 10:47:34 +01:00
return redstone;
}
@Override
2016-10-08 21:46:16 +02:00
public void readFromNBT(NBTTagCompound tag) {
2016-03-25 10:47:34 +01:00
super.readFromNBT(tag);
owenerUdid = tag.getString("ownerID");
}
@Override
2016-10-08 21:46:16 +02:00
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
2016-03-25 10:47:34 +01:00
super.writeToNBT(tag);
tag.setString("ownerID", owenerUdid);
2016-05-18 17:53:54 +02:00
return tag;
2016-03-25 10:47:34 +01:00
}
2015-11-22 12:16:54 +01:00
}