Foxy Posted July 6, 2015 Share Posted July 6, 2015 Hi i'm new to modding and i require a bit of help, everything is explained in Professions topic in mods section. Link to GitHub [sOLVED] Herbalism: }else if (event.state.getBlock() == Blocks.wheat || event.state.getBlock() == Blocks.potatoes || event.state.getBlock() == Blocks.carrots){ int age = (Integer)event.state.getValue(BlockCrops.AGE); if (age != 7){ Professions.herbalism.setExp(Professions.herbalism.getExp() - (2 * Professions.herbalism.getLevel())); Professions.herbalism.update(); }else{ Professions.herbalism.setExp(Professions.herbalism.getExp() + (3 * Professions.herbalism.getLevel())); Professions.herbalism.update(); } Quote Link to comment Share on other sites More sharing options...
Awesome_Spider Posted July 6, 2015 Share Posted July 6, 2015 What exactly do you need help with? The people on this forum are not going to code your mod for you if that's what you're asking. Quote Link to comment Share on other sites More sharing options...
Failender Posted July 6, 2015 Share Posted July 6, 2015 "I'm currently having problems with Fishing (don't know how to check if player is fishing and if player fished anything), Herbalism (don't know how to check if wheat is fully grown)" Quote Link to comment Share on other sites More sharing options...
Awesome_Spider Posted July 6, 2015 Share Posted July 6, 2015 Oh, sorry, my bad. For some reason I didn't see that. Quote Link to comment Share on other sites More sharing options...
Ernio Posted July 6, 2015 Share Posted July 6, 2015 As for Herbalism - I belive the "age" of crop (includes wheat) is its metadata. EDIT "I belive..." == "It is" Quote 1.7.10 is no longer supported by forge, you are on your own. Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 I tried checking if metadata is 7 but it still adds exp event if it isn't if (event.state.getBlock() == Blocks.wheat){ if (Blocks.wheat.getMetaFromState() == 7){ Professions.herbalism.setExp(Professions.herbalism.getExp() + (3 * Professions.herbalism.getLevel())); Professions.herbalism.update(); }else{ Professions.herbalism.setExp(Professions.herbalism.getExp() - (2 * Professions.herbalism.getLevel())); Professions.herbalism.update(); } This isn't only way i checked but still doesn't work. Quote Link to comment Share on other sites More sharing options...
Failender Posted July 7, 2015 Share Posted July 7, 2015 Look closer at your 2nd if condition. Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 @Failender I fixed that alredy but diesieben07 said i shouldn't use getMetaFromState so i am experimenting again. Never ever call getMetaFromState yourself. Interact with the IBlockState, it's there so that you don't have to work with stupid magic numbers (7) anymore. The age is stored in the property BlockCrops.AGE. @diesieben70 Can you explain why i shouldn't use getMetaFromState ? I also modified if statement, but not sure if it's correct way to do it. Don't really understand how Block States work. }else if (event.state.getBlock() == Blocks.wheat || event.state.getBlock() == Blocks.potatoes || event.state.getBlock() == Blocks.carrots){ if (event.state.getBlock() == event.state.withProperty(BlockCrops.AGE, 7)){ Professions.herbalism.setExp(Professions.herbalism.getExp() - (2 * Professions.herbalism.getLevel())); Professions.herbalism.update(); }else{ Professions.herbalism.setExp(Professions.herbalism.getExp() + (3 * Professions.herbalism.getLevel())); Professions.herbalism.update(); } Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 I am beginner programmer so i don't know much but i'm learning. Here is another try: }else if (event.state.getBlock() == Blocks.wheat || event.state.getBlock() == Blocks.potatoes || event.state.getBlock() == Blocks.carrots){ int age = (Integer)event.state.getValue(BlockCrops.AGE); if (age != 7){ Professions.herbalism.setExp(Professions.herbalism.getExp() - (2 * Professions.herbalism.getLevel())); Professions.herbalism.update(); }else{ Professions.herbalism.setExp(Professions.herbalism.getExp() + (3 * Professions.herbalism.getLevel())); Professions.herbalism.update(); } This way it works as intended but idk if it's considered right or wrong programming. Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 That looks fine as far as the block state handling goes. Is there anything else that needs to be handled or done ? Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 Well, the "Professions.herbalism" makes me suspicious whether you actually handle the fact that there is more than one player. I don't ... which classes should i check to see how it's done ? Btw i added github link also do i need to initiate save of players current exp and level on server or world exit or is it automatic, i guess it's not ? Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 Do i need to implement IExtendedPropperties to every profession class or just core class ? Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 Okay i finished implementing IExtendedEntityProperties, is this alright: Profession.java package com.foxy.profs.common.profession; import com.foxy.profs.common.library.ProfLib; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class Profession implements IExtendedEntityProperties { protected String name; protected static String IDENTIFICATION = "Professions"; protected int exp, maxExp, level, maxLevel; public Profession(String name, int maxExp, int maxLevel){ this.name = name; this.maxExp = maxExp; this.level = 1; this.maxLevel = maxLevel; } public static final Profession register(EntityPlayer player){ return (Profession)player.getExtendedProperties(IDENTIFICATION); } public static final Profession get(EntityPlayer player){ return (Profession)player.getExtendedProperties(IDENTIFICATION); } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound profession = new NBTTagCompound(); profession.setInteger("Exp", this.exp); profession.setInteger("Max Exp", this.maxExp); profession.setInteger("Level", level); profession.setInteger("Max Level", this.maxLevel); compound.setTag(name, profession); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound profession = (NBTTagCompound) compound.getTag(name); profession.getInteger("Exp"); profession.getInteger("Max Exp"); profession.getInteger("Level"); profession.getInteger("Max Level"); System.out.println("[" + ProfLib.MODID + "]Has loaded " + name + " from NBT: [Exp" + this.exp + "/" + this.maxExp + "][Level" + this.level + "/" + this.maxLevel + "]"); } @Override public void init(Entity entity, World world) { } public String getName(){ return name; } public int getExp(){ return exp; } public int getMaxExp(){ return maxExp; } public int getLevel(){ return level; } public int getMaxLevel(){ return maxLevel; } public Profession setName(String name){ this.name = name; return this; } public Profession setExp(int exp){ this.exp = exp; return this; } public Profession setMaxExp(int maxExp){ this.maxExp = maxExp; return this; } public Profession setLevel(int level){ this.level = level; return this; } public Profession setMaxLevel(int maxLevel){ this.maxLevel = maxLevel; return this; } public Profession addExp(int exp){ this.exp += exp; return this; } public Profession removeExp(int exp){ this.exp -= exp; return this; } public Profession addExp(int exp, int modifier){ this.exp += (exp * modifier); return this; } public Profession removeExp(int exp, int modifier){ this.exp -= (exp * modifier); return this; } public boolean isLeveledUp(){ if (level == maxLevel){ return true; }else{ return false; } } public boolean isMaxed(){ if (exp == maxExp && isLeveledUp()){ return true; }else{ return false; } } public boolean canLevelUp(){ if (exp >= maxExp && !isLeveledUp()){ return true; }else{ return false; } } public boolean canLevelDown(){ if (exp < 0){ if (level != 1){ return true; }else{ return false; } } return false; } public boolean canEarnExp(){ if (exp > 0 || exp <= maxExp){ return true; }else{ return false; } } public boolean canLoseExp(){ if (exp > 0){ return true; }else{ return false; } } public void levelUp(){ level++; exp = (exp - maxExp); maxExp = (maxExp + (15 * level)); } public void levelDown(){ level--; exp = (maxExp - (exp * -1)); maxExp = (maxExp + (15 * level)); } public void update(){ while (exp >= maxExp || exp < 0){ if (canLevelUp()){ levelUp(); }else if (canLevelDown()){ levelDown(); } } } } ProfEvents.java package com.foxy.profs.common.event; import com.foxy.profs.common.init.Professions; import com.foxy.profs.common.profession.Profession; import net.minecraft.block.BlockCrops; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import net.minecraftforge.event.entity.player.PlayerUseItemEvent; import net.minecraftforge.event.world.BlockEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class ProfEvents { @SubscribeEvent public void onBlockBroken(BlockEvent.BreakEvent event){ if (!event.getPlayer().capabilities.isCreativeMode){ if (event.state.getBlock() == Blocks.coal_ore || event.state.getBlock() == Blocks.redstone_ore || event.state.getBlock() == Blocks.lit_redstone_ore){ Professions.mining.setExp(Professions.mining.getExp() + (2 + (2 * Professions.mining.getLevel()))); Professions.mining.update(); }else if (event.state.getBlock() == Blocks.iron_ore || event.state.getBlock() == Blocks.lapis_ore || event.state.getBlock() == Blocks.quartz_ore){ Professions.mining.setExp(Professions.mining.getExp() + (4 + (2 * Professions.mining.getLevel()))); Professions.mining.update(); }else if (event.state.getBlock() == Blocks.gold_ore || event.state.getBlock() == Blocks.diamond_ore || event.state.getBlock() == Blocks.emerald_ore){ Professions.mining.setExp(Professions.mining.getExp() + (8 + (2 * Professions.mining.getLevel()))); Professions.mining.update(); }else if (event.state.getBlock() == Blocks.leaves || event.state.getBlock() == Blocks.leaves2){ Professions.woodcutting.setExp(Professions.woodcutting.getExp() + (1 + Professions.woodcutting.getLevel())); Professions.woodcutting.update(); }else if (event.state.getBlock() == Blocks.log || event.state.getBlock() == Blocks.log2){ Professions.woodcutting.setExp(Professions.woodcutting.getExp() + (3 + Professions.woodcutting.getLevel())); Professions.woodcutting.update(); }else if (event.state.getBlock() == Blocks.wheat || event.state.getBlock() == Blocks.potatoes || event.state.getBlock() == Blocks.carrots){ int age = (Integer)event.state.getValue(BlockCrops.AGE); if (age != 7){ Professions.herbalism.setExp(Professions.herbalism.getExp() - (2 * Professions.herbalism.getLevel())); Professions.herbalism.update(); }else{ Professions.herbalism.setExp(Professions.herbalism.getExp() + (3 * Professions.herbalism.getLevel())); Professions.herbalism.update(); } }else if (event.state.getBlock() == Blocks.reeds){ Professions.herbalism.setExp(Professions.herbalism.getExp() + (1 * Professions.herbalism.getLevel())); } } } @SubscribeEvent public void onFishedUp(PlayerUseItemEvent event){ if (!event.entityPlayer.capabilities.isCreativeMode){ if (event.entityPlayer.getHeldItem() != null){ if (event.entityPlayer.getHeldItem().equals(Items.fishing_rod)){ } } } } @SubscribeEvent public void onEntityConstructing(EntityConstructing event){ if (event.entity instanceof EntityPlayer && Profession.get((EntityPlayer) event.entity) == null){ Profession.register((EntityPlayer) event.entity); } } } Professions is init class for all professions... Professions package com.foxy.profs.common.init; import com.foxy.profs.common.profession.Fishing; import com.foxy.profs.common.profession.Herbalism; import com.foxy.profs.common.profession.Hunting; import com.foxy.profs.common.profession.Mining; import com.foxy.profs.common.profession.Profession; import com.foxy.profs.common.profession.Woodcutting; public class Professions { public static Profession hunting; public static Profession woodcutting; public static Profession mining; public static Profession herbalism; public static Profession fishing; public static void init(){ hunting = new Hunting("Hunting", 100, 725); woodcutting = new Woodcutting("Woodcutting", 100, 725); mining = new Mining("Mining", 100, 725); herbalism = new Herbalism("Herbalism", 100, 725); fishing = new Fishing("Fishing", 100, 725); } } Quote Link to comment Share on other sites More sharing options...
Failender Posted July 7, 2015 Share Posted July 7, 2015 correct me if Im wrong but i never see u using that properties. Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 what property ? Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 How should i specify which profession is used/wanted then? Quote Link to comment Share on other sites More sharing options...
Foxy Posted July 7, 2015 Author Share Posted July 7, 2015 Is this what you mean ? package com.foxy.profs.common.profession; import com.foxy.profs.common.library.ProfLib; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class Profession implements IExtendedEntityProperties { protected String name; protected static String IDENTIFICATION = "Professions"; protected int exp, maxExp, level, maxLevel; public Profession hunting = new Profession("Hunting", 100, 725); public Profession woodcutting = new Profession("Woodcutting", 100, 725); public Profession mining = new Profession("Mining", 100, 725); public Profession herbalism = new Profession("Herbalism", 100, 725); public Profession fishing = new Profession("Fishing", 100, 725); public Profession(String name, int maxExp, int maxLevel){ this.name = name; this.exp = 0; this.level = 1; this.maxExp = maxExp; this.maxLevel = maxLevel; } public static final Profession register(EntityPlayer player){ return (Profession)player.getExtendedProperties(IDENTIFICATION); } public static final Profession get(EntityPlayer player){ return (Profession)player.getExtendedProperties(IDENTIFICATION); } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound profession = new NBTTagCompound(); profession.setInteger("Exp", this.exp); profession.setInteger("Max Exp", this.maxExp); profession.setInteger("Level", level); profession.setInteger("Max Level", this.maxLevel); compound.setTag(name, profession); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound profession = (NBTTagCompound) compound.getTag(name); this.exp = profession.getInteger("Exp"); this.level = profession.getInteger("Level"); this.maxExp = profession.getInteger("Max Exp"); this.maxLevel = profession.getInteger("Max Level"); System.out.println("[" + ProfLib.MODID + "]Has loaded " + name + " from NBT: [Exp" + this.exp + "/" + this.maxExp + "][Level" + this.level + "/" + this.maxLevel + "]"); } @Override public void init(Entity entity, World world) { } public String getName(){ return name; } public int getExp(){ return exp; } public int getMaxExp(){ return maxExp; } public int getLevel(){ return level; } public int getMaxLevel(){ return maxLevel; } public Profession setName(String name){ this.name = name; return this; } public Profession setExp(int exp){ this.exp = exp; return this; } public Profession setMaxExp(int maxExp){ this.maxExp = maxExp; return this; } public Profession setLevel(int level){ this.level = level; return this; } public Profession setMaxLevel(int maxLevel){ this.maxLevel = maxLevel; return this; } public Profession addExp(int exp){ this.exp += exp; return this; } public Profession removeExp(int exp){ this.exp -= exp; return this; } public Profession addExp(int exp, int modifier){ this.exp += (exp + (modifier * level)); return this; } public Profession removeExp(int exp, int modifier){ this.exp -= (exp * modifier); return this; } public boolean isLeveledUp(){ if (level == maxLevel){ return true; }else{ return false; } } public boolean isMaxed(){ if (exp == maxExp && isLeveledUp()){ return true; }else{ return false; } } public boolean canLevelUp(){ if (exp >= maxExp && !isLeveledUp()){ return true; }else{ return false; } } public boolean canLevelDown(){ if (exp < 0){ if (level != 1){ return true; }else{ return false; } } return false; } public boolean canEarnExp(){ if (exp > 0 || exp <= maxExp){ return true; }else{ return false; } } public boolean canLoseExp(){ if (exp > 0){ return true; }else{ return false; } } public void levelUp(){ level++; exp = (exp - maxExp); maxExp = (maxExp + (15 * level)); } public void levelDown(){ level--; exp = (maxExp - (exp * -1)); maxExp = (maxExp + (15 * level)); } public void update(){ while (exp >= maxExp || exp < 0){ if (canLevelUp()){ levelUp(); }else if (canLevelDown()){ levelDown(); } } } } Also should i use datawatchers for exp and level or no ? EDIT: no need for datawatchers cause players wont need to know about other player exp or level. EDIT: ProfEvent class package com.foxy.profs.common.event; import com.foxy.profs.common.profession.Profession; import net.minecraft.block.BlockCrops; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import net.minecraftforge.event.entity.player.PlayerUseItemEvent; import net.minecraftforge.event.world.BlockEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class ProfEvents { @SubscribeEvent public void onEntityConstructing(EntityConstructing event){ if (event.entity instanceof EntityPlayer && Profession.get((EntityPlayer) event.entity) == null){ Profession.register((EntityPlayer) event.entity); } } @SubscribeEvent public void onBlockBroken(BlockEvent.BreakEvent event){ Profession profession = Profession.get((EntityPlayer) event.getPlayer()); if (!event.getPlayer().capabilities.isCreativeMode){ if (event.state.getBlock() == Blocks.coal_ore || event.state.getBlock() == Blocks.redstone_ore || event.state.getBlock() == Blocks.lit_redstone_ore){ profession.mining.addExp(2, 2); profession.mining.update(); }else if (event.state.getBlock() == Blocks.iron_ore || event.state.getBlock() == Blocks.lapis_ore || event.state.getBlock() == Blocks.quartz_ore){ profession.mining.addExp(4, 2); profession.mining.update(); }else if (event.state.getBlock() == Blocks.gold_ore || event.state.getBlock() == Blocks.diamond_ore || event.state.getBlock() == Blocks.emerald_ore){ profession.mining.addExp(8, 2); profession.mining.update(); }else if (event.state.getBlock() == Blocks.leaves || event.state.getBlock() == Blocks.leaves2){ profession.woodcutting.addExp(1, 1); profession.woodcutting.update(); }else if (event.state.getBlock() == Blocks.log || event.state.getBlock() == Blocks.log2){ profession.woodcutting.addExp(3, 1); profession.woodcutting.update(); }else if (event.state.getBlock() == Blocks.wheat || event.state.getBlock() == Blocks.potatoes || event.state.getBlock() == Blocks.carrots){ int age = (Integer)event.state.getValue(BlockCrops.AGE); if (age != 7){ profession.herbalism.removeExp(2, 1); profession.herbalism.update(); }else{ profession.herbalism.removeExp(3, 1); profession.herbalism.update(); } }else if (event.state.getBlock() == Blocks.reeds){ profession.herbalism.addExp(1, 1); } } } @SubscribeEvent public void onBlockPlaced(BlockEvent.PlaceEvent event){ Profession profession = Profession.get((EntityPlayer) event.player); if (!event.player.capabilities.isCreativeMode){ if (event.placedBlock.getBlock() == Blocks.reeds){ profession.mining.removeExp(3, 1); } } } } do i need to cast event.player and event.getPlayer() to (EntityPlayer), seeing as they are already players i think they shouldn't. Quote Link to comment Share on other sites More sharing options...
Ernio Posted July 7, 2015 Share Posted July 7, 2015 No, that's not right. Lemme explain. There is some EntityPlayer. Each living entity has a map looking +/- like this Map<KeyToProps, IEEP>. You can assign class you create that inplements IEEP to given entity (by putting it into that map) - that happens in construction event - you literally assign "additional" "storage" (called IEEP) to given player. Now - in that storage you can hold ANY kind of data. it doesn't need to be even saved to disk. It can be really anything - object, variable, object iwth other objects. Then if you decide - you can save it to given entity's NBT. That's whole logic behind it. Now - profession is something that is assigned to PLAYER, not all players. That's what you need to do - you need to make fields in your IEEP class that will hold profession data about given player. Design you can do: (nice and readable) EntityPlayer --> ProfessionsMap implements IEEP -----> Herbalism extends Profession -----> Mining extends Profession -----> Combat extends Profession. Where "Profession" would be an object that holds data about specific profession for given player. Then you can load/save that data using load/saveNBTData(...). P.S I wonder how your code (one you posted in last post) is not givng OutOfMemeory. Unless it does (you are literally making infinite number of "Profession" there). Quote 1.7.10 is no longer supported by forge, you are on your own. Link to comment Share on other sites More sharing options...
Foxy Posted July 8, 2015 Author Share Posted July 8, 2015 Thanks for help, but before i can finish this project i should read more tutorials on java so i have base knowledge of java. I understand what you are saying but don't know how to fully implement it, so i will start with simpler things. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.