2017-10-04 17:05:04 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
2018-02-28 21:06:33 +01:00
|
|
|
* Copyright (C) 2016-2018 malte0811
|
2017-10-04 17:05:04 +02:00
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
* Industrial Wires is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-09-03 20:35:23 +02:00
|
|
|
package malte0811.industrialwires.util;
|
2017-10-04 17:05:04 +02:00
|
|
|
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
2018-02-17 20:37:53 +01:00
|
|
|
import net.minecraft.entity.Entity;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-02-17 20:37:53 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2018-03-02 22:05:27 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
public class LocalSidedWorld {
|
2018-02-08 22:08:21 +01:00
|
|
|
private World world;
|
2017-10-04 17:05:04 +02:00
|
|
|
private BlockPos origin;
|
|
|
|
private EnumFacing facing;
|
|
|
|
private boolean mirror;
|
2018-03-02 22:05:27 +01:00
|
|
|
public boolean isRemote;
|
2018-02-28 21:03:26 +01:00
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
public LocalSidedWorld(World world, BlockPos origin, EnumFacing facing, boolean mirror) {
|
|
|
|
this.world = world;
|
|
|
|
this.facing = facing;
|
|
|
|
this.mirror = mirror;
|
|
|
|
this.origin = origin;
|
2018-03-02 22:05:27 +01:00
|
|
|
if (world!=null) {
|
|
|
|
isRemote = world.isRemote;
|
|
|
|
}
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:37:53 +01:00
|
|
|
public IBlockState getBlockState(BlockPos pos) {
|
|
|
|
return world.getBlockState(getRealPos(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
public TileEntity getTileEntity(BlockPos pos) {
|
|
|
|
return world.getTileEntity(getRealPos(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAir(BlockPos pos) {
|
|
|
|
return world.isAirBlock(getRealPos(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setBlockState(BlockPos pos, IBlockState setTo) {
|
|
|
|
return world.setBlockState(getRealPos(pos), setTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void spawnEntity(Entity e) {
|
2018-05-26 18:18:53 +02:00
|
|
|
Vec3d posTransf = e.getPositionVector();
|
|
|
|
Vec3d pos = getRealPos(posTransf);
|
2018-02-17 20:37:53 +01:00
|
|
|
e.setPosition(pos.x, pos.y, pos.z);
|
|
|
|
Vec3d motion = getRealDirection(new Vec3d(e.motionX, e.motionY, e.motionZ));
|
|
|
|
e.motionX = motion.x;
|
|
|
|
e.motionY = motion.y;
|
|
|
|
e.motionZ = motion.z;
|
|
|
|
world.spawnEntity(e);
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:03:26 +01:00
|
|
|
public void setTileEntity(BlockPos pos, TileEntity setTo) {
|
|
|
|
world.setTileEntity(getRealPos(pos), setTo);
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:05:27 +01:00
|
|
|
|
|
|
|
public void markForUpdate(BlockPos rel) {
|
|
|
|
BlockPos pos = getRealPos(rel);
|
|
|
|
IBlockState state = world.getBlockState(pos);
|
|
|
|
world.notifyBlockUpdate(pos, state, state, 3);
|
|
|
|
world.notifyNeighborsOfStateChange(pos, state.getBlock(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Transformation utils
|
2018-02-17 20:37:53 +01:00
|
|
|
public BlockPos getRealPos(BlockPos relative) {
|
|
|
|
return MiscUtils.offset(origin, facing, mirror, relative);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vec3d getRealPos(Vec3d relative) {
|
2018-05-26 18:18:53 +02:00
|
|
|
return MiscUtils.offset(new Vec3d(origin.getX()+.5, origin.getY()+.5, origin.getZ()+.5), facing, mirror,
|
2018-11-17 12:23:31 +01:00
|
|
|
relative.add(.5, .5, .5));
|
2018-02-17 20:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vec3d getRealDirection(Vec3d dir) {
|
|
|
|
return MiscUtils.offset(Vec3d.ZERO, facing, mirror, dir);
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:05:27 +01:00
|
|
|
public EnumFacing realToTransformed(@Nullable EnumFacing f) {
|
|
|
|
if (f==null||f.getAxis()== EnumFacing.Axis.Y) {
|
|
|
|
return f;
|
|
|
|
}
|
2018-03-27 21:31:03 +02:00
|
|
|
//+6 because getHorizontal uses abs(input%3). No idea why.
|
2018-11-17 12:23:31 +01:00
|
|
|
return EnumFacing.byHorizontalIndex(f.getHorizontalIndex() - facing.getHorizontalIndex() + 6);
|
2018-03-02 22:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public EnumFacing transformedToReal(@Nullable EnumFacing f) {
|
|
|
|
if (f==null||f.getAxis()== EnumFacing.Axis.Y) {
|
|
|
|
return f;
|
|
|
|
}
|
2018-11-17 12:23:31 +01:00
|
|
|
return EnumFacing.byHorizontalIndex(f.getHorizontalIndex() + facing.getHorizontalIndex() + 2);
|
2018-03-02 22:05:27 +01:00
|
|
|
}
|
2018-02-28 21:03:26 +01:00
|
|
|
|
2018-02-17 20:37:53 +01:00
|
|
|
//Getters+Setters
|
2017-10-04 17:05:04 +02:00
|
|
|
public World getWorld() {
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFacing(EnumFacing facing) {
|
|
|
|
this.facing = facing;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOrigin(BlockPos origin) {
|
|
|
|
this.origin = origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMirror(boolean mirror) {
|
|
|
|
this.mirror = mirror;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockPos getOrigin() {
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:37:53 +01:00
|
|
|
public void setWorld(World world) {
|
|
|
|
this.world = world;
|
2018-03-02 22:05:27 +01:00
|
|
|
if (world!=null) {
|
|
|
|
isRemote = world.isRemote;
|
|
|
|
}
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:37:53 +01:00
|
|
|
public boolean isMirrored() {
|
|
|
|
return mirror;
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|
2018-02-08 22:08:21 +01:00
|
|
|
|
2018-02-17 20:37:53 +01:00
|
|
|
public EnumFacing getFacing() {
|
|
|
|
return facing;
|
2018-02-08 22:08:21 +01:00
|
|
|
}
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|