Skip to content

Commit

Permalink
Replace reused code with single function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruling-0 committed Jan 20, 2025
1 parent 2e1c4cc commit fd5a3e1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 76 deletions.
40 changes: 15 additions & 25 deletions src/main/java/mods/natura/blocks/NSlabBase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package mods.natura.blocks;

import mods.natura.blocks.trees.Planks;
import mods.natura.items.blocks.PlanksItem;
import net.minecraft.block.BlockWoodSlab;
import net.minecraft.init.Blocks;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
Expand All @@ -12,13 +13,10 @@
import mods.natura.common.NaturaTab;

public abstract class NSlabBase extends BlockWoodSlab {

public static String[] woodNames = new String[] { "eucalyptus", "sakura", "ghost", "redwood", "blood", "bush",
"maple", "silverbell", "purpleheart", "tiger", "willow", "darkwood", "fusewood" };
// group 1 is the first 8 types, group 2 is the next 8, etc.
// slabs are max of 8 per group due to vanilla use of metadata, so this variable
// maps slabs to the overall wood metadata used elsewhere, such as for textures and flammability
private final int group;
// metaGroup 1 is the first 8 types, metaGroup 2 is the next 8, etc.
// slabs are max of 8 per group due to vanilla use of metadata, so this variable maps
// wooden slabs to the overall wood metadata used elsewhere, such as for textures and flammability
private final int metaGroup;

public NSlabBase(boolean isDoubleSlab, int grp) {
super(isDoubleSlab);
Expand All @@ -28,7 +26,7 @@ public NSlabBase(boolean isDoubleSlab, int grp) {
if (!isDoubleSlab) {
this.setCreativeTab(NaturaTab.tab);
}
group = grp;
metaGroup = grp;
}

@Override
Expand All @@ -40,33 +38,25 @@ public IIcon getIcon(int side, int meta) {
@Override
public String func_150002_b(int meta) {
// unlocalized name
return "block.wood." + woodNames[getWoodMeta(meta)] + ".slab";
meta = getWoodMeta(meta);
return "block.wood." + PlanksItem.blockType[meta] + ".slab";
}

@Override
public int getFlammability(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = getWoodMeta(world.getBlockMetadata(x, y, z));
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getFlammability(this);
int meta = getWoodMeta(world.getBlockMetadata(x, y, z));
return Planks.getPlankFlammability(this, meta);
}

@Override
public int getFireSpreadSpeed(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = getWoodMeta(world.getBlockMetadata(x, y, z));
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getEncouragement(this);
}

@Override
public boolean isFlammable(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = getWoodMeta(world.getBlockMetadata(x, y, z));
if (metadata == 2 || metadata == 4 || metadata > 10) return false;
return getFlammability(world, x, y, z, face) > 0;
int meta = getWoodMeta(world.getBlockMetadata(x, y, z));
return Planks.getPlankFireSpreadSpeed(this, meta);
}

private int getWoodMeta(int meta) {
meta = (meta & 7) + (group - 1) * 8;
if (meta < 0 || meta >= woodNames.length) meta = 0;
meta = (meta & 7) + (metaGroup - 1) * 8;
if (meta < 0 || meta >= PlanksItem.blockType.length) meta = 0;
return meta;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import cpw.mods.fml.relauncher.SideOnly;
import mods.natura.common.NContent;

public class NSlab1 extends NSlabBase {
public class PlankSlab1 extends NSlabBase {

public NSlab1(boolean isDoubleSlab) {
public PlankSlab1(boolean isDoubleSlab) {
super(isDoubleSlab, 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import cpw.mods.fml.relauncher.SideOnly;
import mods.natura.common.NContent;

public class NSlab2 extends NSlabBase {
public class PlankSlab2 extends NSlabBase {

public NSlab2(boolean isDoubleSlab) {
public PlankSlab2(boolean isDoubleSlab) {
super(isDoubleSlab, 2);
}

Expand Down
18 changes: 5 additions & 13 deletions src/main/java/mods/natura/blocks/overrides/AlternateBookshelf.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import mods.natura.blocks.trees.Planks;
import net.minecraft.block.BlockBookshelf;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
Expand Down Expand Up @@ -45,23 +46,14 @@ public void registerBlockIcons(IIconRegister iconRegister) {

@Override
public int getFlammability(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getFlammability(this);
int meta = world.getBlockMetadata(x, y, z);
return Planks.getPlankFlammability(this, meta);
}

@Override
public int getFireSpreadSpeed(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getEncouragement(this);
}

@Override
public boolean isFlammable(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return false;
return getFlammability(world, x, y, z, face) > 0;
int meta = world.getBlockMetadata(x, y, z);
return Planks.getPlankFireSpreadSpeed(this, meta);
}

@Override
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/mods/natura/blocks/overrides/AlternateFence.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import mods.natura.blocks.trees.Planks;
import net.minecraft.block.BlockFence;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
Expand Down Expand Up @@ -48,23 +49,14 @@ public void getSubBlocks(Item item, CreativeTabs tabs, List list) {

@Override
public int getFlammability(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getFlammability(this);
int meta = world.getBlockMetadata(x, y, z);
return Planks.getPlankFlammability(this, meta);
}

@Override
public int getFireSpreadSpeed(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getEncouragement(this);
}

@Override
public boolean isFlammable(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return false;
return getFlammability(world, x, y, z, face) > 0;
int meta = world.getBlockMetadata(x, y, z);
return Planks.getPlankFireSpreadSpeed(this, meta);
}

@Override
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/mods/natura/blocks/trees/Planks.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@ public void registerBlockIcons(IIconRegister iconRegister) {

@Override
public int getFlammability(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getFlammability(this);
int meta = world.getBlockMetadata(x, y, z);
return getPlankFlammability(this, meta);
}

public static int getPlankFlammability(Block block, int meta) {
if (meta == 2 || meta == 4 || meta > 10) return 0;
return Blocks.fire.getFlammability(block);
}

@Override
public int getFireSpreadSpeed(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return 0;
return Blocks.fire.getEncouragement(this);
int meta = world.getBlockMetadata(x, y, z);
return getPlankFireSpreadSpeed(this, meta);
}

@Override
public boolean isFlammable(IBlockAccess world, int x, int y, int z, ForgeDirection face) {
int metadata = world.getBlockMetadata(x, y, z);
if (metadata == 2 || metadata == 4 || metadata > 10) return false;
return getFlammability(world, x, y, z, face) > 0;
public static int getPlankFireSpreadSpeed(Block block, int meta) {
if (meta == 2 || meta == 4 || meta > 10) return 0;
return Blocks.fire.getEncouragement(block);
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/mods/natura/common/NContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import mods.natura.blocks.NButton;
import mods.natura.blocks.NFenceGate;
import mods.natura.blocks.NPressurePlate;
import mods.natura.blocks.NSlab1;
import mods.natura.blocks.NSlab2;
import mods.natura.blocks.PlankSlab1;
import mods.natura.blocks.PlankSlab2;
import mods.natura.blocks.NStairs;
import mods.natura.blocks.NTrapdoor;
import mods.natura.blocks.crops.BerryBush;
Expand Down Expand Up @@ -404,16 +404,16 @@ public void preInit() {

// Wooden Slabs
if (PHNatura.enableWoodenSlabs) {
NSlab1 dslab1 = (NSlab1) new NSlab1(true).setBlockName("plankSlab1Double");
NSlab1 sSlab1 = (NSlab1) new NSlab1(false).setBlockName("plankSlab1");
PlankSlab1 dslab1 = (PlankSlab1) new PlankSlab1(true).setBlockName("plankSlab1Double");
PlankSlab1 sSlab1 = (PlankSlab1) new PlankSlab1(false).setBlockName("plankSlab1");
plankSlab1Double = GameRegistry
.registerBlock(dslab1, PlankSlab1Item.class, "plankSlab1Double", sSlab1, dslab1);
plankSlab1 = GameRegistry.registerBlock(sSlab1, PlankSlab1Item.class, "plankSlab1", sSlab1, dslab1);
Blocks.fire.setFireInfo(plankSlab1, 5, 20);
Blocks.fire.setFireInfo(plankSlab1Double, 5, 20);

NSlab2 dslab2 = (NSlab2) new NSlab2(true).setBlockName("plankSlab2Double");
NSlab2 sSlab2 = (NSlab2) new NSlab2(false).setBlockName("plankSlab2");
PlankSlab2 dslab2 = (PlankSlab2) new PlankSlab2(true).setBlockName("plankSlab2Double");
PlankSlab2 sSlab2 = (PlankSlab2) new PlankSlab2(false).setBlockName("plankSlab2");
plankSlab2Double = GameRegistry
.registerBlock(dslab2, PlankSlab2Item.class, "plankSlab2Double", sSlab2, dslab2);
plankSlab2 = GameRegistry.registerBlock(sSlab2, PlankSlab2Item.class, "plankSlab2", sSlab2, dslab2);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/mods/natura/items/blocks/PlankSlab1Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mods.natura.blocks.NSlab1;
import mods.natura.blocks.PlankSlab1;

public class PlankSlab1Item extends ItemSlab {

public PlankSlab1Item(Block block, NSlab1 singleSlab, NSlab1 doubleSlab) {
public PlankSlab1Item(Block block, PlankSlab1 singleSlab, PlankSlab1 doubleSlab) {
super(block, singleSlab, doubleSlab, block == doubleSlab);
}

Expand All @@ -29,12 +29,14 @@ public void addInformation(ItemStack stack, EntityPlayer player, List list, bool
list.add(StatCollector.translateToLocal("tooltip.tree2"));
break;
case 2:
list.add(StatCollector.translateToLocal("tooltip.nethertree"));
list.add(StatCollector.translateToLocal("tooltip.tree3"));
break;
case 3:
list.add(StatCollector.translateToLocal("tooltip.tree4"));
break;
case 4:
list.add(StatCollector.translateToLocal("tooltip.nethertree"));
list.add(StatCollector.translateToLocal("tooltip.tree5"));
break;
case 5:
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mods/natura/items/blocks/PlankSlab2Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mods.natura.blocks.NSlab2;
import mods.natura.blocks.PlankSlab2;

public class PlankSlab2Item extends ItemSlab {

public PlankSlab2Item(Block block, NSlab2 singleSlab, NSlab2 doubleSlab) {
public PlankSlab2Item(Block block, PlankSlab2 singleSlab, PlankSlab2 doubleSlab) {
super(block, singleSlab, doubleSlab, block == doubleSlab);
}

Expand Down

0 comments on commit fd5a3e1

Please sign in to comment.