Skip to content

Commit

Permalink
Create a new Byte datafield. (#5714)
Browse files Browse the repository at this point in the history
* Create a new Byte datafield.

* Clear errer in metadataloader, fill out metadatautil.

* Javadoc fix

Co-authored-by: Llm Dl <LlmDlio@gmail.com>
  • Loading branch information
Warriorrrr and LlmDl authored Feb 6, 2022
1 parent 2801070 commit 04cc8a6
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.palmergames.bukkit.towny.object.metadata;

import org.jetbrains.annotations.NotNull;

public class ByteDataField extends CustomDataField<Byte> {

public ByteDataField(String key) {
super(key);
}

public ByteDataField(String key, byte value, String label) {
super(key, value, label);
}

public ByteDataField(String key, byte value) {
super(key, value);
}

@Override
public @NotNull String getTypeID() {
return typeID();
}

public static String typeID() {
return "towny_bytedf";
}

@Override
public void setValueFromString(String strValue) {
setValue(Byte.parseByte(strValue));
}

@Override
public boolean canParseFromString(String str) {
try {
Byte.parseByte(str);
} catch (NumberFormatException e) {
return false;
}
return true;
}

@Override
public String displayFormattedValue() {
return String.valueOf(getValue());
}

@Override
public @NotNull CustomDataField<Byte> clone() {
return new ByteDataField(getKey(), getValue(), this.label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private MetadataLoader() {
deserializerMap.put(BooleanDataField.typeID(), TownyCDFDeserializer.BOOLEAN_DF);
deserializerMap.put(DecimalDataField.typeID(), TownyCDFDeserializer.DECIMAL_DF);
deserializerMap.put(LongDataField.typeID(), TownyCDFDeserializer.LONG_DF);
deserializerMap.put(ByteDataField.typeID(), TownyCDFDeserializer.BYTE_DF);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ private static <T extends CustomDataField<?>> T deserializeDF(T cdf, String valu
static final DataFieldDeserializer<LongDataField> LONG_DF =
(key, value) -> deserializeDF(new LongDataField(key), value);

static final DataFieldDeserializer<ByteDataField> BYTE_DF =
(key, value) -> deserializeDF(new ByteDataField(key), value);
}
64 changes: 64 additions & 0 deletions src/com/palmergames/bukkit/towny/utils/MetaDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.palmergames.bukkit.towny.object.TownyObject;
import com.palmergames.bukkit.towny.object.metadata.BooleanDataField;
import com.palmergames.bukkit.towny.object.metadata.ByteDataField;
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
import com.palmergames.bukkit.towny.object.metadata.DecimalDataField;
import com.palmergames.bukkit.towny.object.metadata.IntegerDataField;
Expand Down Expand Up @@ -64,6 +65,16 @@ public static boolean hasMeta(TownyObject townyObject, IntegerDataField idf) {
public static boolean hasMeta(TownyObject townyObject, DecimalDataField ddf) {
return townyObject.hasMeta(ddf.getKey());
}

/**
* Does the TownyObject have the ByteDataField meta?
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
* @param bdf ByteDataField to check for on the TownyObject.
* @return true if the TownyObject has the ByteDataField meta.
*/
public static boolean hasMeta(TownyObject townyObject, ByteDataField bdf) {
return townyObject.hasMeta(bdf.getKey());
}

/**
* Get a string from a TownyObject's metadata.
Expand Down Expand Up @@ -136,6 +147,20 @@ public static double getDouble(TownyObject townyObject, DecimalDataField ddf) {
return 0.0;
}

/**
* Get a byte from a TownyObject's metadata.
*
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
* @param bdf ByteDataField to get from the TownyObject.
* @return byte from the metadata or 0.
*/
public static byte getByte(TownyObject townyObject, ByteDataField bdf) {
CustomDataField<?> cdf = townyObject.getMetadata(bdf.getKey());
if (cdf instanceof ByteDataField)
return ((ByteDataField) cdf).getValue();
return 0;
}

/**
* Adds a new StringDataField MetaData to a TownyObject, overriding any existing MetaData with the same key.
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
Expand Down Expand Up @@ -186,6 +211,16 @@ public static void addNewMeta(TownyObject townyObject, DecimalDataField ddf, boo
townyObject.addMetaData(ddf, save);
}

/**
* Adds a new ByteDataField MetaData to a TownyObject, overriding any existing MetaData with the same key.
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
* @param bdf ByteDataField to apply to the TownyObject.
* @param save set true to save the object after applying the MetaData.
*/
public static void addNewMeta(TownyObject townyObject, ByteDataField bdf, boolean save) {
townyObject.addMetaData(bdf, save);
}

/**
* Creates and adds a new StringDataField MetaData to a TownyObject, overriding any existing MetaData with the same key.
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
Expand Down Expand Up @@ -241,6 +276,17 @@ public static void addNewDoubleMeta(TownyObject townyObject, String key, double
addNewMeta(townyObject, new DecimalDataField(key, value), save);
}

/**
* Creates and adds a new ByteDataField MetaData to a TownyObject, overriding any existing MetaData with the same key.
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
* @param key String name of the new ByteDataField key.
* @param value byte value of the ByteDataField key.
* @param save set true to save the object after applying the MetaData.
*/
public static void addNewByteMeta(TownyObject townyObject, String key, byte value, boolean save) {
addNewMeta(townyObject, new ByteDataField(key, value), save);
}

/**
* Sets a StringDataField metadata on a TownyObject.
*
Expand Down Expand Up @@ -330,4 +376,22 @@ public static void setDouble(TownyObject townyObject, DecimalDataField ddf, doub
townyObject.save();
}
}

/**
* Sets a ByteDataField metadata on a TownyObject.
*
* @param townyObject TownyObject, ie: Resident, Town, Nation, TownBlock.
* @param bdf ByteDataField to apply to the TownyObject.
* @param num value to apply to the ByteDataField.
* @param save True to save the TownyObject after setting the metadata.
*/
public static void setByte(TownyObject townyObject, ByteDataField bdf, byte num, boolean save) {
CustomDataField<?> cdf = townyObject.getMetadata(bdf.getKey());
if (cdf instanceof ByteDataField) {
ByteDataField value = (ByteDataField) cdf;
value.setValue(num);
if (save)
townyObject.save();
}
}
}

0 comments on commit 04cc8a6

Please sign in to comment.