Refactor stackable items codebase
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.bartlomiejpluta.demo.world.item;
|
||||
|
||||
public interface ItemStack extends Item {
|
||||
String getId();
|
||||
|
||||
int getCount();
|
||||
|
||||
void setCount(int count);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.bartlomiejpluta.demo.world.item;
|
||||
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
import lombok.NonNull;
|
||||
|
||||
public abstract class UseableStackableItem extends StackableItem implements Useable {
|
||||
|
||||
protected UseableStackableItem(@NonNull String id, int count) {
|
||||
super(id, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
if (--count == 0) {
|
||||
creature.removeItemFromEquipment(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,16 @@ package com.bartlomiejpluta.demo.world.potion;
|
||||
import DB.dao;
|
||||
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
import com.bartlomiejpluta.demo.world.item.StackableItem;
|
||||
import com.bartlomiejpluta.demo.world.item.Useable;
|
||||
import com.bartlomiejpluta.demo.world.item.UseableStackableItem;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class Medicament extends StackableItem implements Useable {
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class Medicament extends UseableStackableItem implements Useable {
|
||||
@Getter
|
||||
private final String id;
|
||||
|
||||
@Getter
|
||||
private final String name;
|
||||
@@ -24,14 +28,12 @@ public class Medicament extends StackableItem implements Useable {
|
||||
super(template.getIcon(), count);
|
||||
this.name = template.getName();
|
||||
this.roller = DiceRoller.of(template.getHp());
|
||||
this.id = format("med:%s", template.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
if(--count == 0) {
|
||||
creature.removeItemFromEquipment(this);
|
||||
}
|
||||
|
||||
super.use(creature);
|
||||
creature.heal(roller.roll());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
@ToString
|
||||
public class Ammunition extends StackableItem implements Useable {
|
||||
|
||||
@@ -26,7 +28,7 @@ public class Ammunition extends StackableItem implements Useable {
|
||||
public Ammunition(@NonNull DB.model.AmmunitionModel template, int count) {
|
||||
super(template.getIcon(), count);
|
||||
|
||||
this.id = template.getId();
|
||||
this.id = format("ammo:%s", template.getId());
|
||||
this.name = template.getName();
|
||||
this.appliesTo = template.getAppliesTo();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user