Updated force field projector block to support the new model
- renamed isOn to isActive for consistency - replaced isPowered by isActive in state LUA API Fixed invalid is_double_sided projector property
This commit is contained in:
parent
4d41580f42
commit
1c92f9f12e
7 changed files with 51 additions and 409 deletions
src/main/java/cr0s/warpdrive
|
@ -5,14 +5,11 @@ import cr0s.warpdrive.api.WarpDriveText;
|
|||
import cr0s.warpdrive.block.TileEntityAbstractBase.UpgradeSlot;
|
||||
import cr0s.warpdrive.data.BlockProperties;
|
||||
import cr0s.warpdrive.data.EnumForceFieldShape;
|
||||
import cr0s.warpdrive.data.EnumForceFieldState;
|
||||
import cr0s.warpdrive.data.EnumTier;
|
||||
import cr0s.warpdrive.item.ItemForceFieldShape;
|
||||
import cr0s.warpdrive.item.ItemForceFieldUpgrade;
|
||||
import cr0s.warpdrive.render.TileEntityForceFieldProjectorRenderer;
|
||||
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.properties.IProperty;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
|
@ -36,19 +33,13 @@ import net.minecraft.world.World;
|
|||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.common.property.ExtendedBlockState;
|
||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||
import net.minecraftforge.common.property.IUnlistedProperty;
|
||||
import net.minecraftforge.common.property.Properties;
|
||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
public class BlockForceFieldProjector extends BlockAbstractForceField {
|
||||
|
||||
public static final PropertyBool IS_DOUBLE_SIDED = PropertyBool.create("is_double_sided");
|
||||
public static final IUnlistedProperty<EnumForceFieldShape> SHAPE = Properties.toUnlisted(PropertyEnum.create("shape", EnumForceFieldShape.class));
|
||||
public static final IUnlistedProperty<EnumForceFieldState> STATE = Properties.toUnlisted(PropertyEnum.create("state", EnumForceFieldState.class));
|
||||
public static final PropertyEnum<EnumForceFieldShape> SHAPE = PropertyEnum.create("shape", EnumForceFieldShape.class);
|
||||
|
||||
private static final AxisAlignedBB AABB_DOWN = new AxisAlignedBB(0.00D, 0.27D, 0.00D, 1.00D, 0.73D, 1.00D);
|
||||
private static final AxisAlignedBB AABB_UP = new AxisAlignedBB(0.00D, 0.27D, 0.00D, 1.00D, 0.73D, 1.00D);
|
||||
|
@ -63,17 +54,17 @@ public class BlockForceFieldProjector extends BlockAbstractForceField {
|
|||
setTranslationKey("warpdrive.force_field.projector." + enumTier.getName());
|
||||
|
||||
setDefaultState(getDefaultState()
|
||||
.withProperty(BlockProperties.ACTIVE, false)
|
||||
.withProperty(BlockProperties.FACING, EnumFacing.DOWN)
|
||||
.withProperty(IS_DOUBLE_SIDED, false)
|
||||
.withProperty(SHAPE, EnumForceFieldShape.NONE)
|
||||
);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
protected BlockStateContainer createBlockState() {
|
||||
return new ExtendedBlockState(this,
|
||||
new IProperty[] { BlockProperties.FACING, IS_DOUBLE_SIDED },
|
||||
new IUnlistedProperty[] { SHAPE, STATE });
|
||||
return new BlockStateContainer(this, BlockProperties.ACTIVE, BlockProperties.FACING, IS_DOUBLE_SIDED, SHAPE);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
|
@ -91,33 +82,18 @@ public class BlockForceFieldProjector extends BlockAbstractForceField {
|
|||
+ (blockState.getValue(IS_DOUBLE_SIDED) ? 8 : 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Nonnull
|
||||
@Override
|
||||
public IBlockState getExtendedState(@Nonnull final IBlockState blockState, final IBlockAccess blockAccess, final BlockPos blockPos) {
|
||||
if (!(blockState instanceof IExtendedBlockState)) {
|
||||
public IBlockState getActualState(@Nonnull final IBlockState blockState, @Nonnull final IBlockAccess blockAccess, @Nonnull final BlockPos blockPos) {
|
||||
final TileEntity tileEntity = blockAccess.getTileEntity(blockPos);
|
||||
if (tileEntity instanceof TileEntityForceFieldProjector) {
|
||||
return blockState
|
||||
.withProperty(BlockProperties.ACTIVE, ((TileEntityForceFieldProjector) tileEntity).isActive())
|
||||
.withProperty(SHAPE, ((TileEntityForceFieldProjector) tileEntity).getShape());
|
||||
} else {
|
||||
return blockState;
|
||||
}
|
||||
final TileEntity tileEntity = blockAccess.getTileEntity(blockPos);
|
||||
EnumForceFieldShape forceFieldShape = EnumForceFieldShape.NONE;
|
||||
EnumForceFieldState forceFieldState = EnumForceFieldState.NOT_CONNECTED;
|
||||
if (tileEntity instanceof TileEntityForceFieldProjector) {
|
||||
final TileEntityForceFieldProjector tileEntityForceFieldProjector = (TileEntityForceFieldProjector) tileEntity;
|
||||
forceFieldShape = tileEntityForceFieldProjector.getShape();
|
||||
forceFieldState = tileEntityForceFieldProjector.getState();
|
||||
}
|
||||
|
||||
return ((IExtendedBlockState) blockState)
|
||||
.withProperty(SHAPE, forceFieldShape)
|
||||
.withProperty(STATE, forceFieldState);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public void modelInitialisation() {
|
||||
super.modelInitialisation();
|
||||
|
||||
// Bind our TESR to our tile entity
|
||||
ClientRegistry.bindTileEntitySpecialRenderer(TileEntityForceFieldProjector.class, new TileEntityForceFieldProjectorRenderer());
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
|
@ -129,19 +105,19 @@ public class BlockForceFieldProjector extends BlockAbstractForceField {
|
|||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean isBlockNormalCube(final IBlockState blockState) {
|
||||
public boolean isBlockNormalCube(@Nonnull final IBlockState blockState) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean isOpaqueCube(final IBlockState blockState) {
|
||||
public boolean isOpaqueCube(@Nonnull final IBlockState blockState) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean isFullCube(final IBlockState blockState) {
|
||||
public boolean isFullCube(@Nonnull final IBlockState blockState) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -174,7 +150,7 @@ public class BlockForceFieldProjector extends BlockAbstractForceField {
|
|||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public void getSubBlocks(final CreativeTabs creativeTab, final NonNullList<ItemStack> list) {
|
||||
public void getSubBlocks(@Nonnull final CreativeTabs creativeTab, @Nonnull final NonNullList<ItemStack> list) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
list.add(new ItemStack(this, 1, i));
|
||||
}
|
||||
|
@ -185,6 +161,15 @@ public class BlockForceFieldProjector extends BlockAbstractForceField {
|
|||
return blockState.getValue(IS_DOUBLE_SIDED) ? 1 : 0;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public IBlockState getStateForPlacement(@Nonnull final World world, @Nonnull final BlockPos blockPos, @Nonnull final EnumFacing facing,
|
||||
final float hitX, final float hitY, final float hitZ, final int metadata,
|
||||
@Nonnull final EntityLivingBase entityLivingBase, @Nonnull final EnumHand enumHand) {
|
||||
final IBlockState blockState = super.getStateForPlacement(world, blockPos, facing, hitX, hitY, hitZ, metadata, entityLivingBase, enumHand);
|
||||
return blockState.withProperty(IS_DOUBLE_SIDED, metadata == 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlacedBy(@Nonnull final World world, @Nonnull final BlockPos blockPos, @Nonnull final IBlockState blockState,
|
||||
@Nonnull final EntityLivingBase entityLivingBase, @Nonnull final ItemStack itemStack) {
|
||||
|
|
|
@ -143,7 +143,7 @@ public class TileEntityForceField extends TileEntity {
|
|||
return tileEntityForceFieldProjector;
|
||||
|
||||
} else if (tileEntityForceFieldProjector.isPartOfForceField(new VectorI(this))) {
|
||||
if (tileEntityForceFieldProjector.isOn()) {
|
||||
if (tileEntityForceFieldProjector.isActive()) {
|
||||
return tileEntityForceFieldProjector;
|
||||
} else {
|
||||
// projector is disabled or out of power
|
||||
|
|
|
@ -9,7 +9,6 @@ import cr0s.warpdrive.config.Dictionary;
|
|||
import cr0s.warpdrive.config.WarpDriveConfig;
|
||||
import cr0s.warpdrive.data.BlockProperties;
|
||||
import cr0s.warpdrive.data.EnumForceFieldShape;
|
||||
import cr0s.warpdrive.data.EnumForceFieldState;
|
||||
import cr0s.warpdrive.data.EnumForceFieldUpgrade;
|
||||
import cr0s.warpdrive.data.FluidWrapper;
|
||||
import cr0s.warpdrive.data.ForceFieldSetup;
|
||||
|
@ -74,7 +73,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
private Vector3 v3Min = new Vector3(-1.0D, -1.0D, -1.0D);
|
||||
private Vector3 v3Max = new Vector3( 1.0D, 1.0D, 1.0D);
|
||||
private Vector3 v3Translation = new Vector3( 0.0D, 0.0D, 0.0D);
|
||||
private boolean legacy_isOn = false;
|
||||
private boolean isActive = false;
|
||||
|
||||
// computed properties
|
||||
private int cooldownTicks;
|
||||
|
@ -84,13 +83,10 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
private int guideTicks;
|
||||
private double damagesEnergyCost = 0.0D;
|
||||
private final HashSet<UUID> setInteractedEntities = new HashSet<>();
|
||||
private boolean isPowered = true;
|
||||
private ForceFieldSetup cache_forceFieldSetup;
|
||||
private ForceFieldSetup legacy_forceFieldSetup;
|
||||
private double consumptionLeftOver = 0.0D;
|
||||
public EnumFacing enumFacing = EnumFacing.UP;
|
||||
public float rotation_deg = 0.0F;
|
||||
public float rotationSpeed_degPerTick = 2.0F;
|
||||
|
||||
// carry over speed to next tick, useful for slow interactions
|
||||
private float carryScanSpeed;
|
||||
|
@ -144,7 +140,15 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
updateTicks = world.rand.nextInt(PROJECTOR_PROJECTION_UPDATE_TICKS);
|
||||
guideTicks = PROJECTOR_GUIDE_UPDATE_TICKS;
|
||||
enumFacing = world.getBlockState(pos).getValue(BlockProperties.FACING);
|
||||
rotation_deg = world.rand.nextFloat() * 360.0F;
|
||||
|
||||
// recover is_double_sided from blockstate property
|
||||
final IBlockState blockState = world.getBlockState(pos);
|
||||
if (blockState.getValue(BlockForceFieldProjector.IS_DOUBLE_SIDED)) {
|
||||
isDoubleSided = true;
|
||||
} else if (isDoubleSided) {
|
||||
// up to 1.5.13, blockstate wasn't set properly so we resynchronize here
|
||||
world.setBlockState(pos, blockState.withProperty(BlockForceFieldProjector.IS_DOUBLE_SIDED, true));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -152,9 +156,6 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
super.update();
|
||||
|
||||
if (world.isRemote) {
|
||||
rotation_deg += rotationSpeed_degPerTick;
|
||||
rotationSpeed_degPerTick = 0.98F * rotationSpeed_degPerTick
|
||||
+ 0.02F * getState().getRotationSpeed_degPerTick();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -203,7 +204,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
|
||||
// Powered ?
|
||||
final int energyRequired;
|
||||
if (!legacy_isOn) {
|
||||
if (!isActive) {
|
||||
energyRequired = (int) Math.round(forceFieldSetup.startupEnergyCost + forceFieldSetup.placeEnergyCost * forceFieldSetup.placeSpeed * PROJECTOR_PROJECTION_UPDATE_TICKS / 20.0F);
|
||||
} else {
|
||||
energyRequired = (int) Math.round( forceFieldSetup.scanEnergyCost * forceFieldSetup.scanSpeed * PROJECTOR_PROJECTION_UPDATE_TICKS / 20.0F);
|
||||
|
@ -212,21 +213,17 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
WarpDrive.logger.error(String.format("Force field projector requires %d to get started bu can only store %d",
|
||||
energyRequired, energy_getMaxStorage()));
|
||||
}
|
||||
final boolean new_isPowered = energy_getEnergyStored() >= energyRequired;
|
||||
if (isPowered != new_isPowered) {
|
||||
isPowered = new_isPowered;
|
||||
markDirty();
|
||||
}
|
||||
final boolean isPowered = energy_getEnergyStored() >= energyRequired;
|
||||
|
||||
final boolean isEnabledAndValid = isEnabled && isAssemblyValid;
|
||||
final boolean isOn = isEnabledAndValid && cooldownTicks <= 0 && isPowered;
|
||||
if (isOn) {
|
||||
if (!legacy_isOn) {
|
||||
final boolean new_isActive = isEnabledAndValid && cooldownTicks <= 0 && isPowered;
|
||||
if (new_isActive) {
|
||||
if (!isActive) {
|
||||
consumeEnergy(forceFieldSetup.startupEnergyCost, false);
|
||||
if (WarpDriveConfig.LOGGING_FORCE_FIELD) {
|
||||
WarpDrive.logger.info(String.format("%s starting up...", this));
|
||||
}
|
||||
legacy_isOn = true;
|
||||
isActive = true;
|
||||
markDirty();
|
||||
}
|
||||
cooldownTicks = 0;
|
||||
|
@ -253,11 +250,11 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
}
|
||||
|
||||
} else {
|
||||
if (legacy_isOn) {
|
||||
if (isActive) {
|
||||
if (WarpDriveConfig.LOGGING_FORCE_FIELD) {
|
||||
WarpDrive.logger.info(String.format("%s shutting down...", this));
|
||||
}
|
||||
legacy_isOn = false;
|
||||
isActive = false;
|
||||
markDirty();
|
||||
cooldownTicks = PROJECTOR_COOLDOWN_TICKS;
|
||||
guideTicks = 0;
|
||||
|
@ -344,8 +341,8 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
calculation_done();
|
||||
}
|
||||
|
||||
boolean isOn() {
|
||||
return legacy_isOn;
|
||||
boolean isActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
boolean isPartOfForceField(final VectorI vector) {
|
||||
|
@ -794,7 +791,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
}
|
||||
|
||||
// force a reboot
|
||||
legacy_isOn = false;
|
||||
isActive = false;
|
||||
|
||||
// invalidate() can be multi-threaded, so we're delaying the destruction
|
||||
if (Commons.isSafeThread()) {
|
||||
|
@ -932,22 +929,6 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
}
|
||||
}
|
||||
|
||||
public EnumForceFieldState getState() {
|
||||
EnumForceFieldState forceFieldState = EnumForceFieldState.NOT_CONNECTED;
|
||||
if (isConnected && isAssemblyValid) {
|
||||
if (isPowered) {
|
||||
if (isOn()) {
|
||||
forceFieldState = EnumForceFieldState.CONNECTED_POWERED;
|
||||
} else {
|
||||
forceFieldState = EnumForceFieldState.CONNECTED_OFFLINE;
|
||||
}
|
||||
} else {
|
||||
forceFieldState = EnumForceFieldState.CONNECTED_NOT_POWERED;
|
||||
}
|
||||
}
|
||||
return forceFieldState;
|
||||
}
|
||||
|
||||
public Vector3 getTranslation() {
|
||||
assert EnumForceFieldUpgrade.TRANSLATION.getProjectorUpgradeSlot() != null;
|
||||
if (hasUpgrade(EnumForceFieldUpgrade.TRANSLATION.getProjectorUpgradeSlot())) {
|
||||
|
@ -1014,9 +995,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
|
||||
setTranslation(tagCompound.getFloat("translationX"), tagCompound.getFloat("translationY"), tagCompound.getFloat("translationZ"));
|
||||
|
||||
legacy_isOn = tagCompound.getBoolean("isOn");
|
||||
|
||||
isPowered = tagCompound.getBoolean("isPowered");
|
||||
isActive = tagCompound.getBoolean("isOn");
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@ -1054,9 +1033,8 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
tagCompound.setFloat("translationZ", (float)v3Translation.z);
|
||||
}
|
||||
|
||||
tagCompound.setBoolean("isOn", legacy_isOn);
|
||||
tagCompound.setBoolean("isOn", isActive);
|
||||
|
||||
tagCompound.setBoolean("isPowered", isPowered);
|
||||
return tagCompound;
|
||||
}
|
||||
|
||||
|
@ -1128,7 +1106,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
|
|||
private Object[] state() { // isConnected, isPowered, shape
|
||||
final long energy = energy_getEnergyStored();
|
||||
final String status = getStatusHeaderInPureText();
|
||||
return new Object[] { status, isEnabled, isConnected, isPowered, getShape().name(), energy };
|
||||
return new Object[] { status, isEnabled, isConnected, isActive, getShape().name(), energy };
|
||||
}
|
||||
|
||||
public Object[] min(final Object[] arguments) {
|
||||
|
|
|
@ -11,7 +11,6 @@ import cr0s.warpdrive.event.ClientHandler;
|
|||
import cr0s.warpdrive.event.ModelBakeEventHandler;
|
||||
import cr0s.warpdrive.event.TooltipHandler;
|
||||
import cr0s.warpdrive.render.ClientCameraHandler;
|
||||
import cr0s.warpdrive.render.CustomModelLoaderProjector;
|
||||
import cr0s.warpdrive.render.RenderEntityNPC;
|
||||
import cr0s.warpdrive.render.RenderEntityParticleBunch;
|
||||
import cr0s.warpdrive.render.RenderOverlayAir;
|
||||
|
@ -37,7 +36,6 @@ import net.minecraft.item.ItemStack;
|
|||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
||||
import net.minecraftforge.client.model.obj.OBJLoader;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.client.registry.IRenderFactory;
|
||||
|
@ -56,7 +54,6 @@ public class ClientProxy extends CommonProxy {
|
|||
|
||||
OBJLoader.INSTANCE.addDomain(WarpDrive.MODID);
|
||||
|
||||
ModelLoaderRegistry.registerLoader(CustomModelLoaderProjector.INSTANCE);
|
||||
MinecraftForge.EVENT_BUS.register(ModelBakeEventHandler.INSTANCE);
|
||||
MinecraftForge.EVENT_BUS.register(SpriteManager.INSTANCE);
|
||||
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
package cr0s.warpdrive.data;
|
||||
|
||||
import net.minecraft.util.IStringSerializable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.HashMap;
|
||||
|
||||
public enum EnumForceFieldState implements IStringSerializable {
|
||||
|
||||
NOT_CONNECTED ("not_connected" , 0.1F ),
|
||||
CONNECTED_NOT_POWERED ("connected_not_powered", 0.5F ),
|
||||
CONNECTED_OFFLINE ("connected_offline" , 2.5F ),
|
||||
CONNECTED_POWERED ("connected_powered" , 15.0F );
|
||||
|
||||
private final String name;
|
||||
private final float rotationSpeed_degPerTick;
|
||||
|
||||
// cached values
|
||||
public static final int length;
|
||||
private static final HashMap<Integer, EnumForceFieldState> ID_MAP = new HashMap<>();
|
||||
|
||||
static {
|
||||
length = EnumForceFieldState.values().length;
|
||||
for (final EnumForceFieldState forceFieldState : values()) {
|
||||
ID_MAP.put(forceFieldState.ordinal(), forceFieldState);
|
||||
}
|
||||
}
|
||||
|
||||
EnumForceFieldState(final String name, final float rotationSpeed_degPerTick) {
|
||||
this.name = name;
|
||||
this.rotationSpeed_degPerTick = rotationSpeed_degPerTick;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public float getRotationSpeed_degPerTick() {
|
||||
return rotationSpeed_degPerTick;
|
||||
}
|
||||
|
||||
public static EnumForceFieldState get(final int damage) {
|
||||
return ID_MAP.get(damage);
|
||||
}
|
||||
}
|
|
@ -1,180 +0,0 @@
|
|||
package cr0s.warpdrive.render;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import cr0s.warpdrive.Commons;
|
||||
import cr0s.warpdrive.WarpDrive;
|
||||
import cr0s.warpdrive.block.forcefield.BlockForceFieldProjector;
|
||||
import cr0s.warpdrive.data.EnumForceFieldShape;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.block.model.*;
|
||||
import net.minecraft.client.renderer.block.model.ItemCameraTransforms.TransformType;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.ICustomModelLoader;
|
||||
import net.minecraftforge.client.model.IModel;
|
||||
import net.minecraftforge.client.model.obj.OBJLoader;
|
||||
import net.minecraftforge.common.model.IModelState;
|
||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.vecmath.Matrix4f;
|
||||
import java.util.*;
|
||||
|
||||
// Wrapper around OBJLoader to re-texture faces depending on IExtendedBlockState
|
||||
public enum CustomModelLoaderProjector implements ICustomModelLoader {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
private static boolean spriteInitialisationDone = false;
|
||||
private static TextureAtlasSprite spriteShape_none;
|
||||
private static final HashMap<EnumForceFieldShape, TextureAtlasSprite> spriteShapes = new HashMap<>(EnumForceFieldShape.length);
|
||||
private static void initSprites() {
|
||||
if (!spriteInitialisationDone) {
|
||||
final TextureMap textureMapBlocks = Minecraft.getMinecraft().getTextureMapBlocks();
|
||||
spriteShapes.put(EnumForceFieldShape.NONE , textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_none"));
|
||||
spriteShapes.put(EnumForceFieldShape.CUBE , textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_cube"));
|
||||
spriteShapes.put(EnumForceFieldShape.CYLINDER_H, textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_cylinder_h"));
|
||||
spriteShapes.put(EnumForceFieldShape.CYLINDER_V, textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_cylinder_v"));
|
||||
spriteShapes.put(EnumForceFieldShape.PLANE , textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_plane"));
|
||||
spriteShapes.put(EnumForceFieldShape.SPHERE , textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_sphere"));
|
||||
spriteShapes.put(EnumForceFieldShape.TUBE , textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_tube"));
|
||||
spriteShapes.put(EnumForceFieldShape.TUNNEL , textureMapBlocks.getAtlasSprite("warpdrive:blocks/forcefield/projector-shape_tunnel"));
|
||||
spriteShape_none = spriteShapes.get(EnumForceFieldShape.NONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResourceManagerReload(@Nonnull final IResourceManager resourceManager) {
|
||||
OBJLoader.INSTANCE.onResourceManagerReload(resourceManager);
|
||||
spriteInitialisationDone = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accepts(final ResourceLocation modelLocation) {
|
||||
return WarpDrive.MODID.equals(modelLocation.getNamespace()) && modelLocation.getPath().endsWith(".wobj");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IModel loadModel(@Nonnull final ResourceLocation modelLocation) throws Exception {
|
||||
return new MyModel(OBJLoader.INSTANCE.loadModel(modelLocation));
|
||||
}
|
||||
|
||||
private class MyModel implements IModel {
|
||||
private final IModel model;
|
||||
|
||||
MyModel(final IModel model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Collection<ResourceLocation> getDependencies() {
|
||||
return model.getDependencies();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Collection<ResourceLocation> getTextures() {
|
||||
return model.getTextures();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public IBakedModel bake(@Nonnull final IModelState state, @Nonnull final VertexFormat format,
|
||||
@Nonnull final java.util.function.Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter) {
|
||||
return new MyBakedModel(model.bake(state, format, bakedTextureGetter));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public IModelState getDefaultState() {
|
||||
return model.getDefaultState();
|
||||
}
|
||||
}
|
||||
|
||||
class MyBakedModel implements IBakedModel {
|
||||
|
||||
private final IBakedModel bakedModel;
|
||||
|
||||
MyBakedModel(final IBakedModel bakedModel) {
|
||||
this.bakedModel = bakedModel;
|
||||
initSprites();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<BakedQuad> getQuads(@Nullable final IBlockState blockState, @Nullable final EnumFacing enumFacing, final long rand) {
|
||||
final List<BakedQuad> bakedQuadsIn = bakedModel.getQuads(blockState, enumFacing, rand);
|
||||
final IExtendedBlockState exState = (IExtendedBlockState) blockState;
|
||||
EnumForceFieldShape enumForceFieldShape = exState != null ? exState.getValue(BlockForceFieldProjector.SHAPE) : EnumForceFieldShape.NONE;
|
||||
if (enumForceFieldShape == null) {
|
||||
if (Commons.throttleMe("CustomModelLoaderProjector Invalid shape")) {
|
||||
new RuntimeException(String.format("Invalid shape for %s facing %s",
|
||||
blockState, enumFacing ))
|
||||
.printStackTrace(WarpDrive.printStreamError);
|
||||
}
|
||||
enumForceFieldShape = EnumForceFieldShape.NONE;
|
||||
}
|
||||
final TextureAtlasSprite spriteShape = spriteShapes.get(enumForceFieldShape);
|
||||
final List<BakedQuad> bakedQuadsOut = Lists.newArrayList();
|
||||
for(final BakedQuad bakedQuadIn : bakedQuadsIn) {
|
||||
if (bakedQuadIn.getSprite().equals(spriteShape_none)) {
|
||||
final BakedQuad bakedQuadOut = new BakedQuadRetextured(bakedQuadIn, spriteShape);
|
||||
bakedQuadsOut.add(bakedQuadOut);
|
||||
} else {
|
||||
bakedQuadsOut.add(bakedQuadIn);
|
||||
}
|
||||
}
|
||||
return ImmutableList.copyOf(bakedQuadsOut);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAmbientOcclusion() {
|
||||
return bakedModel.isAmbientOcclusion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGui3d() {
|
||||
return bakedModel.isGui3d();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBuiltInRenderer() {
|
||||
return bakedModel.isBuiltInRenderer();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public TextureAtlasSprite getParticleTexture() {
|
||||
return bakedModel.getParticleTexture();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Nonnull
|
||||
@Override
|
||||
public ItemCameraTransforms getItemCameraTransforms() {
|
||||
return bakedModel.getItemCameraTransforms();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ItemOverrideList getOverrides() {
|
||||
return bakedModel.getOverrides();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Pair<? extends IBakedModel, Matrix4f> handlePerspective(@Nonnull final TransformType cameraTransformType) {
|
||||
return ((IBakedModel) bakedModel).handlePerspective(cameraTransformType);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
package cr0s.warpdrive.render;
|
||||
|
||||
import cr0s.warpdrive.WarpDrive;
|
||||
import cr0s.warpdrive.block.forcefield.TileEntityForceFieldProjector;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.*;
|
||||
import net.minecraft.client.renderer.GlStateManager.DestFactor;
|
||||
import net.minecraft.client.renderer.GlStateManager.SourceFactor;
|
||||
import net.minecraft.client.renderer.block.model.BakedQuad;
|
||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.IModel;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.common.model.TRSRTransformation;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TileEntityForceFieldProjectorRenderer extends TileEntitySpecialRenderer<TileEntityForceFieldProjector> {
|
||||
|
||||
private IBakedModel bakedModel;
|
||||
|
||||
private IBakedModel getBakedModel() {
|
||||
// Since we cannot bake in preInit() we do lazy baking of the model as soon as we need it for rendering
|
||||
if (bakedModel == null) {
|
||||
final ResourceLocation resourceLocation = new ResourceLocation(WarpDrive.MODID, "block/forcefield/projector_ring.obj");
|
||||
final IModel model = RenderCommons.getModel(resourceLocation);
|
||||
bakedModel = model.bake(TRSRTransformation.identity(), DefaultVertexFormats.ITEM, ModelLoader.defaultTextureGetter());
|
||||
}
|
||||
return bakedModel;
|
||||
}
|
||||
|
||||
private static List<BakedQuad> quads;
|
||||
|
||||
@Override
|
||||
public void render(@Nonnull final TileEntityForceFieldProjector tileEntityForceFieldProjector, final double x, final double y, final double z,
|
||||
final float partialTicks, final int destroyStage, final float alpha) {
|
||||
if (!tileEntityForceFieldProjector.getWorld().isBlockLoaded(tileEntityForceFieldProjector.getPos(), false)) {
|
||||
return;
|
||||
}
|
||||
if (quads == null) {
|
||||
quads = getBakedModel().getQuads(null, null, 0L);
|
||||
}
|
||||
final Tessellator tessellator = Tessellator.getInstance();
|
||||
GlStateManager.pushAttrib();
|
||||
GlStateManager.pushMatrix();
|
||||
|
||||
GlStateManager.translate(x + 0.5D, y + 0.5D, z + 0.5D);
|
||||
switch (tileEntityForceFieldProjector.enumFacing) {
|
||||
case DOWN : GlStateManager.rotate(180.0F, 1.0F, 0.0F, 0.0F); break;
|
||||
case UP : break;
|
||||
case NORTH: GlStateManager.rotate(+90.0F, 1.0F, 0.0F, 0.0F); GlStateManager.rotate(180.0F, 0.0F, 0.0F, 1.0F); break;
|
||||
case SOUTH: GlStateManager.rotate(+90.0F, 1.0F, 0.0F, 0.0F); break;
|
||||
case WEST : GlStateManager.rotate(+90.0F, 1.0F, 0.0F, 0.0F); GlStateManager.rotate(+90.0F, 0.0F, 0.0F, 1.0F); break;
|
||||
case EAST : GlStateManager.rotate(+90.0F, 1.0F, 0.0F, 0.0F); GlStateManager.rotate(-90.0F, 0.0F, 0.0F, 1.0F); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
GlStateManager.blendFunc(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA);
|
||||
GlStateManager.enableBlend();
|
||||
// GlStateManager.disableCull();
|
||||
RenderHelper.disableStandardItemLighting();
|
||||
GlStateManager.enableLighting();
|
||||
// @TODO setLightmapDisabled
|
||||
|
||||
final float wheelRotation = tileEntityForceFieldProjector.rotation_deg + partialTicks * tileEntityForceFieldProjector.rotationSpeed_degPerTick;
|
||||
GlStateManager.rotate(wheelRotation, 0.0F, 1.0F, 0.0F);
|
||||
|
||||
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
|
||||
final BufferBuilder worldRenderer = tessellator.getBuffer();
|
||||
worldRenderer.setTranslation(-0.5, -0.5, -0.5);
|
||||
worldRenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
|
||||
|
||||
RenderCommons.renderModelTESR(quads, worldRenderer, tileEntityForceFieldProjector.getWorld().getCombinedLight(tileEntityForceFieldProjector.getPos(), 15));
|
||||
|
||||
tessellator.draw();
|
||||
worldRenderer.setTranslation(0.0D, 0.0D, 0.0D);
|
||||
|
||||
RenderHelper.enableStandardItemLighting();
|
||||
GlStateManager.disableBlend();
|
||||
// GlStateManager.enableCull();
|
||||
GlStateManager.popMatrix();
|
||||
GlStateManager.popAttrib();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue