Revert some no longer needed changes from earlier Material changes

This commit is contained in:
DerFrZocker 2023-09-05 19:14:20 +02:00
parent 2ee3cca8b6
commit 4d26fc9aa6
No known key found for this signature in database
GPG key ID: 713F71FFFE1DDF91
4 changed files with 165 additions and 108 deletions

View file

@ -76,18 +76,21 @@ public class Crops extends MaterialData {
* @return CropState of this crop * @return CropState of this crop
*/ */
public CropState getState() { public CropState getState() {
if (getItemType() == Material.LEGACY_CROPS || getItemType() == Material.LEGACY_CARROT || getItemType() == Material.LEGACY_POTATO) { switch (getItemType()) {
case LEGACY_CROPS:
case LEGACY_CARROT:
case LEGACY_POTATO:
// Mask the data just in case top bit set // Mask the data just in case top bit set
return CropState.getByData((byte) (getData() & 0x7)); return CropState.getByData((byte) (getData() & 0x7));
} case LEGACY_BEETROOT_BLOCK:
case LEGACY_NETHER_WARTS:
if (getItemType() == Material.LEGACY_BEETROOT_BLOCK || getItemType() == Material.LEGACY_NETHER_WARTS) {
// Mask the data just in case top bits are set // Mask the data just in case top bits are set
// Will return SEEDED, SMALL, TALL, RIPE for the three growth data values // Will return SEEDED, SMALL, TALL, RIPE for the three growth data values
return CropState.getByData((byte) (((getData() & 0x3) * 7 + 2) / 3)); return CropState.getByData((byte) (((getData() & 0x3) * 7 + 2) / 3));
} default:
throw new IllegalArgumentException("Block type is not a crop"); throw new IllegalArgumentException("Block type is not a crop");
} }
}
/** /**
* Sets the growth state of this crop * Sets the growth state of this crop
@ -104,20 +107,22 @@ public class Crops extends MaterialData {
* @param state New growth state of this crop * @param state New growth state of this crop
*/ */
public void setState(CropState state) { public void setState(CropState state) {
if (getItemType() == Material.LEGACY_CROPS || getItemType() == Material.LEGACY_CARROT || getItemType() == Material.LEGACY_POTATO) { switch (getItemType()) {
case LEGACY_CROPS:
case LEGACY_CARROT:
case LEGACY_POTATO:
// Preserve the top bit in case it is set // Preserve the top bit in case it is set
setData((byte) ((getData() & 0x8) | state.getData())); setData((byte) ((getData() & 0x8) | state.getData()));
return; break;
} case LEGACY_NETHER_WARTS:
case LEGACY_BEETROOT_BLOCK:
if (getItemType() == Material.LEGACY_NETHER_WARTS || getItemType() == Material.LEGACY_BEETROOT_BLOCK) {
// Preserve the top bits in case they are set // Preserve the top bits in case they are set
setData((byte) ((getData() & 0xC) | (state.getData() >> 1))); setData((byte) ((getData() & 0xC) | (state.getData() >> 1)));
return; break;
} default:
throw new IllegalArgumentException("Block type is not a crop"); throw new IllegalArgumentException("Block type is not a crop");
} }
}
@Override @Override
public String toString() { public String toString() {

View file

@ -71,24 +71,23 @@ public class Wood extends MaterialData {
* @return TreeSpecies of this wood block * @return TreeSpecies of this wood block
*/ */
public TreeSpecies getSpecies() { public TreeSpecies getSpecies() {
if (getItemType() == Material.LEGACY_WOOD || getItemType() == Material.LEGACY_WOOD_DOUBLE_STEP) { switch (getItemType()) {
case LEGACY_WOOD:
case LEGACY_WOOD_DOUBLE_STEP:
return TreeSpecies.getByData((byte) getData()); return TreeSpecies.getByData((byte) getData());
} case LEGACY_LOG:
case LEGACY_LEAVES:
if (getItemType() == Material.LEGACY_LOG || getItemType() == Material.LEGACY_LEAVES) {
return TreeSpecies.getByData((byte) (getData() & 0x3)); return TreeSpecies.getByData((byte) (getData() & 0x3));
} case LEGACY_LOG_2:
case LEGACY_LEAVES_2:
if (getItemType() == Material.LEGACY_LOG_2 || getItemType() == Material.LEGACY_LEAVES_2) {
return TreeSpecies.getByData((byte) ((getData() & 0x3) | 0x4)); return TreeSpecies.getByData((byte) ((getData() & 0x3) | 0x4));
} case LEGACY_SAPLING:
case LEGACY_WOOD_STEP:
if (getItemType() == Material.LEGACY_SAPLING || getItemType() == Material.LEGACY_WOOD_STEP) {
return TreeSpecies.getByData((byte) (getData() & 0x7)); return TreeSpecies.getByData((byte) (getData() & 0x7));
} default:
throw new IllegalArgumentException("Invalid block type for tree species"); throw new IllegalArgumentException("Invalid block type for tree species");
} }
}
/** /**
* Correct the block type for certain species-type combinations. * Correct the block type for certain species-type combinations.
@ -103,20 +102,22 @@ public class Wood extends MaterialData {
case REDWOOD: case REDWOOD:
case BIRCH: case BIRCH:
case JUNGLE: case JUNGLE:
if (type == Material.LEGACY_LOG_2) { switch (type) {
case LEGACY_LOG_2:
return Material.LEGACY_LOG; return Material.LEGACY_LOG;
} case LEGACY_LEAVES_2:
if (type == Material.LEGACY_LEAVES_2) {
return Material.LEGACY_LEAVES; return Material.LEGACY_LEAVES;
default:
} }
break; break;
case ACACIA: case ACACIA:
case DARK_OAK: case DARK_OAK:
if (type == Material.LEGACY_LOG) { switch (type) {
case LEGACY_LOG:
return Material.LEGACY_LOG_2; return Material.LEGACY_LOG_2;
} case LEGACY_LEAVES:
if (type == Material.LEGACY_LEAVES) {
return Material.LEGACY_LEAVES_2; return Material.LEGACY_LEAVES_2;
default:
} }
break; break;
} }
@ -130,18 +131,17 @@ public class Wood extends MaterialData {
*/ */
public void setSpecies(final TreeSpecies species) { public void setSpecies(final TreeSpecies species) {
boolean firstType = false; boolean firstType = false;
switch (getItemType()) {
if (getItemType() == Material.LEGACY_WOOD || getItemType() == Material.LEGACY_WOOD_DOUBLE_STEP) { case LEGACY_WOOD:
case LEGACY_WOOD_DOUBLE_STEP:
setData(species.getData()); setData(species.getData());
return; break;
} case LEGACY_LOG:
case LEGACY_LEAVES:
if (getItemType() == Material.LEGACY_LOG || getItemType() == Material.LEGACY_LEAVES) {
firstType = true; firstType = true;
// fall through to next switch statement below // fall through to next switch statement below
} case LEGACY_LOG_2:
case LEGACY_LEAVES_2:
if (getItemType() == Material.LEGACY_LOG || getItemType() == Material.LEGACY_LEAVES || getItemType() == Material.LEGACY_LOG_2 || getItemType() == Material.LEGACY_LEAVES_2) {
switch (species) { switch (species) {
case GENERIC: case GENERIC:
case REDWOOD: case REDWOOD:
@ -159,16 +159,15 @@ public class Wood extends MaterialData {
break; break;
} }
setData((byte) ((getData() & 0xC) | (species.getData() & 0x3))); setData((byte) ((getData() & 0xC) | (species.getData() & 0x3)));
return; break;
} case LEGACY_SAPLING:
case LEGACY_WOOD_STEP:
if (getItemType() == Material.LEGACY_SAPLING || getItemType() == Material.LEGACY_WOOD_STEP) {
setData((byte) ((getData() & 0x8) | species.getData())); setData((byte) ((getData() & 0x8) | species.getData()));
return; break;
} default:
throw new IllegalArgumentException("Invalid block type for tree species"); throw new IllegalArgumentException("Invalid block type for tree species");
} }
}
@Override @Override
public String toString() { public String toString() {

View file

@ -2,18 +2,71 @@ package org.bukkit;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.bukkit.support.AbstractTestingBase; import org.bukkit.material.MaterialData;
import org.junit.Test; import org.junit.Test;
public class MaterialTest extends AbstractTestingBase { public class MaterialTest {
@Test
public void getByName() {
for (Material material : Material.values()) {
assertThat(Material.getMaterial(material.toString()), is(material));
}
}
@Test @Test
public void getByNameNull() { public void getByNameNull() {
assertThat(Material.getMaterial(null), is(nullValue())); assertThat(Material.getMaterial(null), is(nullValue()));
} }
@Test
public void getData() {
for (Material material : Material.values()) {
if (!material.isLegacy()) {
continue;
}
Class<? extends MaterialData> clazz = material.getData();
assertThat(material.getNewData((byte) 0), is(instanceOf(clazz)));
}
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void matchMaterialByNull() { public void matchMaterialByNull() {
Material.matchMaterial(null); Material.matchMaterial(null);
} }
@Test
public void matchMaterialByName() {
for (Material material : Material.values()) {
assertThat(Material.matchMaterial(material.toString()), is(material));
}
}
@Test
public void matchMaterialByKey() {
for (Material material : Material.values()) {
if (material.isLegacy()) {
continue;
}
assertThat(Material.matchMaterial(material.getKey().toString()), is(material));
}
}
@Test
public void matchMaterialByWrongNamespace() {
for (Material material : Material.values()) {
if (material.isLegacy()) {
continue;
}
assertNull(Material.matchMaterial("bogus:" + material.getKey().getKey()));
}
}
@Test
public void matchMaterialByLowerCaseAndSpaces() {
for (Material material : Material.values()) {
String name = material.toString().replaceAll("_", " ").toLowerCase(java.util.Locale.ENGLISH);
assertThat(Material.matchMaterial(name), is(material));
}
}
} }