Jump to content

[1.8] BlockFluidFinite subclass problem with custom Blockstate Properties


Jershy

Recommended Posts

I'm working on a water block that acts sort of like flood water so I decided to use BlockFluidFinite for the parent class because it has just what I want. The problem is that I want to add custom properties such as "FLOWSPEED" and "FLOWDIRECTION" along with "LEVEL" in the Parent class. However, the "setDefaultBlockState" method is final. the only thing that I thought I could do at this point was to copy all the code and paste it into my class. That just seemed much to silly and fraut with problems. Any suggestions?

 

Class;

package src.IVWeather.block;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;

import com.google.common.collect.Maps;
import java.util.Map;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import src.IVWeather.*;
import src.IVWeather.block.properties.PropertyFloodWater;
import src.IVWeather.fluid.IVBlockFluidFinite;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.BlockStaticLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.EnumFaceDirection;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.EnumFacing.Plane;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.BlockFluidFinite;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.block.properties.PropertyEnum;

public class BlockRushingWater extends BlockFluidFinite implements IFluidBlock{


public int tick_counter;
public static final String name = "IVWFloodWaters";
public static final PropertyEnum FLOWDIRECTION = PropertyEnum.create("flow_dir", EnumFacing.class, EnumFacing.HORIZONTALS);
public static final PropertyInteger FLOWSPEED = PropertyInteger.create("flow_speed", 0, 10);


public BlockRushingWater(Fluid fluid) {
	super(fluid, Material.water);
	this.tickRate = 5;
	GameRegistry.registerBlock(this, name);
	setUnlocalizedName(name);
	setCreativeTab(CreativeTabs.tabBlock);
	this.setDefaultState(this.blockState.getBaseState().withProperty(LEVEL, Integer.valueOf(0)).withProperty(FLOWDIRECTION, EnumFacing.NORTH).withProperty(FLOWSPEED, Integer.valueOf(0)));
}

@Override
protected BlockState createBlockState() {
	return new BlockState(this, new IProperty[]{LEVEL, FLOWSPEED, FLOWDIRECTION});// 121
}

public boolean canDrain(World world, BlockPos pos) {
    return true;
}

