Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define default EntityFlag.BREATHING true #25

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/main/java/org/sculk/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.cloudburstmc.protocol.bedrock.BedrockServerSession;
import org.cloudburstmc.protocol.bedrock.data.AttributeData;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.data.skin.SerializedSkin;
import org.cloudburstmc.protocol.bedrock.packet.*;
import org.sculk.entity.Attribute;
import org.sculk.entity.AttributeFactory;
import org.sculk.entity.HumanEntity;
import org.sculk.entity.data.SyncedEntityData;
import org.sculk.form.Form;
import org.sculk.player.PlayerInterface;
import org.sculk.player.client.ClientChainData;
Expand Down Expand Up @@ -35,6 +37,7 @@
public class Player extends HumanEntity implements PlayerInterface {

private final BedrockServerSession serverSession;
private final SyncedEntityData data = new SyncedEntityData(this);
private LoginChainData loginChainData;

private AtomicInteger formId;
Expand All @@ -47,6 +50,20 @@ public Player(BedrockServerSession session, ClientChainData data) {

this.formId = new AtomicInteger(0);
this.forms = new Int2ObjectOpenHashMap<>();

initEntity();
}

@Override
public void initEntity() {
super.initEntity();
System.out.println("init Entity");

}

public void updateFlags() {
this.data.setFlags(EntityFlag.BREATHING, true);
this.data.updateFlag();
}

public void kick(String message) {
Expand Down Expand Up @@ -124,13 +141,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();
}

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


import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataMap;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityDataPacket;
import org.sculk.Player;

import java.util.EnumSet;

/*
* ____ _ _
* / ___| ___ _ _| | | __
* \___ \ / __| | | | | |/ /
* ___) | (__| |_| | | <
* |____/ \___|\__,_|_|_|\_\
*
* 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 SyncedEntityData {

private final EnumSet<EntityFlag> flags = EnumSet.noneOf(EntityFlag.class);
private final EntityDataMap entityDataMap = new EntityDataMap();

private final Player player;

public SyncedEntityData(Player player) {
this.player = player;
}

public void updateFlag() {
SetEntityDataPacket setEntityDataPacket = new SetEntityDataPacket();
setEntityDataPacket.getMetadata().putFlags(this.flags);
this.player.sendDataPacket(setEntityDataPacket);
}

public boolean getFlag(EntityFlag entityFlag) {
return flags.contains(entityFlag);
}

public void setFlags(EntityFlag flags, boolean value) {
if(this.flags.contains(flags) != value) {
if(value) {
this.flags.add(flags);
} else {
this.flags.remove(flags);
}
this.entityDataMap.putFlags(this.flags);
}
}

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

}
Loading