Consumed item charges are now set correctly and is supported by splitting/stacking #281

This commit is contained in:
Ziggy
2020-11-29 10:46:08 +01:00
parent a601a0387b
commit 64a61d5b5e
4 changed files with 66 additions and 16 deletions

View File

@@ -204,7 +204,8 @@ class StaticItemLoader internal constructor() : DataLoader() {
}
class GenericItemInfo(set: SdbResultSet, colorArray: SdbIntegerColumnArraySet) {
val charges: Int = set.getInt("charges").toInt()
val color: IntArray = Arrays.copyOfRange(colorArray.getArray(set), 0, 5)
get() = field.clone()
val value: Int = set.getInt("value").toInt()
@@ -213,7 +214,8 @@ class StaticItemLoader internal constructor() : DataLoader() {
}
class ObjectItemInfo(set: SdbResultSet, colorArray: SdbIntegerColumnArraySet) {
val charges: Int = set.getInt("charges").toInt()
val color: IntArray = Arrays.copyOfRange(colorArray.getArray(set), 0, 5)
get() = field.clone()
val value: Int = set.getInt("value").toInt()

View File

@@ -55,6 +55,7 @@ object StaticItemCreator {
applyAttributes(obj, info.wearableInfo)
applyAttributes(obj, info.weaponInfo)
applyAttributes(obj, info.collectionInfo)
applyAttributes(obj, info.consumableInfo)
applyAttributes(obj, info.costumeInfo)
applyAttributes(obj, info.crystalInfo)
applyAttributes(obj, info.dnaInfo)
@@ -175,6 +176,16 @@ object StaticItemCreator {
applyColors(obj, info.color)
}
private fun applyAttributes(obj: TangibleObject, info: StaticItemLoader.ConsumableItemInfo?) {
if (info == null)
return
if (info.charges != 0) {
obj.addAttribute("charges", info.charges.toString())
obj.counter = info.charges
}
}
@Suppress("UNUSED_PARAMETER")
private fun applyAttributes(obj: TangibleObject, info: StaticItemLoader.CostumeItemInfo?) {
@@ -213,20 +224,24 @@ object StaticItemCreator {
private fun applyAttributes(obj: TangibleObject, info: StaticItemLoader.GenericItemInfo?) {
if (info == null)
return
if (info.value != 0)
obj.addAttribute("charges", info.value.toString())
if (info.charges != 0) {
obj.addAttribute("charges", info.charges.toString())
obj.counter = info.charges
}
applyColors(obj, info.color)
}
private fun applyAttributes(obj: TangibleObject, info: StaticItemLoader.ObjectItemInfo?) {
if (info == null)
return
if (info.value != 0)
obj.addAttribute("charges", info.value.toString())
if (info.charges != 0) {
obj.addAttribute("charges", info.charges.toString())
obj.counter = info.charges
}
applyColors(obj, info.color)
}

View File

@@ -13,6 +13,7 @@ import com.projectswg.holocore.resources.support.objects.swg.SWGObject;
import com.projectswg.holocore.resources.support.objects.swg.tangible.TangibleObject;
import java.util.Collection;
import java.util.Map;
public class SWGObjectRadial implements RadialHandlerInterface {
@@ -88,14 +89,30 @@ public class SWGObjectRadial implements RadialHandlerInterface {
}
// Create new object using same template
// TODO needs to copy other stuff as well, such as customization variables and object attributes
String template = originalStack.getTemplate();
TangibleObject newStack = (TangibleObject) ObjectCreator.createObjectFromTemplate(template);
for (Map.Entry<String, String> attributeEntry : originalStack.getAttributes().entrySet()) {
newStack.addAttribute(attributeEntry.getKey(), attributeEntry.getValue());
}
newStack.setObjectName(originalStack.getObjectName());
newStack.setStringId(originalStack.getStringId());
newStack.setDetailStf(originalStack.getDetailStringId());
// Adjust stack sizes
originalStack.setCounter(oldStackSize);
newStack.setCounter(newStackSize);
if (originalStack.hasAttribute("charges")) {
originalStack.addAttribute("charges", String.valueOf(oldStackSize));
newStack.addAttribute("charges", String.valueOf(newStackSize));
}
for (Map.Entry<String, Integer> customizationEntry : originalStack.getCustomization().entrySet()) {
newStack.putCustomization(customizationEntry.getKey(), customizationEntry.getValue());
}
// We don't use moveToContainer, because that would trigger auto-stacking.
newStack.systemMove(container);

View File

@@ -102,12 +102,15 @@ public class TangibleObject extends SWGObject {
if (newParent != null && counter > 0) {
// Scan container for matching stackable item
String ourTemplate = getTemplate();
Map<String, String> ourAttributes = getAttributes();
Map<String, String> ourAttributes = new HashMap<>(getAttributes());
TangibleObject bestMatch = null;
ourAttributes.remove("charges"); // Don't compare charges count
for (SWGObject candidate : newParent.getContainedObjects()) {
String theirTemplate = candidate.getTemplate();
Map<String, String> theirAttributes = candidate.getAttributes();
Map<String, String> theirAttributes = new HashMap<>(candidate.getAttributes());
theirAttributes.remove("charges"); // Don't compare charges count
if (candidate == this)
continue; // Can't transfer into itself
@@ -126,9 +129,16 @@ public class TangibleObject extends SWGObject {
if (bestMatch != null) {
int theirCounter = bestMatch.getCounter();
int transferAmount = Math.min(bestMatch.getMaxCounter() - theirCounter, counter);
int added = theirCounter + transferAmount;
int subtracted = counter - transferAmount;
bestMatch.setCounter(theirCounter + transferAmount);
setCounter(counter - transferAmount);
if (hasAttribute("charges")) {
addAttribute("charges", String.valueOf(subtracted));
bestMatch.addAttribute("charges", String.valueOf(added));
}
bestMatch.setCounter(added);
setCounter(subtracted);
if (getCounter() > 0)
return true;
DestroyObjectIntent.broadcast(this);
@@ -443,7 +453,7 @@ public class TangibleObject extends SWGObject {
@Override
public void save(NetBufferStream stream) {
super.save(stream);
stream.addByte(1);
stream.addByte(2);
appearanceData.save(stream);
stream.addInt(maxHitPoints);
stream.addInt(components);
@@ -459,6 +469,7 @@ public class TangibleObject extends SWGObject {
stream.addAscii(e.getKey());
stream.addAscii(e.getValue());
});
stream.addInt(counter);
}
@Override
@@ -478,6 +489,9 @@ public class TangibleObject extends SWGObject {
objectEffects = stream.getArray();
optionFlags = stream.getInt();
stream.getList((i) -> effectsMap.put(stream.getAscii(), stream.getAscii()));
if (version == 2) {
counter = stream.getInt();
}
}
@Override
@@ -495,6 +509,7 @@ public class TangibleObject extends SWGObject {
data.putByteArray("objectEffects", objectEffects);
data.putInteger("optionFlags", optionFlags);
data.putMap("effectsMap", effectsMap);
data.putInteger("counter", counter);
}
@Override
@@ -511,5 +526,6 @@ public class TangibleObject extends SWGObject {
objectEffects = data.getByteArray("objectEffects");
optionFlags = data.getInteger("optionFlags", 0);
effectsMap.putAll(data.getMap("effectsMap", String.class, String.class));
counter = data.getInteger("counter", 0);
}
}