 public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) {
	 boolean changed = false;// 60
      int quantaRemaining = ((Integer)state.getValue(LEVEL)).intValue() + 1;// 61
      int prevRemaining = quantaRemaining;// 64
      EnumFacing flowDirection = ((EnumFacing) state.getValue(FLOWDIRECTION));
      int flowSpeed = ((Integer) state.getValue(FLOWSPEED)).intValue();
      System.out.println("Quanta; "+quantaRemaining);
      System.out.println("flow_Speed; "+flowSpeed);
      System.out.println(flowDirection);
    
      quantaRemaining = this.tryToFlowVerticallyInto(world, pos, quantaRemaining, flowDirection, flowSpeed);
      
      if(flowSpeed < 1) {
    	  flowDirection = null;
      }
      
      if(quantaRemaining >= 1) {// 67
         if(quantaRemaining != prevRemaining) {// 71
            changed = true;// 73
            if(quantaRemaining == 1) {// 74
               world.setBlockState(pos, state.withProperty(LEVEL, Integer.valueOf(quantaRemaining - 1)), 2);// 76
               return;// 77
            }
         } else if(quantaRemaining == 1) {// 80
            return;// 82
         }
         
         int lowerthan = quantaRemaining - 1;// 86
         int total = quantaRemaining;// 87
         int count = 1;// 88
         Iterator each = Plane.HORIZONTAL.iterator();// 90
         List<BlockPos> open = new ArrayList();
         List<BlockPos> water = new ArrayList();
         int newQua = quantaRemaining > 1? (quantaRemaining / 2) - (quantaRemaining % 2): quantaRemaining;
         float size = quantaRemaining / this.quantaPerBlockFloat;
    
         if(flowSpeed > 0) {
         BlockPos flowOff = pos.offset(flowDirection);
         IBlockState block = world.getBlockState(flowOff);
        	 if(world.getBlockState(flowOff).getBlock() == Blocks.air) {
        		quantaRemaining -= newQua;
        	 	world.setBlockState(pos, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(quantaRemaining)), 2);
        	 	world.setBlockState(flowOff, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(newQua)).withProperty(FLOWDIRECTION, flowDirection), 2);
        	 	flowSpeed--;
        	 }else if(block.getBlock() == this){
        		 int addTo = (Integer) block.getValue(LEVEL);
        		 int all = newQua+ addTo;
        		 if(all < this.quantaPerBlock) {
        		 world.setBlockState(pos, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(quantaRemaining) - 1), 2);
	        	 world.setBlockState(flowOff, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(newQua + addTo)).withProperty(FLOWDIRECTION, flowDirection), 2);
        		 }else{
        		 int additional = all - this.quantaPerBlock;	 
        		 world.setBlockState(pos, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(quantaRemaining) - 1), 2);
		         world.setBlockState(flowOff, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(this.quantaPerBlock)).withProperty(FLOWDIRECTION, flowDirection), 2);
	        	 world.setBlockState(flowOff.offset(flowDirection), this.getDefaultState().withProperty(LEVEL, Integer.valueOf(additional)).withProperty(FLOWDIRECTION, flowDirection), 2);	 	 
        		 }
        	 }else{
        		float totalHard = this.getBlockHardness(world, flowOff) + block.getBlock().getBlockHardness(world, flowOff.offset(flowDirection));
        		if(size * (flowSpeed / 10) >= totalHard) {//TODO these numbers for how much speed it takes to move a block were kinda arbitrary. make 'em better
        			world.setBlockToAir(flowOff);
        			world.setBlockToAir(flowOff.offset(flowDirection));
        		}
        	 }  

         }
         
         while(each.hasNext()) {
            EnumFacing rem = (EnumFacing)each.next();
            BlockPos i$ = pos.offset(rem);// 92
            IBlockState state2 = world.getBlockState(i$);
            
            if(this.displaceIfPossible(world, i$)) {// 93
               world.setBlockToAir(i$);// 94
            }
            int side = this.getQuantaValueBelow(world, i$, lowerthan);// 96
            if(side >= 0) {// 97
               ++count;// 99
               total += side;// 100
            }
         }
         
         if(count == 1) {// 104
            if(changed) {// 106
               world.setBlockState(pos, state.withProperty(LEVEL, Integer.valueOf(quantaRemaining - 1)), 2);// 108
            }

         } else {
            int div = total / count;// 113
            int rem = total % count;// 114
            Iterator horPlane = Plane.HORIZONTAL.iterator();// 116

            while(true) {
               BlockPos off;
               EnumFacing dir;
               int quanta;
               int newFlowSpeed;
               do {
                  if(!horPlane.hasNext()) {
                     if(rem > 0) {// 145
                        ++div;// 147
                     }

                     world.setBlockState(pos, state.withProperty(LEVEL, Integer.valueOf(div - 1)), 2);// 149
                     return;// 150
                  }

                  dir = (EnumFacing)horPlane.next();
                  off = pos.offset(dir);// 118
                  quanta = this.getQuantaValueBelow(world, off, lowerthan);// 119
                  PropertyFloodWater floodwater = this.getProperty(world, off);
               } while(quanta < 0);// 120

               int newquanta = div;// 122
               if(rem == count || rem > 1 && rand.nextInt(count - rem) != 0) {// 123
                  newquanta = div + 1;// 125
                  --rem;// 126
               }

               if(newquanta != quanta) {// 129
                  if(newquanta == 0) {// 131
                     world.setBlockToAir(off);// 133
                  } else if(quanta >= 0){
                	  if(flowSpeed < 10) {
                		  if(quanta != 0) {
                			  world.setBlockState(off, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(newquanta - 1)).withProperty(FLOWSPEED, flowSpeed + (int) quanta != 0 ? newquanta-quanta / quanta: 1).withProperty(FLOWDIRECTION, dir), 2);// 137
                		  }else{
                			  world.setBlockState(off, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(newquanta - 1)).withProperty(FLOWSPEED, flowSpeed + 1).withProperty(FLOWDIRECTION, dir), 2);// 137
                		  }
                	  }else{
                		  world.setBlockState(off, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(newquanta - 1)));
                	  }
                  } else{
                	  world.setBlockState(off, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(newquanta - 1)));
                  }
