Skip to content

Commit

Permalink
Attribute (#24)
Browse files Browse the repository at this point in the history
* Add experience attribute

* init hungerManager and experienceManager to function initEntity()
  • Loading branch information
JblusItsMe authored Aug 23, 2024
1 parent 9bc5073 commit 4f9b9f4
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 9 deletions.
28 changes: 21 additions & 7 deletions src/main/java/org/sculk/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public Player(BedrockServerSession session, ClientChainData data) {
this.forms = new Int2ObjectOpenHashMap<>();
}

@Override
public void initEntity() {
super.initEntity();
}

public void kick(String message) {
DisconnectPacket packet = new DisconnectPacket();
packet.setKickMessage(message);
Expand Down Expand Up @@ -124,13 +129,22 @@ public SerializedSkin getSerializedSkin() {
}

public void sendAttributes() {
UpdateAttributesPacket pk = new UpdateAttributesPacket();
pk.setRuntimeEntityId(this.getRuntimeId());
List<AttributeData> attributes = pk.getAttributes();
//attributes.add(AttributeFactory.getINSTANCE().mustGet(Attribute.HUNGER));
pk.setAttributes(attributes);
sendDataPacket(pk);
System.out.println();
UpdateAttributesPacket updateAttributesPacket = new UpdateAttributesPacket();
updateAttributesPacket.setRuntimeEntityId(this.getRuntimeId());
List<AttributeData> attributes = updateAttributesPacket.getAttributes();

Attribute hunger = AttributeFactory.getINSTANCE().mustGet(Attribute.HUNGER);
attributes.add(new AttributeData(hunger.getId(), hunger.getMinValue(), hunger.getMaxValue(), hunger.getCurrentValue(), hunger.getDefaultValue()));

Attribute experienceLevel = AttributeFactory.getINSTANCE().mustGet(Attribute.EXPERIENCE_LEVEL);
attributes.add(new AttributeData(experienceLevel.getId(), experienceLevel.getMinValue(), experienceLevel.getMaxValue(), experienceLevel.getCurrentValue(), experienceLevel.getDefaultValue()));

Attribute experience = AttributeFactory.getINSTANCE().mustGet(Attribute.EXPERIENCE);
attributes.add(new AttributeData(experience.getId(), experience.getMinValue(), experience.getMaxValue(), experience.getCurrentValue(), experience.getDefaultValue()));

updateAttributesPacket.setAttributes(attributes);
sendDataPacket(updateAttributesPacket);
System.out.println(updateAttributesPacket);
}

public long getPing() {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/sculk/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
* @author: SculkTeams
* @link: http://www.sculkmp.org/
*/
public class Entity {
public abstract class Entity {

public void initEntity() {}

}
22 changes: 22 additions & 0 deletions src/main/java/org/sculk/entity/HumanEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.sculk.entity;


import org.sculk.entity.manager.ExperienceManager;
import org.sculk.entity.manager.HungerManager;

/*
* ____ _ _
* / ___| ___ _ _| | | __
Expand All @@ -17,4 +20,23 @@
* @link: http://www.sculkmp.org/
*/
public class HumanEntity extends Living {

protected HungerManager hungerManager;
protected ExperienceManager experienceManager;

@Override
public void initEntity() {
super.initEntity();
this.hungerManager = new HungerManager(this);
this.experienceManager = new ExperienceManager(this);
}

public HungerManager getHungerManager() {
return hungerManager;
}

public ExperienceManager getExperienceManager() {
return experienceManager;
}

}
10 changes: 9 additions & 1 deletion src/main/java/org/sculk/entity/Living.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.sculk.entity;


import org.sculk.entity.manager.HungerManager;

/*
* ____ _ _
* / ___| ___ _ _| | | __
Expand All @@ -16,5 +18,11 @@
* @author: SculkTeams
* @link: http://www.sculkmp.org/
*/
public class Living extends Entity {
public abstract class Living extends Entity {

@Override
public void initEntity() {
super.initEntity();
}

}
175 changes: 175 additions & 0 deletions src/main/java/org/sculk/entity/manager/ExperienceManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.sculk.entity.manager;


import org.sculk.entity.Attribute;
import org.sculk.entity.AttributeFactory;
import org.sculk.entity.Entity;
import org.sculk.entity.HumanEntity;
import org.sculk.event.player.PlayerExperienceChangeEvent;
import org.sculk.utils.ExperienceUtils;

import java.util.Map;

/*
* ____ _ _
* / ___| ___ _ _| | | __
* \___ \ / __| | | | | |/ /
* ___) | (__| |_| | | <
* |____/ \___|\__,_|_|_|\_\
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author: SculkTeams
* @link: http://www.sculkmp.org/
*/
public class ExperienceManager {

private Attribute levelAttribute;
private Attribute progressAttribute;

private int totalXp = 0;
private boolean canAttractXpOrbs = true;
private int xpCooldown = 0;

private HumanEntity humanEntity;

public ExperienceManager(HumanEntity humanEntity) {
this.humanEntity = humanEntity;
this.levelAttribute = fetchAttribute(humanEntity, Attribute.EXPERIENCE_LEVEL);
this.progressAttribute = fetchAttribute(humanEntity, Attribute.EXPERIENCE);
}

private static Attribute fetchAttribute(Entity entity, String attributeId) {
Attribute attribute = AttributeFactory.getINSTANCE().mustGet(attributeId);
// TODO next step add attribute to entity
return attribute;
}

public boolean setXpAndProgress(Integer level, Float progress) {
PlayerExperienceChangeEvent playerExperienceChangeEvent = new PlayerExperienceChangeEvent(this.humanEntity, getXpLevel(), getXpProgress(), level, progress);
playerExperienceChangeEvent.call();

if(playerExperienceChangeEvent.isCancelled()) {
return false;
}
level = playerExperienceChangeEvent.getNewLevel();
progress = playerExperienceChangeEvent.getNewProgress();

if(level != null) {
this.levelAttribute.setValue(level, true, true);
}
if(progress != null) {
this.progressAttribute.setValue(progress, true, true);
}
return true;
}

public int getXpLevel() {
return (int) this.levelAttribute.getCurrentValue();
}

public boolean setXpLevel(int level) {
return this.setXpAndProgress(level, null);
}

public boolean addXpLevels(int amount) {
int oldLevel = this.getXpLevel();
return this.setXpLevel(oldLevel + amount);
}

public boolean substractXpLevels(int amount) {
return this.addXpLevels(-amount);
}

public float getXpProgress() {
return this.progressAttribute.getCurrentValue();
}

public boolean setXpProgress(float progress) {
return this.setXpAndProgress(null, progress);
}

public int getRemainderXp() {
return (int) (ExperienceUtils.getXpToCompleteLevel(this.getXpLevel()) * this.getXpProgress());
}

public int getCurrentTotalXp() {
return ExperienceUtils.getXpToReachLevel(this.getXpLevel()) + this.getRemainderXp();
}

public boolean setCurrentTotalXp(int amount) {
float newLevel = ExperienceUtils.getLevelFromXp(amount);
int xpLevel = (int) (newLevel - (int) newLevel);
float xpProgress = (int) (newLevel - (int) newLevel);
return setXpAndProgress(xpLevel, xpProgress);
}

public boolean addXp(int amount) {
amount = Math.min(amount, Integer.MAX_VALUE - this.totalXp);
int oldLevel = this.getXpLevel();
int oldTotal = this.getCurrentTotalXp();
if(this.setCurrentTotalXp(oldTotal + amount)) {
if(amount > 0) {
this.totalXp += amount;
}
return true;
}
return false;
}

public boolean subtractXp(int amount) {
return this.addXp(-amount);
}

public void setXpAndProgressNoEvent(int level, float progress) {
this.levelAttribute.setValue(level, true, true);
this.progressAttribute.setValue(progress, true, true);
}

public int getLifetimeTotalXp() {
return this.totalXp;
}

public void setLifetimeTotalXp(int amount) {
if(amount < 0 || amount > Integer.MAX_VALUE) {
throw new IllegalArgumentException("XP must be greater than 0 and less than " + Integer.MAX_VALUE);
}
this.totalXp = amount;
}

public boolean canPickupXp() {
return this.xpCooldown == 0;
}

public void onPickupXp(int xpValue) {
int mainHandIndex = -1;
int offHandIndex = -2;

// TODO: Logic for repair item

this.addXp(xpValue);
this.resetXpCooldown();
}

public void resetXpCooldown() {
this.xpCooldown = 2;
}

public void tick(int tickDiff) {
if(this.xpCooldown > 0) {
this.xpCooldown = Math.max(0, this.xpCooldown - tickDiff);
}
}

public boolean canAttractXpOrbs() {
return this.canAttractXpOrbs;
}

public void setCanAttractXpOrbs(boolean canAttractXpOrbs) {
this.canAttractXpOrbs = canAttractXpOrbs;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.sculk.event.player;


import org.sculk.entity.HumanEntity;
import org.sculk.event.Cancellable;
import org.sculk.event.EntityEvent;

/*
* ____ _ _
* / ___| ___ _ _| | | __
* \___ \ / __| | | | | |/ /
* ___) | (__| |_| | | <
* |____/ \___|\__,_|_|_|\_\
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author: SculkTeams
* @link: http://www.sculkmp.org/
*/
public class PlayerExperienceChangeEvent extends EntityEvent implements Cancellable {

protected HumanEntity humanEntity;
protected int oldLevel;
protected float oldProgress;
protected int newLevel;
protected float newProgress;

public PlayerExperienceChangeEvent(HumanEntity humanEntity, int oldLevel, float oldProgress, int newLevel, float newProgress) {
this.humanEntity = humanEntity;
this.oldLevel = oldLevel;
this.oldProgress = oldProgress;
this.newLevel = newLevel;
this.newProgress = newProgress;
}

public int getOldLevel() {
return oldLevel;
}

public float getOldProgress() {
return oldProgress;
}

public int getNewLevel() {
return newLevel;
}

public float getNewProgress() {
return newProgress;
}

public void setNewLevel(int newLevel) {
this.newLevel = newLevel;
}

public void setNewProgress(float newProgress) {
if(newProgress < 0.0 || newProgress > 1.0) {
throw new IllegalArgumentException("XP progress must be range 0-1");
}
this.newProgress = newProgress;
}
}
Loading

0 comments on commit 4f9b9f4

Please sign in to comment.