Jump to content

Recommended Posts

Posted

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();

}

 

 

Posted

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.

Posted

"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)"

Posted

Oh, sorry, my bad. For some reason I didn't see that.

Posted

As for Herbalism - I belive the "age" of crop (includes wheat) is its metadata.

 

EDIT

"I belive..." == "It is"

1.7.10 is no longer supported by forge, you are on your own.

Posted

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.

 

Posted

@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();

}

 

 

Posted

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.

Posted

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 ?

Posted

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);

}

 

}

 

 

Posted

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.

Posted

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).

1.7.10 is no longer supported by forge, you are on your own.

Posted

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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
    • Okay, but does the modpack works with 1.12 or just with 1.12.2, because I need the Forge client specifically for Minecraft 1.12, not 1.12.2
    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.