Last active
August 29, 2015 14:27
-
-
Save StillManic/4ac7d34327339ef9b769 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rcteam.rc2.block; | |
import com.google.common.collect.Lists; | |
import net.minecraft.block.properties.PropertyDirection; | |
import net.minecraft.block.properties.PropertyHelper; | |
import net.minecraft.block.state.BlockState; | |
import net.minecraft.block.state.IBlockState; | |
import net.minecraft.entity.EntityLivingBase; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.util.BlockPos; | |
import net.minecraft.util.EnumFacing; | |
import net.minecraft.block.Block; | |
import net.minecraft.tileentity.TileEntity; | |
import net.minecraft.world.IBlockAccess; | |
import net.minecraft.world.World; | |
import rcteam.rc2.RC2; | |
import rcteam.rc2.block.te.TileEntityTrack; | |
import rcteam.rc2.rollercoaster.*; | |
import rcteam.rc2.util.Utils; | |
import java.util.Collection; | |
import java.util.List; | |
public class BlockTrack extends Block { | |
public static final PropertyDirection FACING = PropertyDirection.create("facing", Lists.newArrayList(EnumFacing.HORIZONTALS)); | |
public static final TrackPieceProperty PIECE_PROPERTY = new TrackPieceProperty("piece"); | |
// public ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[] {FACING, PIECE_PROPERTY}, new IUnlistedProperty[] {TrackProperty.instance}); | |
protected TrackPieceInfo info; | |
public BlockTrack(TrackPieceInfo info) { | |
super(info.getCategory().getMaterial()); | |
this.info = info; | |
setCreativeTab(RC2.tab); | |
setBlockUnbreakable(); | |
setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(PIECE_PROPERTY, info.getCurrentPiece())); | |
setBlockBounds(0f, 0f, 0f, 1f, 0.5f, 1f); | |
setUnlocalizedName("track_" + info.getCategory().getName()); | |
} | |
public TrackPieceInfo getInfo() { | |
return this.info; | |
} | |
public void setInfo(TrackPieceInfo info) { | |
this.info = info; | |
} | |
@Override | |
public boolean isOpaqueCube() { | |
return false; | |
} | |
@Override | |
public boolean isFullCube() { | |
return false; | |
} | |
@Override | |
public boolean isVisuallyOpaque() { | |
return false; | |
} | |
// @Override | |
// public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos) { | |
// return ((IExtendedBlockState) state).withProperty(TrackProperty.instance, this.info); | |
// } | |
@Override | |
public BlockState createBlockState() { | |
return new BlockState(this, FACING, PIECE_PROPERTY); | |
// return new ExtendedBlockState(this, new IProperty[] {FACING}, new IUnlistedProperty[] {TrackProperty.instance}); | |
} | |
@Override | |
public IBlockState getStateFromMeta(int meta) { | |
if (this.info != null) { | |
return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(PIECE_PROPERTY, this.info.getCurrentPiece()); | |
} | |
return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)); | |
} | |
@Override | |
public int getMetaFromState(IBlockState state) { | |
// this.info = (TrackPieceInfo) state.getValue(PIECE_PROPERTY); | |
// if (this.info == null) { | |
// this.info = ((BlockTrack) state.getBlock()).getInfo(); | |
// } else { | |
// this.info.setCurrentPiece(state.getValue(PIECE_PROPERTY)); | |
// } | |
// this.info.setCurrentPiece((TrackPiece) state.getValue(PIECE_PROPERTY)); | |
// this.info.setCurrentPiece(this.info.getNextPiece()); | |
// state.cycleProperty(PIECE_PROPERTY); | |
// this.info.setCurrentPiece((TrackPiece) state.getValue(PIECE_PROPERTY)); | |
EnumFacing facing = ((EnumFacing) state.getValue(FACING)); | |
if (facing == EnumFacing.DOWN || facing == EnumFacing.UP) { | |
return 0; | |
} else { | |
return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex(); | |
} | |
} | |
@Override | |
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) { | |
// TrackPiece statePiece = (TrackPiece) state.getValue(PIECE_PROPERTY); | |
// return state.cycleProperty(PIECE_PROPERTY); | |
// return state.withProperty(PIECE_PROPERTY, this.info.getPiece("large_corner_right")); | |
if (((TrackPiece) state.getValue(PIECE_PROPERTY)) != this.info.getCurrentPiece()) { | |
state = state.withProperty(PIECE_PROPERTY, this.info.getCurrentPiece()); | |
} | |
if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof TileEntityTrack) { | |
((TileEntityTrack) world.getTileEntity(pos)).info = this.info; | |
} | |
return state; | |
} | |
@Override | |
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { | |
//TODO: things will be set from the track designer later | |
return this.getDefaultState().withProperty(FACING, Utils.getFacingFromEntity(worldIn, pos, placer, false, false)); | |
// return this.getDefaultState().withProperty(FACING, Utils.getFacingFromEntity(worldIn, pos, placer, false, false)).withProperty(PIECE_PROPERTY, this.info.cycleCurrentPiece()); | |
} | |
@Override | |
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { | |
if (this.info.getCurrentPiece() != state.getValue(PIECE_PROPERTY)) { | |
RC2.logger.info("current piece: " + this.info.getCurrentPiece().getName()); | |
RC2.logger.info("state piece: " + ((TrackPiece) state.getValue(PIECE_PROPERTY)).getName()); | |
} | |
} | |
@Override | |
public int getRenderType() { | |
return 3; | |
} | |
@Override | |
public boolean hasTileEntity(IBlockState state) { | |
return true; | |
} | |
@Override | |
public TileEntity createTileEntity(World world, IBlockState state) { | |
return new TileEntityTrack(this.info); | |
} | |
@Override | |
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { | |
// if (worldIn.isRemote) { | |
RC2.logger.info("someones punching me!"); | |
// this.info.setCurrentPiece(this.info.getNextPiece()); | |
// RC2.logger.info("current after changing: " + this.info.getCurrentPiece().getName()); | |
// ((BlockTrack) state.getBlock()).setInfo(this.info); | |
// worldIn.setBlockState(pos, state.withProperty(PIECE_PROPERTY, this.info.getCurrentPiece()), 3); | |
// worldIn.setBlockState(pos, state.withProperty(PIECE_PROPERTY, this.info.getPiece("straight")), 3); | |
// worldIn.markBlockRangeForRenderUpdate(pos, pos); | |
// } | |
return true; | |
} | |
public static class TrackPieceProperty extends PropertyHelper { | |
private List<TrackPiece> allowedValues; | |
public TrackPieceProperty(String name) { | |
super(name, TrackPiece.class); | |
} | |
public void setAllowedValues(List<TrackPiece> allowedValues) { | |
this.allowedValues = allowedValues; | |
} | |
@Override | |
public Collection getAllowedValues() { | |
return this.allowedValues; | |
} | |
@Override | |
public String getName(Comparable value) { | |
return ((TrackPiece) value).getName(); | |
// return this.getName(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rcteam.rc2.item; | |
import net.minecraft.block.Block; | |
import net.minecraft.block.state.IBlockState; | |
import net.minecraft.client.renderer.ItemMeshDefinition; | |
import net.minecraft.client.resources.model.ModelResourceLocation; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.ItemBlock; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.launchwrapper.Launch; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.util.BlockPos; | |
import net.minecraft.util.EnumFacing; | |
import net.minecraft.world.World; | |
import rcteam.rc2.block.BlockTrack; | |
import rcteam.rc2.rollercoaster.TrackPiece; | |
import rcteam.rc2.util.Reference; | |
import rcteam.rc2.util.TrackStateMapper; | |
import rcteam.rc2.util.Utils; | |
import java.util.List; | |
public class ItemTrack extends ItemBlock { | |
private BlockTrack track; | |
public ItemTrack(Block track) { | |
super(track); | |
this.track = (BlockTrack) track; | |
} | |
public BlockTrack getTrack() { | |
return this.track; | |
} | |
@Override | |
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, IBlockState newState) { | |
if ((Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) { //this makes tracks placeable by hand when run in a dev env | |
//TODO! | |
// if (!stack.hasTagCompound()) { | |
NBTTagCompound compound = new NBTTagCompound(); | |
compound.setTag("current_piece", track.getInfo().getCurrentPiece().writeToNBT()); | |
stack.setTagCompound(compound); | |
// } | |
if (player.isSneaking()) { | |
track.getInfo().setCurrentPiece("medium_corner"); | |
world.setBlockState(pos, track.getActualState(newState.withProperty(BlockTrack.FACING, Utils.getFacingFromEntity(world, pos, player, false, false)).withProperty(BlockTrack.PIECE_PROPERTY, track.getInfo().getPiece("medium_corner")), world, pos), 3); | |
} else { | |
track.getInfo().setCurrentPiece("straight"); | |
world.setBlockState(pos, track.getActualState(newState.withProperty(BlockTrack.FACING, Utils.getFacingFromEntity(world, pos, player, false, false)).withProperty(BlockTrack.PIECE_PROPERTY, track.getInfo().getPiece("straight")), world, pos), 3); | |
} | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip, boolean advanced) { | |
if (!stack.hasTagCompound()) { | |
NBTTagCompound compound = new NBTTagCompound(); | |
stack.setTagCompound(compound); | |
compound.setTag("current_piece", this.track.getInfo().getCurrentPiece().writeToNBT()); | |
} else if (!stack.getTagCompound().hasKey("current_piece")) { | |
stack.getTagCompound().setTag("current_piece", this.track.getInfo().getCurrentPiece().writeToNBT()); | |
} | |
TrackPiece piece = TrackPiece.readFromNBT(stack.getTagCompound().getCompoundTag("current_piece")); | |
tooltip.add(piece.getDisplayName()); | |
tooltip.add(piece.getName()); | |
} | |
public static class ItemTrackMeshDefinition implements ItemMeshDefinition { | |
public static final ItemTrackMeshDefinition INSTANCE = new ItemTrackMeshDefinition(); | |
private String name = "inventory"; | |
public void setVariantName(String name) { | |
if (name != null && !name.isEmpty()) { | |
this.name = name; | |
} | |
} | |
@Override | |
public ModelResourceLocation getModelLocation(ItemStack stack) { | |
ModelResourceLocation location = new ModelResourceLocation(Reference.RESOURCE_PREFIX + "tracks/hyper_twister", name); | |
return location; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rcteam.rc2.block; | |
import com.google.common.collect.Lists; | |
import com.google.common.collect.Maps; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.resources.model.ModelBakery; | |
import net.minecraft.client.resources.model.ModelResourceLocation; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemBlock; | |
import net.minecraft.item.ItemStack; | |
import net.minecraftforge.client.model.ModelLoader; | |
import net.minecraftforge.fml.common.registry.GameData; | |
import net.minecraftforge.fml.common.registry.GameRegistry; | |
import net.minecraft.block.Block; | |
import net.minecraft.tileentity.TileEntity; | |
import net.minecraftforge.fml.relauncher.Side; | |
import org.apache.commons.lang3.tuple.Pair; | |
import rcteam.rc2.block.te.*; | |
import rcteam.rc2.item.ItemTrack; | |
import rcteam.rc2.rollercoaster.CategoryEnum; | |
import rcteam.rc2.rollercoaster.TrackPieceInfo; | |
import rcteam.rc2.rollercoaster.TrackPieceRegistry; | |
import rcteam.rc2.util.Reference; | |
import rcteam.rc2.util.TrackStateMapper; | |
import java.util.List; | |
import java.util.Map; | |
public class RC2Blocks { | |
public static final Map<Block, Pair<String, ModelResourceLocation>> modelMap = Maps.newHashMap(); | |
public static final Map<CategoryEnum, TrackPieceInfo> infoMap = Maps.newEnumMap(CategoryEnum.class); | |
public static Block entrance; | |
public static Block track_steel; | |
public static Block track_wood; | |
public static Block track_inverted; | |
public static Block track_water; | |
public static void preInit(Side side) { | |
entrance = new BlockEntrance(); | |
infoMap.put(CategoryEnum.STEEL, new TrackPieceInfo(CategoryEnum.STEEL, TrackPieceRegistry.INSTANCE.getPieces())); //TODO: distinguish between different category pieces and different Style pieces! | |
infoMap.put(CategoryEnum.WOODEN, new TrackPieceInfo(CategoryEnum.WOODEN, TrackPieceRegistry.INSTANCE.getPieces())); | |
infoMap.put(CategoryEnum.INVERTED, new TrackPieceInfo(CategoryEnum.INVERTED, TrackPieceRegistry.INSTANCE.getPieces())); | |
infoMap.put(CategoryEnum.WATER, new TrackPieceInfo(CategoryEnum.WATER, TrackPieceRegistry.INSTANCE.getPieces())); | |
BlockTrack.PIECE_PROPERTY.setAllowedValues(infoMap.get(CategoryEnum.STEEL).getPieces()); | |
track_steel = new BlockTrack(infoMap.get(CategoryEnum.STEEL)); | |
BlockTrack.PIECE_PROPERTY.setAllowedValues(infoMap.get(CategoryEnum.WOODEN).getPieces()); | |
track_wood = new BlockTrack(infoMap.get(CategoryEnum.WOODEN)); | |
BlockTrack.PIECE_PROPERTY.setAllowedValues(infoMap.get(CategoryEnum.INVERTED).getPieces()); | |
track_inverted = new BlockTrack(infoMap.get(CategoryEnum.INVERTED)); | |
BlockTrack.PIECE_PROPERTY.setAllowedValues(infoMap.get(CategoryEnum.WATER).getPieces()); | |
track_water = new BlockTrack(infoMap.get(CategoryEnum.WATER)); | |
registerBlock(entrance, "entrance"); | |
registerBlock(track_steel, ItemTrack.class, "track_steel", side); | |
registerBlock(track_wood, ItemTrack.class, "track_wooden", side); | |
registerBlock(track_inverted, ItemTrack.class, "track_inverted", side); | |
registerBlock(track_water, ItemTrack.class, "track_water", side); | |
if (side == Side.CLIENT) { | |
// ModelLoader.setCustomStateMapper(track_steel, TrackStateMapper.INSTANCE); | |
// ModelLoader.setCustomStateMapper(track_wood, TrackStateMapper.INSTANCE); | |
// ModelLoader.setCustomStateMapper(track_inverted, TrackStateMapper.INSTANCE); | |
// ModelLoader.setCustomStateMapper(track_water, TrackStateMapper.INSTANCE); | |
// ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(track_steel), 0, new ModelResourceLocation(Reference.RESOURCE_PREFIX + "tracks/hyper_twister", "inventory")); | |
// ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(track_wood), 0, new ModelResourceLocation(Reference.RESOURCE_PREFIX + "tracks/hyper_twister", "inventory")); | |
// ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(track_inverted), 0, new ModelResourceLocation(Reference.RESOURCE_PREFIX + "tracks/hyper_twister", "inventory")); | |
// ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(track_water), 0, new ModelResourceLocation(Reference.RESOURCE_PREFIX + "tracks/hyper_twister", "inventory")); | |
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(track_steel), ItemTrack.ItemTrackMeshDefinition.INSTANCE); | |
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(track_wood), ItemTrack.ItemTrackMeshDefinition.INSTANCE); | |
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(track_inverted), ItemTrack.ItemTrackMeshDefinition.INSTANCE); | |
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(track_water), ItemTrack.ItemTrackMeshDefinition.INSTANCE); | |
ModelBakery.addVariantName(Item.getItemFromBlock(track_steel), Reference.RESOURCE_PREFIX + "tracks/hyper_twister"); | |
ModelBakery.addVariantName(Item.getItemFromBlock(track_wood), Reference.RESOURCE_PREFIX + "tracks/hyper_twister"); | |
ModelBakery.addVariantName(Item.getItemFromBlock(track_inverted), Reference.RESOURCE_PREFIX + "tracks/hyper_twister"); | |
ModelBakery.addVariantName(Item.getItemFromBlock(track_water), Reference.RESOURCE_PREFIX + "tracks/hyper_twister"); | |
} | |
registerTE(TileEntityEntrance.class, entrance); | |
// registerTE(TileEntityTrackSteel.class, track_steel); | |
// registerTE(TileEntityTrackWood.class, track_wood); | |
// registerTE(TileEntityTrackInverted.class, track_inverted); | |
// registerTE(TileEntityTrackWater.class, track_water); | |
registerTE(TileEntityTrack.class, track_steel); | |
registerTE(TileEntityTrack.class, track_wood); | |
registerTE(TileEntityTrack.class, track_inverted); | |
registerTE(TileEntityTrack.class, track_water); | |
} | |
private static void registerBlock(Block block, String name) { | |
GameRegistry.registerBlock(block, name); | |
modelMap.put(block, Pair.of(name, null)); | |
} | |
private static void registerBlock(Block block, Class<? extends ItemBlock> itemClass, String name, Side side) { | |
if (block instanceof BlockTrack && side == Side.CLIENT) { | |
ModelLoader.setCustomStateMapper(block, TrackStateMapper.INSTANCE); | |
} | |
GameRegistry.registerBlock(block, itemClass, name); | |
modelMap.put(block, Pair.of(name, null)); | |
} | |
private static void registerTE(Class<? extends TileEntity> te, Block block) { | |
GameRegistry.registerTileEntity(te, modelMap.get(block).getLeft()); | |
} | |
public static void init(Side side) { | |
List<Block> blocks = Lists.newArrayList(modelMap.keySet()); | |
for (Block block : blocks) { | |
ModelResourceLocation location = new ModelResourceLocation(Reference.RESOURCE_PREFIX + modelMap.get(block).getLeft(), "inventory"); | |
modelMap.put(block, Pair.of(modelMap.get(block).getLeft(), location)); | |
if (side == Side.CLIENT) { | |
if (block instanceof BlockTrack) { | |
// ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(block), ItemTrack.ItemTrackMeshDefinition.INSTANCE); | |
// Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), ItemTrack.ItemTrackMeshDefinition.INSTANCE); | |
// Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), 0, ItemTrack.ItemTrackMeshDefinition.INSTANCE.getModelLocation(new ItemStack(Item.getItemFromBlock(block)))); | |
// ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, ItemTrack.ItemTrackMeshDefinition.INSTANCE.getModelLocation(new ItemStack(Item.getItemFromBlock(block)))); | |
} else { | |
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), 0, location); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rcteam.rc2.block.te; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.network.NetworkManager; | |
import net.minecraft.network.Packet; | |
import net.minecraft.network.play.server.S35PacketUpdateTileEntity; | |
import net.minecraft.tileentity.TileEntity; | |
import rcteam.rc2.block.BlockTrack; | |
import rcteam.rc2.network.packets.PacketThemeParkEntrance; | |
import rcteam.rc2.rollercoaster.*; | |
public class TileEntityTrack extends TileEntity { | |
// public CoasterStyle style; | |
// public CategoryEnum category; | |
// public TrackPiece piece; | |
public TrackPieceInfo info; | |
public TileEntityTrack() { | |
// this.info = new TrackPieceInfo(CategoryEnum.STEEL, TrackPieceRegistry.INSTANCE.getPiece("straight")); | |
} | |
// public TileEntityTrack(CoasterStyle style) { | |
// this.style = style; | |
// } | |
// public TileEntityTrack(CategoryEnum category, TrackPiece piece) { | |
// this.category = category; | |
// this.piece = piece; | |
// } | |
public TileEntityTrack(TrackPieceInfo info) { | |
this.info = info; | |
} | |
@Override | |
public void readFromNBT(NBTTagCompound compound) { | |
super.readFromNBT(compound); | |
// if (compound.hasKey("info")) { | |
// this.info = TrackPieceInfo.readFromNBT(compound.getCompoundTag("info")); | |
this.info = TrackPieceInfo.readFromNBT(compound); | |
((BlockTrack) this.blockType).setInfo(this.info); | |
// } | |
} | |
@Override | |
public void writeToNBT(NBTTagCompound compound) { | |
super.writeToNBT(compound); | |
compound.setTag("info", this.info.writeToNBT()); | |
} | |
@Override | |
public void onChunkUnload() { | |
this.writeToNBT(this.getTileData()); | |
} | |
@Override | |
public Packet getDescriptionPacket() { | |
return new S35PacketUpdateTileEntity(this.getPos(), this.getBlockMetadata(), this.info.writeToNBT()); | |
} | |
@Override | |
public void onDataPacket(NetworkManager manager, S35PacketUpdateTileEntity packet) { | |
this.readFromNBT(packet.getNbtCompound()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment