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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package malte0811.industrialWires.converter;
|
|
|
|
|
2018-06-02 21:22:26 +02:00
|
|
|
import blusunrize.immersiveengineering.api.MultiblockHandler;
|
2017-10-04 17:05:04 +02:00
|
|
|
import blusunrize.immersiveengineering.common.IEContent;
|
|
|
|
import blusunrize.immersiveengineering.common.blocks.metal.BlockTypes_MetalDecoration0;
|
2018-05-12 19:04:38 +02:00
|
|
|
import blusunrize.immersiveengineering.common.util.Utils;
|
|
|
|
import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4;
|
2017-10-04 17:05:04 +02:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
|
|
|
import malte0811.industrialWires.blocks.converter.MechanicalMBBlockType;
|
2018-04-22 22:14:08 +02:00
|
|
|
import malte0811.industrialWires.blocks.converter.TileEntityMechMB;
|
2018-05-13 18:54:26 +02:00
|
|
|
import malte0811.industrialWires.client.render.TileRenderMechMB;
|
2018-05-12 19:04:38 +02:00
|
|
|
import malte0811.industrialWires.entities.EntityBrokenPart;
|
2017-10-04 17:05:04 +02:00
|
|
|
import malte0811.industrialWires.util.LocalSidedWorld;
|
2018-03-23 16:27:32 +01:00
|
|
|
import malte0811.industrialWires.util.MiscUtils;
|
2018-06-02 21:22:26 +02:00
|
|
|
import malte0811.industrialWires.util.MultiblockTemplateManual;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.client.renderer.block.model.BakedQuad;
|
2018-05-14 18:51:42 +02:00
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2018-02-08 22:08:21 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2018-04-04 20:47:44 +02:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-05-12 19:04:38 +02:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2018-02-08 22:08:21 +01:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2017-10-04 17:05:04 +02:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
|
2018-03-23 16:27:32 +01:00
|
|
|
import java.util.Comparator;
|
2017-10-04 17:05:04 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
2018-03-23 16:27:32 +01:00
|
|
|
import static blusunrize.immersiveengineering.common.IEContent.blockMetalDecoration0;
|
|
|
|
import static blusunrize.immersiveengineering.common.blocks.metal.BlockTypes_MetalDecoration0.HEAVY_ENGINEERING;
|
|
|
|
import static blusunrize.immersiveengineering.common.blocks.metal.BlockTypes_MetalDecoration0.LIGHT_ENGINEERING;
|
2018-06-02 21:22:26 +02:00
|
|
|
import static malte0811.industrialWires.IndustrialWires.MODID;
|
2017-10-04 17:05:04 +02:00
|
|
|
import static malte0811.industrialWires.blocks.converter.MechanicalMBBlockType.NO_MODEL;
|
|
|
|
import static malte0811.industrialWires.util.NBTKeys.TYPE;
|
|
|
|
|
|
|
|
public abstract class MechMBPart {
|
|
|
|
public static final Map<String, MechMBPart> INSTANCES = new HashMap<>();
|
2018-02-08 22:08:21 +01:00
|
|
|
public LocalSidedWorld world;
|
2018-05-14 18:51:42 +02:00
|
|
|
protected Map<BlockPos, IBlockState> original = new HashMap<>();
|
2017-10-04 17:05:04 +02:00
|
|
|
|
|
|
|
// These 3 are called once per tick in bulk in this order
|
2018-02-08 22:08:21 +01:00
|
|
|
public abstract void createMEnergy(MechEnergy e);
|
|
|
|
public abstract double requestMEnergy(MechEnergy e);
|
2017-10-04 17:05:04 +02:00
|
|
|
// This should do any misc ticking as well
|
2018-02-08 22:08:21 +01:00
|
|
|
public abstract void insertMEnergy(double added);
|
2017-10-04 17:05:04 +02:00
|
|
|
|
2018-02-08 22:08:21 +01:00
|
|
|
public abstract double getInertia();
|
2018-03-23 16:27:32 +01:00
|
|
|
public abstract double getMaxSpeed();
|
2017-10-04 17:05:04 +02:00
|
|
|
public abstract void writeToNBT(NBTTagCompound out);
|
2018-02-28 21:03:26 +01:00
|
|
|
public abstract void readFromNBT(NBTTagCompound in);
|
2017-10-04 17:05:04 +02:00
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public List<BakedQuad> getRotatingQuads() {
|
2018-05-13 18:54:26 +02:00
|
|
|
return TileRenderMechMB.BASE_MODELS.get(getRotatingBaseModel())
|
2017-10-04 17:05:04 +02:00
|
|
|
.getQuads(null, null, 123);
|
|
|
|
|
|
|
|
}
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public abstract ResourceLocation getRotatingBaseModel();
|
|
|
|
|
|
|
|
public abstract boolean canForm(LocalSidedWorld w);
|
|
|
|
|
2018-03-23 16:27:32 +01:00
|
|
|
protected boolean hasSupportPillars(LocalSidedWorld w) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
IBlockState state = w.getBlockState(new BlockPos(2*i-1, j-1, 0));
|
|
|
|
if (!isLightEngineering(state)) {
|
2018-03-24 17:45:25 +01:00
|
|
|
return false;
|
2018-03-23 16:27:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 17:45:25 +01:00
|
|
|
return true;
|
2018-03-23 16:27:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 18:51:42 +02:00
|
|
|
public abstract short getFormPattern(int offset);
|
2017-10-04 17:05:04 +02:00
|
|
|
|
2018-05-14 18:51:42 +02:00
|
|
|
public abstract void breakOnFailure(MechEnergy energy);
|
|
|
|
|
|
|
|
public ItemStack getOriginalItem(BlockPos pos) {
|
|
|
|
IBlockState state = getOriginalBlock(pos);
|
|
|
|
return new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state));
|
|
|
|
}
|
|
|
|
|
|
|
|
public IBlockState getOriginalBlock(BlockPos pos) {
|
|
|
|
return original.getOrDefault(pos, Blocks.AIR.getDefaultState());
|
|
|
|
}
|
2018-02-17 20:37:53 +01:00
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
public abstract MechanicalMBBlockType getType();
|
|
|
|
|
2018-03-24 17:45:25 +01:00
|
|
|
public <T> boolean hasCapability(Capability<T> cap, EnumFacing side, BlockPos pos) {
|
2018-02-08 22:08:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-24 17:45:25 +01:00
|
|
|
public <T> T getCapability(Capability<T> cap, EnumFacing side, BlockPos pos) {
|
2018-02-08 22:08:21 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:27:32 +01:00
|
|
|
public static final BiMap<String, Class<? extends MechMBPart>> REGISTRY = HashBiMap.create();
|
|
|
|
|
2018-04-04 20:47:44 +02:00
|
|
|
public static final String SHAFT_KEY = "shaft";
|
2018-06-02 21:22:26 +02:00
|
|
|
public static final ResourceLocation EXAMPLE_MECHMB_LOC = new ResourceLocation(MODID, "example_mech_mb");
|
2018-04-04 20:47:44 +02:00
|
|
|
|
2018-05-14 18:51:42 +02:00
|
|
|
public static final Comparator<MechMBPart> SORT_BY_COUNT = (a, b)-> {
|
|
|
|
if (a.getLength()!=b.getLength()) {
|
|
|
|
return Integer.compare(a.getLength(), b.getLength());
|
|
|
|
}
|
|
|
|
for (int i = 0;i<a.getLength();i++) {
|
|
|
|
int aBits = MiscUtils.count1Bits(a.getFormPattern(i));
|
|
|
|
int bBits = MiscUtils.count1Bits(b.getFormPattern(i));
|
|
|
|
if (aBits!=bBits) {
|
|
|
|
return Integer.compare(aBits, bBits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
2018-02-28 21:03:26 +01:00
|
|
|
public static void preInit() {
|
2017-10-04 17:05:04 +02:00
|
|
|
REGISTRY.put("flywheel", MechPartFlywheel.class);
|
|
|
|
REGISTRY.put("singleCoil", MechPartSingleCoil.class);
|
2018-02-28 21:03:26 +01:00
|
|
|
REGISTRY.put("twoElectrodes", MechPartTwoElectrodes.class);
|
|
|
|
REGISTRY.put("commutator", MechPartCommutator.class);
|
2018-04-04 20:47:44 +02:00
|
|
|
REGISTRY.put(SHAFT_KEY, MechPartShaft.class);
|
2018-03-02 22:05:27 +01:00
|
|
|
REGISTRY.put("speedometer", MechPartSpeedometer.class);
|
2018-03-23 16:27:32 +01:00
|
|
|
REGISTRY.put("commFour", MechPartCommutator4Phase.class);
|
|
|
|
REGISTRY.put("fourCoils", MechPartFourCoils.class);
|
|
|
|
REGISTRY.put("fourElectrodes", MechPartFourElectrodes.class);
|
2017-10-04 17:05:04 +02:00
|
|
|
|
|
|
|
for (String key : REGISTRY.keySet()) {
|
|
|
|
cacheNewInstance(key);
|
2018-06-02 21:22:26 +02:00
|
|
|
MultiblockHandler.registerMultiblock(
|
|
|
|
new MultiblockTemplateManual(getSchematicLocationForPart(key)));
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 21:22:26 +02:00
|
|
|
public static ResourceLocation getSchematicLocationForPart(Class<? extends MechMBPart> cl) {
|
|
|
|
String name = REGISTRY.inverse().get(cl);
|
|
|
|
return getSchematicLocationForPart(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ResourceLocation getSchematicLocationForPart(String name) {
|
|
|
|
if (name==null)
|
|
|
|
return null;
|
|
|
|
name = MiscUtils.toSnakeCase(name);
|
|
|
|
return new ResourceLocation(MODID, name);
|
|
|
|
}
|
|
|
|
|
2018-06-04 18:10:52 +02:00
|
|
|
public static MultiblockHandler.IMultiblock getManualMBForPart(Class<? extends MechMBPart> cl) {
|
|
|
|
return MiscUtils.getMBFromName(getSchematicLocationForPart(cl).toString());
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
public static void cacheNewInstance(String key) {
|
|
|
|
try {
|
|
|
|
MechMBPart instance = REGISTRY.get(key).newInstance();
|
|
|
|
INSTANCES.put(key, instance);
|
|
|
|
} catch (IllegalAccessException | InstantiationException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:08:21 +01:00
|
|
|
public static MechMBPart fromNBT(NBTTagCompound nbt, LocalSidedWorld w) {
|
2017-10-04 17:05:04 +02:00
|
|
|
String name = nbt.getString(TYPE);
|
|
|
|
Class<? extends MechMBPart> clazz = REGISTRY.get(name);
|
|
|
|
try {
|
|
|
|
MechMBPart ret = clazz.newInstance();
|
|
|
|
ret.readFromNBT(nbt);
|
2018-02-08 22:08:21 +01:00
|
|
|
if (w==null)
|
|
|
|
throw new NullPointerException();
|
|
|
|
ret.world = w;
|
2017-10-04 17:05:04 +02:00
|
|
|
return ret;
|
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
throw new RuntimeException("While creating mechanical MB part", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static NBTTagCompound toNBT(MechMBPart part) {
|
|
|
|
Class<? extends MechMBPart> clazz = part.getClass();
|
|
|
|
String name = REGISTRY.inverse().get(clazz);
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
part.writeToNBT(nbt);
|
|
|
|
nbt.setString(TYPE, name);
|
|
|
|
return nbt;
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:37:53 +01:00
|
|
|
public static boolean isValidDefaultCenter(IBlockState state) {
|
2018-03-23 16:27:32 +01:00
|
|
|
return state.getBlock()== IEContent.blockMetalDecoration0&&
|
|
|
|
state.getValue(IEContent.blockMetalDecoration0.property)==BlockTypes_MetalDecoration0.HEAVY_ENGINEERING;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isHeavyEngineering(IBlockState state) {
|
|
|
|
return isValidDefaultCenter(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isLightEngineering(IBlockState state) {
|
2017-10-04 17:05:04 +02:00
|
|
|
return state.getBlock()== IEContent.blockMetalDecoration0&&
|
|
|
|
state.getValue(IEContent.blockMetalDecoration0.property)==BlockTypes_MetalDecoration0.LIGHT_ENGINEERING;
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:51:42 +02:00
|
|
|
public IBlockState getDefaultShaft() {
|
|
|
|
return blockMetalDecoration0.getDefaultState().withProperty(blockMetalDecoration0.property,
|
|
|
|
HEAVY_ENGINEERING);
|
2018-03-23 16:27:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 18:51:42 +02:00
|
|
|
public IBlockState getLightEngineering() {
|
|
|
|
return blockMetalDecoration0.getDefaultState().withProperty(blockMetalDecoration0.property,
|
|
|
|
LIGHT_ENGINEERING);
|
2018-03-23 16:27:32 +01:00
|
|
|
}
|
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
|
2018-04-22 22:14:08 +02:00
|
|
|
public void form(LocalSidedWorld w, Consumer<TileEntityMechMB> initializer) {
|
2018-02-08 22:08:21 +01:00
|
|
|
world = w;
|
2017-10-04 17:05:04 +02:00
|
|
|
BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain();
|
2018-05-14 18:51:42 +02:00
|
|
|
for (int z = 0;z<getLength();z++) {
|
|
|
|
short pattern = getFormPattern(z);
|
|
|
|
int i = 0;
|
|
|
|
for (int y = -1; y <= 1; y++) {
|
|
|
|
for (int x = -1; x <= 1; x++) {
|
|
|
|
if ((pattern & (1 << i)) != 0) {
|
|
|
|
pos.setPos(x, y, -z);
|
|
|
|
w.setBlockState(pos, IndustrialWires.mechanicalMB.getStateFromMeta((i == 4 ? getType() : NO_MODEL).ordinal()));
|
|
|
|
TileEntity te = w.getTileEntity(pos);
|
|
|
|
if (te instanceof TileEntityMechMB) {
|
|
|
|
initializer.accept((TileEntityMechMB) te);
|
|
|
|
}
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|
2018-05-14 18:51:42 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disassemble() {
|
|
|
|
BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain();
|
|
|
|
for (int z = 0;z<getLength();z++) {
|
|
|
|
for (int x = -1; x <= 1; x++) {
|
|
|
|
for (int y = -1; y <= 1; y++) {
|
|
|
|
pos.setPos(x, y, -z);
|
|
|
|
world.setBlockState(pos, getOriginalBlock(pos));
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos.release();
|
|
|
|
}
|
|
|
|
|
2018-05-12 19:04:38 +02:00
|
|
|
protected void spawnBrokenParts(int count, MechEnergy energy, ResourceLocation texture) {
|
|
|
|
Matrix4 mat = new Matrix4();
|
|
|
|
mat.rotate(Utils.RAND.nextDouble(), 0, 0, 1);
|
|
|
|
Vec3d baseVec = new Vec3d(0, 1.5, 0);
|
|
|
|
for (int i = 0;i<count;i++) {
|
|
|
|
mat.rotate(2*Math.PI / count, 0, 0, 1);
|
|
|
|
Vec3d pos = mat.apply(baseVec);
|
|
|
|
EntityBrokenPart e = new EntityBrokenPart(world.getWorld(), texture);
|
|
|
|
e.setPosition(pos.x, pos.y, -.5);
|
|
|
|
double speed = (energy.getSpeed() / getMaxSpeed()) / 1.5;
|
|
|
|
e.motionX = pos.y * speed;
|
|
|
|
e.motionY = -pos.x * speed;
|
|
|
|
e.motionZ = (Utils.RAND.nextDouble() - .5) * speed / 10;
|
|
|
|
world.spawnEntity(e);
|
|
|
|
e.breakBlocks(speed * speed * 1.5 * 1.5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
public int getLength() {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-04-04 20:47:44 +02:00
|
|
|
|
|
|
|
public abstract AxisAlignedBB getBoundingBox(BlockPos offsetPart);
|
2017-10-04 17:05:04 +02:00
|
|
|
}
|