//TODO i dont like how all this looks in regards to efficiency CLEAN IT UP!
                  world.scheduleUpdate(off, this, this.tickRate);// 139
               }

               --count;// 141
            }
         }
      }
 }

 private PropertyFloodWater getProperty(World world, BlockPos off) {
	IBlockState state = world.getBlockState(off);
	if(state.getBlock() == this) {
	return new PropertyFloodWater((Integer) state.getValue(FLOWSPEED), (EnumFacing) state.getValue(FLOWDIRECTION));
	}else {
	return null;
	}
}

public int tryToFlowVerticallyInto(World world, BlockPos pos, int amtToInput, EnumFacing flowDirection2, int flowspeed) {
      IBlockState myState = world.getBlockState(pos);// 154
      BlockPos other = pos.add(0, this.densityDir, 0);// 155
      if(other.getY() >= 0 && other.getY() < world.getHeight()) {// 156
         int amt = this.getQuantaValueBelow(world, other, this.quantaPerBlock);// 162
         if(flowDirection2 != EnumFacing.DOWN && flowspeed < 10) {
        	 world.setBlockState(pos, this.getDefaultState().withProperty(FLOWSPEED, Integer.valueOf(flowspeed + 1)), 3);//TODO you really need to figure out what these flags do
         }
         if(amt >= 0) {// 163
            amt += amtToInput;// 165
            if(amt > this.quantaPerBlock) {// 166
               world.setBlockState(other, myState.withProperty(LEVEL, Integer.valueOf(this.quantaPerBlock - 1)), 3);// 168
               world.scheduleUpdate(other, this, this.tickRate);// 169
               return amt - this.quantaPerBlock;// 170
            } else if(amt > 0) {// 172
               world.setBlockState(other, myState.withProperty(LEVEL, Integer.valueOf(amt - 1)), 3);// 174
               world.scheduleUpdate(other, this, this.tickRate);// 175
               world.setBlockToAir(pos);// 176
               return 0;// 177
            } else {
               return amtToInput;// 179
            }
         } else {
            int density_other = getDensity(world, other);// 183
            if(density_other == Integer.MAX_VALUE) {// 184
               if(this.displaceIfPossible(world, other)) {// 186
                  world.setBlockState(other, myState.withProperty(LEVEL, Integer.valueOf(amtToInput - 1)), 3);// 188
                  world.scheduleUpdate(other, this, this.tickRate);// 189
                  world.setBlockToAir(pos);// 190
                  return 0;// 191
               } else {
                  return amtToInput;// 195
               }
            } else {
               IBlockState state;
               if(this.densityDir < 0) {// 199
                  if(density_other < this.density) {// 201
                     state = world.getBlockState(other);// 203
                     world.setBlockState(other, myState.withProperty(LEVEL, Integer.valueOf(amtToInput - 1)), 3);// 204
                     world.setBlockState(pos, state, 3);// 205
                     world.scheduleUpdate(other, this, this.tickRate);// 206
                     world.scheduleUpdate(pos, state.getBlock(), state.getBlock().tickRate(world));// 207
                     return 0;// 208
                  }
               } else if(density_other > this.density) {// 213
                  state = world.getBlockState(other);// 215
                  world.setBlockState(other, myState.withProperty(LEVEL, Integer.valueOf(amtToInput - 1)), 3);// 216
                  world.setBlockState(other, state, 3);// 217
                  world.scheduleUpdate(other, this, this.tickRate);// 218
                  world.scheduleUpdate(other, state.getBlock(), state.getBlock().tickRate(world));// 219
                  return 0;// 220
               }

               return amtToInput;// 223
            }
         }
      } else {
         world.setBlockToAir(pos);// 158
         return 0;// 159
      }
}

public String getName() {
	return name;
}

}

Link to comment
Share on other sites

You don't need to override the setDefaultState method, you just need to invoke it (which you're already doing).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

state.withProperty(...)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.