Update to new forge rarity system

This commit is contained in:
Shadows_of_Fire 2019-05-02 03:08:15 -04:00
parent 1ce4d21159
commit 95a25dbfc4
21 changed files with 466 additions and 225 deletions

View file

@ -24,17 +24,18 @@ import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockCrystal extends BlockBase{
public class BlockCrystal extends BlockBase {
public static final TheCrystals[] ALL_CRYSTALS = TheCrystals.values();
private static final PropertyEnum<TheCrystals> TYPE = PropertyEnum.create("type", TheCrystals.class);
private final boolean isEmpowered;
public BlockCrystal(String name, boolean isEmpowered){
public BlockCrystal(String name, boolean isEmpowered) {
super(Material.ROCK, name);
this.isEmpowered = isEmpowered;
this.setHardness(1.5F);
@ -43,66 +44,66 @@ public class BlockCrystal extends BlockBase{
}
@Override
public int damageDropped(IBlockState state){
public int damageDropped(IBlockState state) {
return this.getMetaFromState(state);
}
@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list){
for(int j = 0; j < ALL_CRYSTALS.length; j++){
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list) {
for (int j = 0; j < ALL_CRYSTALS.length; j++) {
list.add(new ItemStack(this, 1, j));
}
}
@Override
protected ItemBlockBase getItemBlock(){
protected ItemBlockBase getItemBlock() {
return new TheItemBlock(this);
}
@Override
public void registerRendering(){
for(int i = 0; i < ALL_CRYSTALS.length; i++){
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this, 1, i), this.getRegistryName(), TYPE.getName()+"="+ALL_CRYSTALS[i].name);
public void registerRendering() {
for (int i = 0; i < ALL_CRYSTALS.length; i++) {
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this, 1, i), this.getRegistryName(), TYPE.getName() + "=" + ALL_CRYSTALS[i].name);
}
}
@Override
public IBlockState getStateFromMeta(int meta){
public IBlockState getStateFromMeta(int meta) {
return this.getDefaultState().withProperty(TYPE, TheCrystals.values()[meta]);
}
@Override
public int getMetaFromState(IBlockState state){
public int getMetaFromState(IBlockState state) {
return state.getValue(TYPE).ordinal();
}
@Override
protected BlockStateContainer createBlockState(){
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, TYPE);
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getRarity(ItemStack stack) {
return stack.getItemDamage() >= ALL_CRYSTALS.length ? EnumRarity.COMMON : ALL_CRYSTALS[stack.getItemDamage()].rarity;
}
public static class TheItemBlock extends ItemBlockBase{
public static class TheItemBlock extends ItemBlockBase {
public TheItemBlock(Block block){
public TheItemBlock(Block block) {
super(block);
this.setHasSubtypes(true);
this.setMaxDamage(0);
}
@Override
public String getTranslationKey(ItemStack stack){
return stack.getItemDamage() >= ALL_CRYSTALS.length ? StringUtil.BUGGED_ITEM_NAME : this.getTranslationKey()+"_"+ALL_CRYSTALS[stack.getItemDamage()].name;
public String getTranslationKey(ItemStack stack) {
return stack.getItemDamage() >= ALL_CRYSTALS.length ? StringUtil.BUGGED_ITEM_NAME : this.getTranslationKey() + "_" + ALL_CRYSTALS[stack.getItemDamage()].name;
}
@Override
public boolean hasEffect(ItemStack stack){
return this.block instanceof BlockCrystal && ((BlockCrystal)this.block).isEmpowered;
public boolean hasEffect(ItemStack stack) {
return this.block instanceof BlockCrystal && ((BlockCrystal) this.block).isEmpowered;
}
}
}

View file

@ -17,6 +17,7 @@ import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.IRarity;
public class BlockBase extends Block implements ItemBlockBase.ICustomRarity, IHasModel{
@ -51,7 +52,7 @@ public class BlockBase extends Block implements ItemBlockBase.ICustomRarity, IHa
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getRarity(ItemStack stack){
return EnumRarity.COMMON;
}
}

View file

@ -12,43 +12,40 @@ package de.ellpeck.actuallyadditions.mod.blocks.base;
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.block.Block;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.IRarity;
public class ItemBlockBase extends ItemBlock{
public class ItemBlockBase extends ItemBlock {
public ItemBlockBase(Block block){
public ItemBlockBase(Block block) {
super(block);
this.setHasSubtypes(false);
this.setMaxDamage(0);
}
@Override
public String getTranslationKey(ItemStack stack){
public String getTranslationKey(ItemStack stack) {
return this.getTranslationKey();
}
@Override
public int getMetadata(int damage){
public int getMetadata(int damage) {
return damage;
}
@Override
public EnumRarity getRarity(ItemStack stack){
if(this.block instanceof ICustomRarity){
return ((ICustomRarity)this.block).getRarity(stack);
}
else{
public IRarity getForgeRarity(ItemStack stack) {
if (this.block instanceof ICustomRarity) {
return ((ICustomRarity) this.block).getRarity(stack);
} else {
return Util.FALLBACK_RARITY;
}
}
public interface ICustomRarity{
public interface ICustomRarity {
EnumRarity getRarity(ItemStack stack);
IRarity getRarity(ItemStack stack);
}
}

View file

@ -63,7 +63,7 @@ public class ItemDisplay{
for(int k = 0; k < list.size(); ++k){
if(k == 0){
list.set(k, this.stack.getRarity().color+list.get(k));
list.set(k, this.stack.getItem().getForgeRarity(this.stack).getColor()+list.get(k));
}
else{
list.set(k, TextFormatting.GRAY+list.get(k));

View file

@ -10,9 +10,13 @@
package de.ellpeck.actuallyadditions.mod.booklet.page;
import java.util.Arrays;
import java.util.List;
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.booklet.gui.GuiBooklet;
import de.ellpeck.actuallyadditions.mod.util.RefHelp;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import de.ellpeck.actuallyadditions.mod.util.Util;
@ -23,15 +27,11 @@ import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.fml.client.config.GuiUtils;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import java.util.Arrays;
import java.util.List;
public class PageCrafting extends BookletPage{
private final List<IRecipe> recipes;
@ -152,8 +152,8 @@ public class PageCrafting extends BookletPage{
else if(recipe instanceof ShapedOreRecipe){
ShapedOreRecipe shaped = (ShapedOreRecipe)recipe;
try{
width = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 4);
height = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 5);
width = RefHelp.getPrivateValue(ShapedOreRecipe.class, shaped, 4);
height = RefHelp.getPrivateValue(ShapedOreRecipe.class, shaped, 5);
}
catch(Exception e){
ActuallyAdditions.LOGGER.error("Something went wrong trying to get the Crafting Recipe in the booklet to display!", e);

View file

@ -10,6 +10,11 @@
package de.ellpeck.actuallyadditions.mod.items;
import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.Sets;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues;
import de.ellpeck.actuallyadditions.mod.items.base.ItemToolAA;
@ -22,7 +27,6 @@ import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
@ -30,86 +34,77 @@ import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.Sets;
public class ItemAllToolAA extends ItemToolAA implements IColorProvidingItem{
public class ItemAllToolAA extends ItemToolAA implements IColorProvidingItem {
public final int color;
public ItemAllToolAA(ToolMaterial toolMat, String repairItem, String unlocalizedName, EnumRarity rarity, int color){
public ItemAllToolAA(ToolMaterial toolMat, String repairItem, String unlocalizedName, IRarity rarity, int color) {
super(4.0F, -2F, toolMat, repairItem, unlocalizedName, rarity, new HashSet<>());
this.color = color;
this.setMaxDamage(toolMat.getMaxUses()*4);
this.setMaxDamage(toolMat.getMaxUses() * 4);
this.setHarvestLevels(toolMat.getHarvestLevel());
}
public ItemAllToolAA(ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, EnumRarity rarity, int color){
public ItemAllToolAA(ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, IRarity rarity, int color) {
super(4.0F, -2F, toolMat, repairItem, unlocalizedName, rarity, new HashSet<>());
this.color = color;
this.setMaxDamage(toolMat.getMaxUses()*4);
this.setMaxDamage(toolMat.getMaxUses() * 4);
this.setHarvestLevels(toolMat.getHarvestLevel());
}
private void setHarvestLevels(int amount){
for(String s : this.getToolClasses(null)){
private void setHarvestLevels(int amount) {
for (String s : this.getToolClasses(null)) {
this.setHarvestLevel(s, amount);
}
}
@Override
protected void registerRendering(){
protected void registerRendering() {
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this), new ResourceLocation(ActuallyAdditions.MODID, "item_paxel"), "inventory");
}
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ){
if(!playerIn.isSneaking()) return Items.IRON_HOE.onItemUse(playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ);
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (!playerIn.isSneaking()) return Items.IRON_HOE.onItemUse(playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ);
return Items.IRON_SHOVEL.onItemUse(playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ);
}
@Override
public boolean canHarvestBlock(IBlockState state, ItemStack stack){
public boolean canHarvestBlock(IBlockState state, ItemStack stack) {
return this.hasExtraWhitelist(state.getBlock()) || state.getMaterial().isToolNotRequired() || state.getBlock() == Blocks.SNOW_LAYER || state.getBlock() == Blocks.SNOW || (state.getBlock() == Blocks.OBSIDIAN ? this.toolMaterial.getHarvestLevel() >= 3 : state.getBlock() != Blocks.DIAMOND_BLOCK && state.getBlock() != Blocks.DIAMOND_ORE ? state.getBlock() != Blocks.EMERALD_ORE && state.getBlock() != Blocks.EMERALD_BLOCK ? state.getBlock() != Blocks.GOLD_BLOCK && state.getBlock() != Blocks.GOLD_ORE ? state.getBlock() != Blocks.IRON_BLOCK && state.getBlock() != Blocks.IRON_ORE ? state.getBlock() != Blocks.LAPIS_BLOCK && state.getBlock() != Blocks.LAPIS_ORE ? state.getBlock() != Blocks.REDSTONE_ORE && state.getBlock() != Blocks.LIT_REDSTONE_ORE ? state.getMaterial() == Material.ROCK || state.getMaterial() == Material.IRON || state.getMaterial() == Material.ANVIL : this.toolMaterial.getHarvestLevel() >= 2 : this.toolMaterial.getHarvestLevel() >= 1 : this.toolMaterial.getHarvestLevel() >= 1 : this.toolMaterial.getHarvestLevel() >= 2 : this.toolMaterial.getHarvestLevel() >= 2 : this.toolMaterial.getHarvestLevel() >= 2);
}
private boolean hasExtraWhitelist(Block block){
private boolean hasExtraWhitelist(Block block) {
String name = block.getRegistryName().toString();
for(String list : ConfigStringListValues.PAXEL_EXTRA_MINING_WHITELIST.getValue()){
if(list.equals(name)){
return true;
}
for (String list : ConfigStringListValues.PAXEL_EXTRA_MINING_WHITELIST.getValue()) {
if (list.equals(name)) { return true; }
}
return false;
}
@Override
public Set<String> getToolClasses(ItemStack stack){
public Set<String> getToolClasses(ItemStack stack) {
return Sets.newHashSet("pickaxe", "axe", "shovel");
}
@Override
public float getDestroySpeed(ItemStack stack, IBlockState state){
if(state.getBlock() == Blocks.WEB){
public float getDestroySpeed(ItemStack stack, IBlockState state) {
if (state.getBlock() == Blocks.WEB) {
return 15.0F;
}
else{
} else {
return this.hasExtraWhitelist(state.getBlock()) || state.getBlock().getHarvestTool(state) == null || state.getBlock().getHarvestTool(state).isEmpty() || this.getToolClasses(stack).contains(state.getBlock().getHarvestTool(state)) ? this.efficiency : 1.0F;
}
}
@SideOnly(Side.CLIENT)
@Override
public IItemColor getItemColor(){
public IItemColor getItemColor() {
return (stack, pass) -> pass > 0 ? ItemAllToolAA.this.color : 0xFFFFFF;
}

View file

@ -10,29 +10,30 @@
package de.ellpeck.actuallyadditions.mod.items;
import java.util.Collections;
import java.util.Set;
import com.google.common.collect.Sets;
import de.ellpeck.actuallyadditions.mod.items.base.ItemToolAA;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import java.util.Collections;
import java.util.Set;
import net.minecraftforge.common.IRarity;
public class ItemAxeAA extends ItemToolAA{
private static final Set<Block> EFFECTIVE_ON = Sets.newHashSet(Blocks.PLANKS, Blocks.BOOKSHELF, Blocks.LOG, Blocks.LOG2, Blocks.CHEST, Blocks.PUMPKIN, Blocks.LIT_PUMPKIN, Blocks.MELON_BLOCK, Blocks.LADDER, Blocks.WOODEN_BUTTON, Blocks.WOODEN_PRESSURE_PLATE);
public ItemAxeAA(Item.ToolMaterial material, String repairItem, String unlocalizedName, EnumRarity rarity){
public ItemAxeAA(Item.ToolMaterial material, String repairItem, String unlocalizedName, IRarity rarity){
super(6.0F, -3.0F, material, repairItem, unlocalizedName, rarity, EFFECTIVE_ON);
this.setHarvestLevel("axe", material.getHarvestLevel());
}
public ItemAxeAA(Item.ToolMaterial material, ItemStack repairItem, String unlocalizedName, EnumRarity rarity){
public ItemAxeAA(Item.ToolMaterial material, ItemStack repairItem, String unlocalizedName, IRarity rarity){
super(6.0F, -3.0F, material, repairItem, unlocalizedName, rarity, EFFECTIVE_ON);
this.setHarvestLevel("axe", material.getHarvestLevel());
}

View file

@ -19,14 +19,15 @@ import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemCrystal extends ItemBase{
public class ItemCrystal extends ItemBase {
private final boolean isEmpowered;
public ItemCrystal(String name, boolean isEmpowered){
public ItemCrystal(String name, boolean isEmpowered) {
super(name);
this.isEmpowered = isEmpowered;
this.setHasSubtypes(true);
@ -34,40 +35,39 @@ public class ItemCrystal extends ItemBase{
}
@Override
public int getMetadata(int damage){
public int getMetadata(int damage) {
return damage;
}
@Override
public boolean hasEffect(ItemStack stack){
public boolean hasEffect(ItemStack stack) {
return this.isEmpowered;
}
@Override
public String getTranslationKey(ItemStack stack){
return stack.getItemDamage() >= BlockCrystal.ALL_CRYSTALS.length ? StringUtil.BUGGED_ITEM_NAME : this.getTranslationKey()+"_"+BlockCrystal.ALL_CRYSTALS[stack.getItemDamage()].name;
public String getTranslationKey(ItemStack stack) {
return stack.getItemDamage() >= BlockCrystal.ALL_CRYSTALS.length ? StringUtil.BUGGED_ITEM_NAME : this.getTranslationKey() + "_" + BlockCrystal.ALL_CRYSTALS[stack.getItemDamage()].name;
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack) {
return stack.getItemDamage() >= BlockCrystal.ALL_CRYSTALS.length ? EnumRarity.COMMON : BlockCrystal.ALL_CRYSTALS[stack.getItemDamage()].rarity;
}
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> list){
if(this.isInCreativeTab(tab)){
for(int j = 0; j < BlockCrystal.ALL_CRYSTALS.length; j++){
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> list) {
if (this.isInCreativeTab(tab)) {
for (int j = 0; j < BlockCrystal.ALL_CRYSTALS.length; j++) {
list.add(new ItemStack(this, 1, j));
}
}
}
@Override
protected void registerRendering(){
for(int i = 0; i < BlockCrystal.ALL_CRYSTALS.length; i++){
String name = this.getRegistryName()+"_"+BlockCrystal.ALL_CRYSTALS[i].name;
protected void registerRendering() {
for (int i = 0; i < BlockCrystal.ALL_CRYSTALS.length; i++) {
String name = this.getRegistryName() + "_" + BlockCrystal.ALL_CRYSTALS[i].name;
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this, 1, i), new ResourceLocation(name), "inventory");
}
}

View file

@ -20,6 +20,7 @@ import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -43,7 +44,7 @@ public class ItemCrystalShard extends ItemBase implements IColorProvidingItem{
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack) {
return stack.getItemDamage() >= BlockCrystal.ALL_CRYSTALS.length ? EnumRarity.COMMON : BlockCrystal.ALL_CRYSTALS[stack.getItemDamage()].rarity;
}

View file

@ -24,6 +24,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fml.relauncher.Side;
@ -50,7 +51,7 @@ public class ItemMisc extends ItemBase{
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack){
return stack.getItemDamage() >= ALL_MISC_ITEMS.length ? EnumRarity.COMMON : ALL_MISC_ITEMS[stack.getItemDamage()].rarity;
}

View file

@ -10,82 +10,76 @@
package de.ellpeck.actuallyadditions.mod.items;
import java.util.Collections;
import java.util.Set;
import com.google.common.collect.Sets;
import de.ellpeck.actuallyadditions.mod.items.base.ItemToolAA;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.IRarity;
import java.util.Collections;
import java.util.Set;
public class ItemPickaxeAA extends ItemToolAA{
public class ItemPickaxeAA extends ItemToolAA {
private static final Set<Block> EFFECTIVE_ON = Sets.newHashSet(Blocks.ACTIVATOR_RAIL, Blocks.COAL_ORE, Blocks.COBBLESTONE, Blocks.DETECTOR_RAIL, Blocks.DIAMOND_BLOCK, Blocks.DIAMOND_ORE, Blocks.DOUBLE_STONE_SLAB, Blocks.GOLDEN_RAIL, Blocks.GOLD_BLOCK, Blocks.GOLD_ORE, Blocks.ICE, Blocks.IRON_BLOCK, Blocks.IRON_ORE, Blocks.LAPIS_BLOCK, Blocks.LAPIS_ORE, Blocks.LIT_REDSTONE_ORE, Blocks.MOSSY_COBBLESTONE, Blocks.NETHERRACK, Blocks.PACKED_ICE, Blocks.RAIL, Blocks.REDSTONE_ORE, Blocks.SANDSTONE, Blocks.RED_SANDSTONE, Blocks.STONE, Blocks.STONE_SLAB, Blocks.STONE_BUTTON, Blocks.STONE_PRESSURE_PLATE);
public ItemPickaxeAA(Item.ToolMaterial material, String repairItem, String unlocalizedName, EnumRarity rarity){
public ItemPickaxeAA(Item.ToolMaterial material, String repairItem, String unlocalizedName, IRarity rarity) {
super(1.0F, -2.8F, material, repairItem, unlocalizedName, rarity, EFFECTIVE_ON);
this.setHarvestLevel("pickaxe", material.getHarvestLevel());
}
public ItemPickaxeAA(Item.ToolMaterial material, ItemStack repairItem, String unlocalizedName, EnumRarity rarity){
public ItemPickaxeAA(Item.ToolMaterial material, ItemStack repairItem, String unlocalizedName, IRarity rarity) {
super(1.0F, -2.8F, material, repairItem, unlocalizedName, rarity, EFFECTIVE_ON);
this.setHarvestLevel("pickaxe", material.getHarvestLevel());
}
@Override
public boolean canHarvestBlock(IBlockState blockIn){
public boolean canHarvestBlock(IBlockState blockIn) {
Block block = blockIn.getBlock();
if(block == Blocks.OBSIDIAN){
if (block == Blocks.OBSIDIAN) {
return this.toolMaterial.getHarvestLevel() == 3;
}
else if(block != Blocks.DIAMOND_BLOCK && block != Blocks.DIAMOND_ORE){
if(block != Blocks.EMERALD_ORE && block != Blocks.EMERALD_BLOCK){
if(block != Blocks.GOLD_BLOCK && block != Blocks.GOLD_ORE){
if(block != Blocks.IRON_BLOCK && block != Blocks.IRON_ORE){
if(block != Blocks.LAPIS_BLOCK && block != Blocks.LAPIS_ORE){
if(block != Blocks.REDSTONE_ORE && block != Blocks.LIT_REDSTONE_ORE){
} else if (block != Blocks.DIAMOND_BLOCK && block != Blocks.DIAMOND_ORE) {
if (block != Blocks.EMERALD_ORE && block != Blocks.EMERALD_BLOCK) {
if (block != Blocks.GOLD_BLOCK && block != Blocks.GOLD_ORE) {
if (block != Blocks.IRON_BLOCK && block != Blocks.IRON_ORE) {
if (block != Blocks.LAPIS_BLOCK && block != Blocks.LAPIS_ORE) {
if (block != Blocks.REDSTONE_ORE && block != Blocks.LIT_REDSTONE_ORE) {
Material material = blockIn.getMaterial();
return material == Material.ROCK || material == Material.IRON || material == Material.ANVIL;
}
else{
} else {
return this.toolMaterial.getHarvestLevel() >= 2;
}
}
else{
} else {
return this.toolMaterial.getHarvestLevel() >= 1;
}
}
else{
} else {
return this.toolMaterial.getHarvestLevel() >= 1;
}
}
else{
} else {
return this.toolMaterial.getHarvestLevel() >= 2;
}
}
else{
} else {
return this.toolMaterial.getHarvestLevel() >= 2;
}
}
else{
} else {
return this.toolMaterial.getHarvestLevel() >= 2;
}
}
@Override
public float getDestroySpeed(ItemStack stack, IBlockState state){
public float getDestroySpeed(ItemStack stack, IBlockState state) {
Material material = state.getMaterial();
return material != Material.IRON && material != Material.ANVIL && material != Material.ROCK ? super.getDestroySpeed(stack, state) : this.efficiency;
}
@Override
public Set<String> getToolClasses(ItemStack stack){
public Set<String> getToolClasses(ItemStack stack) {
return Collections.singleton("pickaxe");
}
}

View file

@ -10,14 +10,17 @@
package de.ellpeck.actuallyadditions.mod.items;
import java.util.Collections;
import java.util.Set;
import com.google.common.collect.Sets;
import de.ellpeck.actuallyadditions.mod.items.base.ItemToolAA;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
@ -25,38 +28,35 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.IRarity;
import java.util.Collections;
import java.util.Set;
public class ItemShovelAA extends ItemToolAA{
public class ItemShovelAA extends ItemToolAA {
private static final Set<Block> EFFECTIVE_ON = Sets.newHashSet(Blocks.CLAY, Blocks.DIRT, Blocks.FARMLAND, Blocks.GRASS, Blocks.GRAVEL, Blocks.MYCELIUM, Blocks.SAND, Blocks.SNOW, Blocks.SNOW_LAYER, Blocks.SOUL_SAND, Blocks.GRASS_PATH);
public ItemShovelAA(Item.ToolMaterial material, String repairItem, String unlocalizedName, EnumRarity rarity){
public ItemShovelAA(Item.ToolMaterial material, String repairItem, String unlocalizedName, IRarity rarity) {
super(1.5F, -3.0F, material, repairItem, unlocalizedName, rarity, EFFECTIVE_ON);
this.setHarvestLevel("shovel", material.getHarvestLevel());
}
public ItemShovelAA(Item.ToolMaterial material, ItemStack repairItem, String unlocalizedName, EnumRarity rarity){
public ItemShovelAA(Item.ToolMaterial material, ItemStack repairItem, String unlocalizedName, IRarity rarity) {
super(1.5F, -3.0F, material, repairItem, unlocalizedName, rarity, EFFECTIVE_ON);
this.setHarvestLevel("shovel", material.getHarvestLevel());
}
@Override
public boolean canHarvestBlock(IBlockState blockIn){
public boolean canHarvestBlock(IBlockState blockIn) {
Block block = blockIn.getBlock();
return block == Blocks.SNOW_LAYER || block == Blocks.SNOW;
}
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
return Items.IRON_SHOVEL.onItemUse(playerIn, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
@Override
public Set<String> getToolClasses(ItemStack stack){
public Set<String> getToolClasses(ItemStack stack) {
return Collections.singleton("shovel");
}
}

View file

@ -20,52 +20,53 @@ import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.IRarity;
public class ItemArmorAA extends ItemArmor implements IDisableableItem{
public class ItemArmorAA extends ItemArmor implements IDisableableItem {
private final ItemStack repairItem;
private final String name;
private final EnumRarity rarity;
private final IRarity rarity;
private final boolean disabled;
public ItemArmorAA(String name, ArmorMaterial material, int type, ItemStack repairItem){
public ItemArmorAA(String name, ArmorMaterial material, int type, ItemStack repairItem) {
this(name, material, type, repairItem, EnumRarity.RARE);
}
public ItemArmorAA(String name, ArmorMaterial material, int type, ItemStack repairItem, EnumRarity rarity){
public ItemArmorAA(String name, ArmorMaterial material, int type, ItemStack repairItem, IRarity rarity) {
super(material, 0, ContainerEnergizer.VALID_EQUIPMENT_SLOTS[type]);
this.repairItem = repairItem;
this.name = name;
this.rarity = rarity;
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(name), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(name) +". It will not be registered.");
if(!this.disabled) this.register();
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(name), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(name) + ". It will not be registered.");
if (!this.disabled) this.register();
}
private void register(){
private void register() {
ItemUtil.registerItem(this, this.getBaseName(), this.shouldAddCreative());
this.registerRendering();
}
protected String getBaseName(){
protected String getBaseName() {
return this.name;
}
public boolean shouldAddCreative(){
public boolean shouldAddCreative() {
return true;
}
protected void registerRendering(){
protected void registerRendering() {
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack) {
return this.rarity;
}
@Override
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack){
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack) {
return StackUtil.isValid(this.repairItem) && ItemUtil.areItemsEqual(this.repairItem, stack, false);
}

View file

@ -15,55 +15,54 @@ import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.config.ConfigurationHandler;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.IRarity;
public class ItemHoeAA extends ItemHoe implements IDisableableItem {
private final String name;
private final EnumRarity rarity;
private final IRarity rarity;
private final ItemStack repairItem;
private final boolean disabled;
public ItemHoeAA(Item.ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, EnumRarity rarity){
public ItemHoeAA(Item.ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, IRarity rarity) {
super(toolMat);
this.repairItem = repairItem;
this.name = unlocalizedName;
this.rarity = rarity;
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(this.name), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(this.name) +". It will not be registered.");
if(!this.disabled) this.register();
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(this.name), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(this.name) + ". It will not be registered.");
if (!this.disabled) this.register();
}
private void register(){
private void register() {
ItemUtil.registerItem(this, this.getBaseName(), this.shouldAddCreative());
this.registerRendering();
}
protected String getBaseName(){
protected String getBaseName() {
return this.name;
}
public boolean shouldAddCreative(){
public boolean shouldAddCreative() {
return true;
}
protected void registerRendering(){
protected void registerRendering() {
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack) {
return this.rarity;
}
@Override
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack){
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack) {
return ItemUtil.areItemsEqual(this.repairItem, stack, false);
}

View file

@ -16,58 +16,57 @@ import de.ellpeck.actuallyadditions.mod.blocks.base.ItemBlockBase;
import de.ellpeck.actuallyadditions.mod.config.ConfigurationHandler;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraftforge.common.IRarity;
public class ItemSwordAA extends ItemSword implements IDisableableItem {
private final String name;
private final EnumRarity rarity;
private final IRarity rarity;
private final ItemStack repairItem;
private final boolean disabled;
public ItemSwordAA(ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, EnumRarity rarity){
public ItemSwordAA(ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, IRarity rarity) {
super(toolMat);
this.repairItem = repairItem;
this.name = unlocalizedName;
this.rarity = rarity;
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(this.name), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(this.name) +". It will not be registered.");
if(!this.disabled) this.register();
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(this.name), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(this.name) + ". It will not be registered.");
if (!this.disabled) this.register();
}
private void register(){
private void register() {
ItemUtil.registerItem(this, this.getBaseName(), this.shouldAddCreative());
this.registerRendering();
}
protected String getBaseName(){
protected String getBaseName() {
return this.name;
}
public boolean shouldAddCreative(){
public boolean shouldAddCreative() {
return true;
}
protected void registerRendering(){
protected void registerRendering() {
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
}
protected Class<? extends ItemBlockBase> getItemBlock(){
protected Class<? extends ItemBlockBase> getItemBlock() {
return ItemBlockBase.class;
}
@Override
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack){
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack) {
return ItemUtil.areItemsEqual(this.repairItem, stack, false);
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack) {
return this.rarity;
}

View file

@ -10,6 +10,8 @@
package de.ellpeck.actuallyadditions.mod.items.base;
import java.util.Set;
import de.ellpeck.actuallyadditions.api.misc.IDisableableItem;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.config.ConfigurationHandler;
@ -17,71 +19,65 @@ import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.block.Block;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.oredict.OreDictionary;
import java.util.Set;
public class ItemToolAA extends ItemTool implements IDisableableItem{
public class ItemToolAA extends ItemTool implements IDisableableItem {
private final String name;
private final EnumRarity rarity;
private final IRarity rarity;
private final ItemStack repairItem;
private String repairOredict;
private final boolean disabled;
public ItemToolAA(float attack, float speed, ToolMaterial toolMat, String repairItem, String unlocalizedName, EnumRarity rarity, Set<Block> effectiveStuff){
public ItemToolAA(float attack, float speed, ToolMaterial toolMat, String repairItem, String unlocalizedName, IRarity rarity, Set<Block> effectiveStuff) {
this(attack, speed, toolMat, ItemStack.EMPTY, unlocalizedName, rarity, effectiveStuff);
this.repairOredict = repairItem;
}
public ItemToolAA(float attack, float speed, ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, EnumRarity rarity, Set<Block> effectiveStuff){
public ItemToolAA(float attack, float speed, ToolMaterial toolMat, ItemStack repairItem, String unlocalizedName, IRarity rarity, Set<Block> effectiveStuff) {
super(attack, speed, toolMat, effectiveStuff);
this.repairItem = repairItem;
this.name = unlocalizedName;
this.rarity = rarity;
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(unlocalizedName), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(unlocalizedName) +". It will not be registered.");
if(!this.disabled) this.register();
this.disabled = ConfigurationHandler.config.getBoolean("Disable: " + StringUtil.badTranslate(unlocalizedName), "Tool Control", false, "This will disable the " + StringUtil.badTranslate(unlocalizedName) + ". It will not be registered.");
if (!this.disabled) this.register();
}
private void register(){
private void register() {
ItemUtil.registerItem(this, this.getBaseName(), this.shouldAddCreative());
this.registerRendering();
}
protected String getBaseName(){
protected String getBaseName() {
return this.name;
}
public boolean shouldAddCreative(){
public boolean shouldAddCreative() {
return true;
}
protected void registerRendering(){
protected void registerRendering() {
ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
}
@Override
public EnumRarity getRarity(ItemStack stack){
public IRarity getForgeRarity(ItemStack stack) {
return this.rarity;
}
@Override
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack){
if(StackUtil.isValid(this.repairItem)){
public boolean getIsRepairable(ItemStack itemToRepair, ItemStack stack) {
if (StackUtil.isValid(this.repairItem)) {
return ItemUtil.areItemsEqual(this.repairItem, stack, false);
}
else if(this.repairOredict != null){
} else if (this.repairOredict != null) {
int[] idsStack = OreDictionary.getOreIDs(stack);
for(int id : idsStack){
if(OreDictionary.getOreName(id).equals(this.repairOredict)){
return true;
}
for (int id : idsStack) {
if (OreDictionary.getOreName(id).equals(this.repairOredict)) { return true; }
}
}
return false;

View file

@ -11,24 +11,24 @@
package de.ellpeck.actuallyadditions.mod.items.metalists;
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.item.EnumRarity;
import net.minecraft.util.IStringSerializable;
import net.minecraftforge.common.IRarity;
public enum TheCrystals implements IStringSerializable{
public enum TheCrystals implements IStringSerializable {
REDSTONE("red", Util.CRYSTAL_RED_RARITY, 0xFF2F21, 158F/255F, 43F/255F, 39F/255F),
LAPIS("blue", Util.CRYSTAL_BLUE_RARITY, 0x5171FF, 37F/255F, 49F/255F, 147F/255F),
DIAMOND("light_blue", Util.CRYSTAL_LIGHT_BLUE_RARITY, 0x35F1FF, 99F/255F, 135F/255F, 210F/255F),
REDSTONE("red", Util.CRYSTAL_RED_RARITY, 0xFF2F21, 158F / 255F, 43F / 255F, 39F / 255F),
LAPIS("blue", Util.CRYSTAL_BLUE_RARITY, 0x5171FF, 37F / 255F, 49F / 255F, 147F / 255F),
DIAMOND("light_blue", Util.CRYSTAL_LIGHT_BLUE_RARITY, 0x35F1FF, 99F / 255F, 135F / 255F, 210F / 255F),
COAL("black", Util.CRYSTAL_BLACK_RARITY, 0x434442, 0.2F, 0.2F, 0.2F),
EMERALD("green", Util.CRYSTAL_GREEN_RARITY, 0x44E033, 54F/255F, 75F/255F, 24F/255F),
EMERALD("green", Util.CRYSTAL_GREEN_RARITY, 0x44E033, 54F / 255F, 75F / 255F, 24F / 255F),
IRON("white", Util.CRYSTAL_WHITE_RARITY, 0xCEDDD4, 0.8F, 0.8F, 0.8F);
public final String name;
public final EnumRarity rarity;
public final IRarity rarity;
public final float[] conversionColorParticles;
public final int clusterColor;
TheCrystals(String name, EnumRarity rarity, int clusterColor, float... conversionColorParticles){
TheCrystals(String name, IRarity rarity, int clusterColor, float... conversionColorParticles) {
this.name = name;
this.rarity = rarity;
this.conversionColorParticles = conversionColorParticles;
@ -36,7 +36,7 @@ public enum TheCrystals implements IStringSerializable{
}
@Override
public String getName(){
public String getName() {
return this.name;
}
}

View file

@ -12,8 +12,9 @@ package de.ellpeck.actuallyadditions.mod.items.metalists;
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.item.EnumRarity;
import net.minecraftforge.common.IRarity;
public enum TheMiscItems{
public enum TheMiscItems {
PAPER_CONE("paper_cone", EnumRarity.COMMON),
MASHED_FOOD("mashed_food", EnumRarity.UNCOMMON),
@ -43,9 +44,9 @@ public enum TheMiscItems{
YOUTUBE_ICON("youtube_icon", Util.FALLBACK_RARITY);
public final String name;
public final EnumRarity rarity;
public final IRarity rarity;
TheMiscItems(String name, EnumRarity rarity){
TheMiscItems(String name, IRarity rarity) {
this.name = name;
this.rarity = rarity;
}

View file

@ -0,0 +1,26 @@
package de.ellpeck.actuallyadditions.mod.util;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.IRarity;
public class Rarity implements IRarity {
TextFormatting color;
String name;
public Rarity(TextFormatting color, String name) {
this.color = color;
this.name = name;
}
@Override
public TextFormatting getColor() {
return color;
}
@Override
public String getName() {
return name;
}
}

View file

@ -0,0 +1,231 @@
package de.ellpeck.actuallyadditions.mod.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.StringJoiner;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
public class RefHelp {
public static class UnableToFindMethodException extends RuntimeException {
private static final long serialVersionUID = 1L;
public UnableToFindMethodException(String[] methodNames, Exception failed) {
super(failed);
}
public UnableToFindMethodException(Throwable failed) {
super(failed);
}
}
public static class UnableToFindClassException extends RuntimeException {
private static final long serialVersionUID = 1L;
public UnableToFindClassException(String[] classNames, @Nullable Exception err) {
super(err);
}
}
public static class UnableToAccessFieldException extends RuntimeException {
private static final long serialVersionUID = 1L;
public UnableToAccessFieldException(String[] fieldNames, Exception e) {
super(e);
}
public UnableToAccessFieldException(Exception e) {
super(e);
}
}
public static class UnableToFindFieldException extends RuntimeException {
private static final long serialVersionUID = 1L;
public UnableToFindFieldException(String[] fieldNameList, Exception e) {
super(e);
}
public UnableToFindFieldException(Exception e) {
super(e);
}
}
public static class UnknownConstructorException extends RuntimeException {
public UnknownConstructorException(final String message) {
super(message);
}
}
public static Field findField(Class<?> clazz, String... fieldNames) {
Exception failed = null;
for (String fieldName : fieldNames) {
try {
Field f = clazz.getDeclaredField(fieldName);
f.setAccessible(true);
return f;
} catch (Exception e) {
failed = e;
}
}
throw new UnableToFindFieldException(fieldNames, failed);
}
@Nonnull
public static Field findField(@Nonnull Class<?> clazz, @Nonnull String fieldName, @Nullable String fieldObfName) {
Preconditions.checkNotNull(clazz);
Preconditions.checkArgument(StringUtils.isNotEmpty(fieldName), "Field name cannot be empty");
String nameToFind = FMLLaunchHandler.isDeobfuscatedEnvironment() ? fieldName : MoreObjects.firstNonNull(fieldObfName, fieldName);
try {
Field f = clazz.getDeclaredField(nameToFind);
f.setAccessible(true);
return f;
} catch (Exception e) {
throw new UnableToFindFieldException(e);
}
}
@SuppressWarnings("unchecked")
public static <T, E> T getPrivateValue(Class<? super E> classToAccess, @Nullable E instance, int fieldIndex) {
try {
Field f = classToAccess.getDeclaredFields()[fieldIndex];
f.setAccessible(true);
return (T) f.get(instance);
} catch (Exception e) {
throw new UnableToAccessFieldException(e);
}
}
@SuppressWarnings("unchecked")
public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instance, String... fieldNames) {
try {
return (T) findField(classToAccess, fieldNames).get(instance);
} catch (Exception e) {
throw new UnableToAccessFieldException(fieldNames, e);
}
}
@SuppressWarnings("unchecked")
public static <T, E> T getPrivateValue(Class<? super E> classToAccess, @Nullable E instance, String fieldName, @Nullable String fieldObfName) {
try {
return (T) findField(classToAccess, fieldName, fieldObfName).get(instance);
} catch (Exception e) {
throw new UnableToAccessFieldException(e);
}
}
public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, int fieldIndex) {
try {
Field f = classToAccess.getDeclaredFields()[fieldIndex];
f.setAccessible(true);
f.set(instance, value);
} catch (Exception e) {
throw new UnableToAccessFieldException(e);
}
}
public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, String... fieldNames) {
try {
findField(classToAccess, fieldNames).set(instance, value);
} catch (Exception e) {
throw new UnableToAccessFieldException(fieldNames, e);
}
}
public static <T, E> void setPrivateValue(Class<? super T> classToAccess, @Nullable T instance, @Nullable E value, String fieldName, @Nullable String fieldObfName) {
try {
findField(classToAccess, fieldName, fieldObfName).set(instance, value);
} catch (Exception e) {
throw new UnableToAccessFieldException(e);
}
}
@SuppressWarnings("unchecked")
public static Class<? super Object> getClass(ClassLoader loader, String... classNames) {
Exception err = null;
for (String className : classNames) {
try {
return (Class<? super Object>) Class.forName(className, false, loader);
} catch (Exception e) {
err = e;
}
}
throw new UnableToFindClassException(classNames, err);
}
/**
* Finds a method with the specified name and parameters in the given class and makes it accessible.
* Note: for performance, store the returned value and avoid calling this repeatedly.
* <p>
* Throws an exception if the method is not found.
*
* @param clazz The class to find the method on.
* @param methodName The name of the method to find (used in developer environments, i.e. "getWorldTime").
* @param methodObfName The obfuscated name of the method to find (used in obfuscated environments, i.e. "getWorldTime").
* If the name you are looking for is on a class that is never obfuscated, this should be null.
* @param parameterTypes The parameter types of the method to find.
* @return The method with the specified name and parameters in the given class.
*/
@Nonnull
public static Method findMethod(@Nonnull Class<?> clazz, @Nonnull String methodName, @Nullable String methodObfName, Class<?>... parameterTypes) {
Preconditions.checkNotNull(clazz);
Preconditions.checkArgument(StringUtils.isNotEmpty(methodName), "Method name cannot be empty");
String nameToFind = FMLLaunchHandler.isDeobfuscatedEnvironment() ? methodName : MoreObjects.firstNonNull(methodObfName, methodName);
try {
Method m = clazz.getDeclaredMethod(nameToFind, parameterTypes);
m.setAccessible(true);
return m;
} catch (Exception e) {
throw new UnableToFindMethodException(e);
}
}
/**
* Finds a constructor in the specified class that has matching parameter types.
*
* @param klass The class to find the constructor in
* @param parameterTypes The parameter types of the constructor.
* @param <T> The type
* @return The constructor
* @throws NullPointerException if {@code klass} is null
* @throws NullPointerException if {@code parameterTypes} is null
* @throws UnknownConstructorException if the constructor could not be found
*/
@Nonnull
public static <T> Constructor<T> findConstructor(@Nonnull final Class<T> klass, @Nonnull final Class<?>... parameterTypes) {
Preconditions.checkNotNull(klass, "class");
Preconditions.checkNotNull(parameterTypes, "parameter types");
try {
Constructor<T> constructor = klass.getDeclaredConstructor(parameterTypes);
constructor.setAccessible(true);
return constructor;
} catch (final NoSuchMethodException e) {
final StringBuilder desc = new StringBuilder();
desc.append(klass.getSimpleName());
StringJoiner joiner = new StringJoiner(", ", "(", ")");
for (Class<?> type : parameterTypes) {
joiner.add(type.getSimpleName());
}
desc.append(joiner);
throw new UnknownConstructorException("Could not find constructor '" + desc.toString() + "' in " + klass);
}
}
}

View file

@ -10,51 +10,48 @@
package de.ellpeck.actuallyadditions.mod.util;
import java.util.Locale;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import net.minecraft.item.EnumRarity;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.common.IRarity;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.oredict.OreDictionary;
public final class Util{
public final class Util {
public static final int WILDCARD = OreDictionary.WILDCARD_VALUE;
public static final int BUCKET = Fluid.BUCKET_VOLUME;
public static final EnumRarity CRYSTAL_RED_RARITY = addRarity("crystalRed", TextFormatting.DARK_RED, ActuallyAdditions.NAME+" Red Crystal");
public static final EnumRarity CRYSTAL_BLUE_RARITY = addRarity("crystalBlue", TextFormatting.DARK_BLUE, ActuallyAdditions.NAME+" Blue Crystal");
public static final EnumRarity CRYSTAL_LIGHT_BLUE_RARITY = addRarity("crystalLightBlue", TextFormatting.BLUE, ActuallyAdditions.NAME+" Light Blue Crystal");
public static final EnumRarity CRYSTAL_BLACK_RARITY = addRarity("crystalBlack", TextFormatting.DARK_GRAY, ActuallyAdditions.NAME+" Black Crystal");
public static final EnumRarity CRYSTAL_GREEN_RARITY = addRarity("crystalGreen", TextFormatting.DARK_GREEN, ActuallyAdditions.NAME+" Green Crystal");
public static final EnumRarity CRYSTAL_WHITE_RARITY = addRarity("crystalWhite", TextFormatting.GRAY, ActuallyAdditions.NAME+" White Crystal");
public static final IRarity CRYSTAL_RED_RARITY = addRarity("crystalRed", TextFormatting.DARK_RED, ActuallyAdditions.NAME + " Red Crystal");
public static final IRarity CRYSTAL_BLUE_RARITY = addRarity("crystalBlue", TextFormatting.DARK_BLUE, ActuallyAdditions.NAME + " Blue Crystal");
public static final IRarity CRYSTAL_LIGHT_BLUE_RARITY = addRarity("crystalLightBlue", TextFormatting.BLUE, ActuallyAdditions.NAME + " Light Blue Crystal");
public static final IRarity CRYSTAL_BLACK_RARITY = addRarity("crystalBlack", TextFormatting.DARK_GRAY, ActuallyAdditions.NAME + " Black Crystal");
public static final IRarity CRYSTAL_GREEN_RARITY = addRarity("crystalGreen", TextFormatting.DARK_GREEN, ActuallyAdditions.NAME + " Green Crystal");
public static final IRarity CRYSTAL_WHITE_RARITY = addRarity("crystalWhite", TextFormatting.GRAY, ActuallyAdditions.NAME + " White Crystal");
public static final EnumRarity FALLBACK_RARITY = addRarity("fallback", TextFormatting.STRIKETHROUGH, ActuallyAdditions.NAME+" Fallback");
public static final IRarity FALLBACK_RARITY = addRarity("fallback", TextFormatting.STRIKETHROUGH, ActuallyAdditions.NAME + " Fallback");
private static EnumRarity addRarity(String name, TextFormatting color, String displayName){
return EnumHelper.addRarity((ActuallyAdditions.MODID+"_"+name).toUpperCase(Locale.ROOT), color, displayName);
private static IRarity addRarity(String name, TextFormatting color, String displayName) {
return new Rarity(color, displayName);
}
public static boolean isDevVersion(){
public static boolean isDevVersion() {
return ActuallyAdditions.VERSION.equals("@VERSION@");
}
public static boolean isClient(){
public static boolean isClient() {
return FMLCommonHandler.instance().getEffectiveSide().isClient();
}
private static String[] splitVersion(){
private static String[] splitVersion() {
return ActuallyAdditions.VERSION.split("-");
}
public static String getMcVersion(){
public static String getMcVersion() {
return splitVersion()[0];
}
public static String getMajorModVersion(){
public static String getMajorModVersion() {
return splitVersion()[1].substring(1);
}
}