2012-12-09 15:13:38 -06:00
package org.bukkit ;
import static org.hamcrest.Matchers.* ;
2019-04-23 14:54:36 +10:00
import static org.junit.Assert.* ;
import com.google.common.collect.Lists ;
2012-12-09 15:13:38 -06:00
import java.util.List ;
2019-04-23 14:54:36 +10:00
import java.util.Map ;
2021-03-16 09:00:00 +11:00
import net.minecraft.core.BlockPosition ;
import net.minecraft.world.EnumHand ;
2021-04-13 19:16:32 +10:00
import net.minecraft.world.entity.EntityInsentient ;
2021-03-16 09:00:00 +11:00
import net.minecraft.world.entity.player.EntityHuman ;
import net.minecraft.world.item.Item ;
import net.minecraft.world.item.ItemRecord ;
import net.minecraft.world.level.BlockAccessAir ;
import net.minecraft.world.level.block.Block ;
import net.minecraft.world.level.block.BlockFalling ;
import net.minecraft.world.level.block.BlockFire ;
import net.minecraft.world.level.block.Blocks ;
import net.minecraft.world.level.block.entity.TileEntityFurnace ;
import net.minecraft.world.level.block.state.BlockBase ;
import net.minecraft.world.level.block.state.IBlockData ;
import net.minecraft.world.phys.MovingObjectPositionBlock ;
2021-04-13 19:16:32 +10:00
import org.bukkit.craftbukkit.CraftEquipmentSlot ;
2012-12-17 01:31:41 -06:00
import org.bukkit.craftbukkit.inventory.CraftItemStack ;
2019-04-23 14:54:36 +10:00
import org.bukkit.craftbukkit.util.CraftMagicNumbers ;
import org.bukkit.enchantments.EnchantmentTarget ;
2021-04-13 19:16:32 +10:00
import org.bukkit.inventory.EquipmentSlot ;
2012-12-17 01:31:41 -06:00
import org.bukkit.inventory.ItemStack ;
2012-12-14 02:02:02 -06:00
import org.bukkit.support.AbstractTestingBase ;
import org.junit.BeforeClass ;
2012-12-09 15:13:38 -06:00
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.junit.runners.Parameterized ;
import org.junit.runners.Parameterized.Parameter ;
import org.junit.runners.Parameterized.Parameters ;
@RunWith ( Parameterized . class )
2012-12-14 02:02:02 -06:00
public class PerMaterialTest extends AbstractTestingBase {
2014-11-26 08:32:16 +11:00
private static Map < Block , Integer > fireValues ;
2012-12-14 02:02:02 -06:00
@BeforeClass
public static void getFireValues ( ) {
2021-06-11 15:00:00 +10:00
fireValues = ( ( BlockFire ) Blocks . FIRE ) . flameOdds ;
2012-12-09 15:13:38 -06:00
}
2020-04-14 12:34:43 +10:00
@Parameters ( name = " {index}: {0} " )
2012-12-09 15:13:38 -06:00
public static List < Object [ ] > data ( ) {
2012-12-14 02:02:02 -06:00
List < Object [ ] > list = Lists . newArrayList ( ) ;
2012-12-09 15:13:38 -06:00
for ( Material material : Material . values ( ) ) {
2018-07-15 10:00:00 +10:00
if ( ! material . isLegacy ( ) ) {
list . add ( new Object [ ] { material } ) ;
}
2012-12-09 15:13:38 -06:00
}
return list ;
}
@Parameter public Material material ;
2018-07-15 10:00:00 +10:00
@Test
public void isBlock ( ) {
if ( material ! = Material . AIR & & material ! = Material . CAVE_AIR & & material ! = Material . VOID_AIR ) {
2018-08-26 12:00:00 +10:00
assertThat ( material . isBlock ( ) , is ( not ( CraftMagicNumbers . getBlock ( material ) = = null ) ) ) ;
2018-07-15 10:00:00 +10:00
}
}
2012-12-09 15:13:38 -06:00
@Test
public void isSolid ( ) {
if ( material = = Material . AIR ) {
assertFalse ( material . isSolid ( ) ) ;
} else if ( material . isBlock ( ) ) {
2016-03-01 08:32:46 +11:00
assertThat ( material . isSolid ( ) , is ( CraftMagicNumbers . getBlock ( material ) . getBlockData ( ) . getMaterial ( ) . isSolid ( ) ) ) ;
2012-12-09 15:13:38 -06:00
} else {
assertFalse ( material . isSolid ( ) ) ;
}
}
2012-12-14 02:02:02 -06:00
@Test
public void isEdible ( ) {
2019-04-23 12:00:00 +10:00
if ( material . isBlock ( ) ) {
assertFalse ( material . isEdible ( ) ) ;
} else {
assertThat ( material . isEdible ( ) , is ( CraftMagicNumbers . getItem ( material ) . isFood ( ) ) ) ;
}
2012-12-14 02:02:02 -06:00
}
@Test
public void isRecord ( ) {
2013-11-04 07:07:38 -06:00
assertThat ( material . isRecord ( ) , is ( CraftMagicNumbers . getItem ( material ) instanceof ItemRecord ) ) ;
2012-12-14 02:02:02 -06:00
}
@Test
public void maxDurability ( ) {
2015-01-25 11:59:37 +00:00
if ( INVALIDATED_MATERIALS . contains ( material ) ) return ;
2012-12-14 02:02:02 -06:00
if ( material = = Material . AIR ) {
assertThat ( ( int ) material . getMaxDurability ( ) , is ( 0 ) ) ;
2020-04-14 12:34:43 +10:00
} else if ( material . isBlock ( ) ) {
2013-11-04 07:07:38 -06:00
Item item = CraftMagicNumbers . getItem ( material ) ;
assertThat ( ( int ) material . getMaxDurability ( ) , is ( item . getMaxDurability ( ) ) ) ;
2012-12-14 02:02:02 -06:00
}
}
@Test
public void maxStackSize ( ) {
2015-01-25 11:59:37 +00:00
if ( INVALIDATED_MATERIALS . contains ( material ) ) return ;
2012-12-17 01:31:41 -06:00
final ItemStack bukkit = new ItemStack ( material ) ;
final CraftItemStack craft = CraftItemStack . asCraftCopy ( bukkit ) ;
2012-12-14 02:02:02 -06:00
if ( material = = Material . AIR ) {
2012-12-17 01:31:41 -06:00
final int MAX_AIR_STACK = 0 /* Why can't I hold all of these AIR? */ ;
assertThat ( material . getMaxStackSize ( ) , is ( MAX_AIR_STACK ) ) ;
assertThat ( bukkit . getMaxStackSize ( ) , is ( MAX_AIR_STACK ) ) ;
assertThat ( craft . getMaxStackSize ( ) , is ( MAX_AIR_STACK ) ) ;
2012-12-14 02:02:02 -06:00
} else {
2013-11-04 07:07:38 -06:00
assertThat ( material . getMaxStackSize ( ) , is ( CraftMagicNumbers . getItem ( material ) . getMaxStackSize ( ) ) ) ;
2012-12-17 01:31:41 -06:00
assertThat ( bukkit . getMaxStackSize ( ) , is ( material . getMaxStackSize ( ) ) ) ;
assertThat ( craft . getMaxStackSize ( ) , is ( material . getMaxStackSize ( ) ) ) ;
2012-12-14 02:02:02 -06:00
}
}
@Test
public void isTransparent ( ) {
if ( material = = Material . AIR ) {
assertTrue ( material . isTransparent ( ) ) ;
} else if ( material . isBlock ( ) ) {
2018-07-15 10:00:00 +10:00
// assertThat(material.isTransparent(), is(not(CraftMagicNumbers.getBlock(material).getBlockData().getMaterial().blocksLight()))); // PAIL: not unit testable anymore (17w50a)
2012-12-14 02:02:02 -06:00
} else {
assertFalse ( material . isTransparent ( ) ) ;
}
}
@Test
public void isFlammable ( ) {
if ( material ! = Material . AIR & & material . isBlock ( ) ) {
2016-03-01 08:32:46 +11:00
assertThat ( material . isFlammable ( ) , is ( CraftMagicNumbers . getBlock ( material ) . getBlockData ( ) . getMaterial ( ) . isBurnable ( ) ) ) ;
2012-12-14 02:02:02 -06:00
} else {
assertFalse ( material . isFlammable ( ) ) ;
}
}
@Test
public void isBurnable ( ) {
if ( material . isBlock ( ) ) {
2014-11-26 08:32:16 +11:00
Block block = CraftMagicNumbers . getBlock ( material ) ;
assertThat ( material . isBurnable ( ) , is ( fireValues . containsKey ( block ) & & fireValues . get ( block ) > 0 ) ) ;
2012-12-14 02:02:02 -06:00
} else {
assertFalse ( material . isBurnable ( ) ) ;
}
}
2017-03-23 23:18:32 +01:00
@Test
public void isFuel ( ) {
2021-03-16 09:00:00 +11:00
assertThat ( material . isFuel ( ) , is ( TileEntityFurnace . isFuel ( new net . minecraft . world . item . ItemStack ( CraftMagicNumbers . getItem ( material ) ) ) ) ) ;
2017-03-23 23:18:32 +01:00
}
2012-12-14 02:02:02 -06:00
@Test
public void isOccluding ( ) {
if ( material . isBlock ( ) ) {
2020-06-25 10:00:00 +10:00
assertThat ( material . isOccluding ( ) , is ( CraftMagicNumbers . getBlock ( material ) . getBlockData ( ) . isOccluding ( BlockAccessAir . INSTANCE , BlockPosition . ZERO ) ) ) ;
2012-12-14 02:02:02 -06:00
} else {
assertFalse ( material . isOccluding ( ) ) ;
}
}
2013-02-15 02:38:34 +01:00
@Test
public void hasGravity ( ) {
if ( material . isBlock ( ) ) {
2013-11-04 07:07:38 -06:00
assertThat ( material . hasGravity ( ) , is ( CraftMagicNumbers . getBlock ( material ) instanceof BlockFalling ) ) ;
2013-02-15 02:38:34 +01:00
} else {
assertFalse ( material . hasGravity ( ) ) ;
}
}
2016-07-08 12:51:32 +10:00
@Test
public void usesDurability ( ) {
if ( ! material . isBlock ( ) ) {
assertThat ( EnchantmentTarget . BREAKABLE . includes ( material ) , is ( CraftMagicNumbers . getItem ( material ) . usesDurability ( ) ) ) ;
} else {
assertFalse ( EnchantmentTarget . BREAKABLE . includes ( material ) ) ;
}
}
2018-01-16 09:57:47 +11:00
2018-07-15 10:00:00 +10:00
@Test
public void testDurability ( ) {
if ( ! material . isBlock ( ) ) {
assertThat ( material . getMaxDurability ( ) , is ( ( short ) CraftMagicNumbers . getItem ( material ) . getMaxDurability ( ) ) ) ;
} else {
assertThat ( material . getMaxDurability ( ) , is ( ( short ) 0 ) ) ;
}
}
2018-01-16 09:57:47 +11:00
@Test
public void testBlock ( ) {
if ( material = = Material . AIR ) {
assertTrue ( material . isBlock ( ) ) ;
} else {
2018-08-26 12:00:00 +10:00
assertThat ( material . isBlock ( ) , is ( equalTo ( CraftMagicNumbers . getBlock ( material ) ! = null ) ) ) ;
2018-01-16 09:57:47 +11:00
}
}
2019-10-06 18:41:56 +11:00
@Test
public void testAir ( ) {
if ( material . isBlock ( ) ) {
assertThat ( material . isAir ( ) , is ( equalTo ( CraftMagicNumbers . getBlock ( material ) . getBlockData ( ) . isAir ( ) ) ) ) ;
} else {
assertThat ( material . isAir ( ) , is ( equalTo ( false ) ) ) ;
}
}
2018-01-16 09:57:47 +11:00
@Test
2018-02-16 10:33:36 +11:00
public void testItem ( ) {
2018-01-16 09:57:47 +11:00
if ( material = = Material . AIR ) {
assertTrue ( material . isItem ( ) ) ;
} else {
assertThat ( material . isItem ( ) , is ( equalTo ( CraftMagicNumbers . getItem ( material ) ! = null ) ) ) ;
}
}
2018-08-02 15:26:02 +03:00
@Test
public void testInteractable ( ) throws ReflectiveOperationException {
if ( material . isBlock ( ) ) {
assertThat ( material . isInteractable ( ) ,
is ( ! CraftMagicNumbers . getBlock ( material ) . getClass ( )
2021-03-16 09:00:00 +11:00
. getMethod ( " interact " , IBlockData . class , net . minecraft . world . level . World . class , BlockPosition . class , EntityHuman . class , EnumHand . class , MovingObjectPositionBlock . class )
2020-06-25 10:00:00 +10:00
. getDeclaringClass ( ) . equals ( BlockBase . class ) ) ) ;
2018-08-02 15:26:02 +03:00
} else {
assertFalse ( material . isInteractable ( ) ) ;
}
}
2018-08-08 04:06:36 +03:00
@Test
public void testBlockHardness ( ) {
if ( material . isBlock ( ) ) {
2021-06-11 15:00:00 +10:00
assertThat ( material . getHardness ( ) , is ( CraftMagicNumbers . getBlock ( material ) . getBlockData ( ) . destroySpeed ) ) ;
2018-08-08 04:06:36 +03:00
}
}
@Test
public void testBlastResistance ( ) {
if ( material . isBlock ( ) ) {
2018-08-26 12:00:00 +10:00
assertThat ( material . getBlastResistance ( ) , is ( CraftMagicNumbers . getBlock ( material ) . getDurability ( ) ) ) ;
2018-08-08 04:06:36 +03:00
}
}
2018-08-25 01:35:40 -04:00
2021-06-12 21:07:45 +01:00
@Test
public void testSlipperiness ( ) {
if ( material . isBlock ( ) ) {
assertThat ( material . getSlipperiness ( ) , is ( CraftMagicNumbers . getBlock ( material ) . getFrictionFactor ( ) ) ) ;
}
}
2018-08-25 01:35:40 -04:00
@Test
public void testBlockDataCreation ( ) {
if ( material . isBlock ( ) ) {
assertNotNull ( material . createBlockData ( ) ) ;
}
}
2020-05-28 19:31:27 +10:00
@Test
public void testCraftingRemainingItem ( ) {
if ( material . isItem ( ) ) {
2020-06-25 10:00:00 +10:00
Item expectedItem = CraftMagicNumbers . getItem ( material ) . getCraftingRemainingItem ( ) ;
2020-05-28 19:31:27 +10:00
Material expected = expectedItem = = null ? null : CraftMagicNumbers . getMaterial ( expectedItem ) ;
assertThat ( material . getCraftingRemainingItem ( ) , is ( expected ) ) ;
}
}
2021-04-13 19:16:32 +10:00
@Test
public void testEquipmentSlot ( ) {
if ( material . isItem ( ) ) {
2021-06-11 15:00:00 +10:00
EquipmentSlot expected = CraftEquipmentSlot . getSlot ( EntityInsentient . getEquipmentSlotForItem ( CraftItemStack . asNMSCopy ( new ItemStack ( material ) ) ) ) ;
2021-04-13 19:16:32 +10:00
assertThat ( material . getEquipmentSlot ( ) , is ( expected ) ) ;
}
}
2012-12-09 15:13:38 -06:00
}