Made DamageType a DataContainer

This commit is contained in:
themode 2020-09-05 16:45:33 +02:00
parent cb52a9d119
commit 7b93881e59
2 changed files with 22 additions and 6 deletions

View File

@ -1,18 +1,21 @@
package net.minestom.server.data;
/**
* Represent an element which can have a {@link Data} attached to it
*/
public interface DataContainer {
/**
* Get the data of this container
* Get the {@link Data} of this container
*
* @return the data of this container, can be null
* @return the {@link Data} of this container, can be null
*/
Data getData();
/**
* Set the data object of this container
* Set the {@link Data} of this container
*
* @param data the data of this container
* @param data the {@link Data} of this container, null to remove it
*/
void setData(Data data);

View File

@ -2,6 +2,8 @@ package net.minestom.server.entity.damage;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.RichMessage;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.Player;
@ -10,7 +12,7 @@ import net.minestom.server.sound.Sound;
/**
* Represents a type of damage
*/
public class DamageType {
public class DamageType implements DataContainer {
public static final DamageType VOID = new DamageType("attack.outOfWorld");
public static final DamageType GRAVITY = new DamageType("attack.fall");
@ -21,6 +23,7 @@ public class DamageType {
}
};
private final String identifier;
private Data data;
public DamageType(String identifier) {
this.identifier = identifier;
@ -35,7 +38,7 @@ public class DamageType {
}
public RichMessage buildChatMessage(Player killed) {
RichMessage richMessage = RichMessage.of(ColoredText.ofFormat("{@death." + identifier + ","+killed.getUsername()+"}"));
RichMessage richMessage = RichMessage.of(ColoredText.ofFormat("{@death." + identifier + "," + killed.getUsername() + "}"));
return richMessage;
}
@ -71,4 +74,14 @@ public class DamageType {
protected Sound getPlayerSound(Player player) {
return Sound.ENTITY_PLAYER_HURT;
}
@Override
public Data getData() {
return data;
}
@Override
public void setData(Data data) {
this.data = data;
}
}