Skip to content

Commit

Permalink
sponge inventory bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NeumimTo committed May 1, 2020
1 parent 0716c1f commit 966961c
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RepeatingSkill extends ActiveSkill {

@Override
public WrappedSkill.WrappedSkillData constructSkillData() {
return new RepeatingSkillData(getId(), super.constructSkillData());
return new RepeatingSkillData(getId());
}

@Override
Expand Down Expand Up @@ -58,8 +58,8 @@ public static class RepeatingSkillData extends WrappedSkill.WrappedSkillData {
public long period;
public long maxDuartion;

public RepeatingSkillData(String skill, SkillData wrapped) {
super(skill, wrapped);
public RepeatingSkillData(String skill) {
super(skill);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setInner(ISkill inner) {

@Override
public WrappedSkillData constructSkillData() {
return new WrappedSkillData(getId(), inner.constructSkillData());
return new WrappedSkillData(getId());
}

@Override
Expand Down Expand Up @@ -116,9 +116,8 @@ public static class WrappedSkillData extends SkillData {

public SkillData wrapped;

public WrappedSkillData(String skill, SkillData wrapped) {
public WrappedSkillData(String skill) {
super(skill);
this.wrapped = wrapped;
}

public SkillData getWrapped() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import cz.neumimto.rpg.api.localization.LocalizationService;
import cz.neumimto.rpg.api.logging.Log;
import cz.neumimto.rpg.api.skills.*;
import cz.neumimto.rpg.api.skills.WrappedSkill.WrappedSkillData;
import cz.neumimto.rpg.api.skills.mods.ActiveSkillPreProcessorWrapper;
import cz.neumimto.rpg.api.skills.scripting.ScriptedSkillNodeDescription;
import cz.neumimto.rpg.api.skills.tree.SkillTree;
Expand Down Expand Up @@ -209,8 +210,8 @@ protected void loadSkill(SkillTree skillTree, Config c) {
String skillId = c.getString("SkillId");
try {
Config parent = c.getConfig("Parent");

SkillData info = createSkillInfo(skillTree, skillId);
String wrappedSkillId = parent.getString("SkillId");
WrappedSkillData info = (WrappedSkillData) createSkillInfo(skillTree, wrappedSkillId);

loadSkill(skillTree, parent, info);
} catch (ConfigException ignored) {}
Expand Down Expand Up @@ -481,6 +482,7 @@ private SkillData getSkillInfo(String id, SkillTree tree) {
SkillData info = tree.getSkills().get(lowercased);
if (info == null) {
info = createSkillInfo(tree, lowercased);
tree.addSkill(info);
}
return info;
}
Expand All @@ -491,9 +493,18 @@ private SkillData createSkillInfo(SkillTree tree, String lowercased) {
if (skill == null) {
throw new IllegalStateException("Could not find a skill " + lowercased + " referenced in the skilltree " + tree.getId());
}
SkillData info = skill.constructSkillData();
SkillData info = constructSkillData(skill);
info.setSkill(skill);
tree.addSkill(info);
return info;
}

private SkillData constructSkillData(ISkill skill) {
SkillData info = skill.constructSkillData();
if (info instanceof WrappedSkillData) {
WrappedSkillData data = (WrappedSkillData) info;
SkillData wrapped = constructSkillData(data.getSkill());
data.setWrapped(wrapped);
}
return info;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public void load() {
try {
StringBuilder finalString = new StringBuilder();
file.createNewFile();
String s = assetService.getAssetAsString("templates/Effect.md");
String template = assetService.getAssetAsString("templates/Effect.md");
for (Map.Entry<String, IGlobalEffect> effect : globalEffects.entrySet()) {
Class aClass = effect.getValue().asEffectClass();
if (aClass != null && aClass.isAnnotationPresent(Generate.class)) {
Expand All @@ -424,6 +424,7 @@ public void load() {

Class<?> modelType = EffectModelFactory.getModelType(aClass);

String s = new String(template);
s = s.replaceAll("\\{\\{effect\\.name}}", name);
s = s.replaceAll("\\{\\{effect\\.description}}", description);

Expand All @@ -448,12 +449,12 @@ public void load() {
}
}

s = assetService.getAssetAsString("templates/Skill.md");
template = assetService.getAssetAsString("templates/Skill.md");
StringBuilder skills = new StringBuilder();
for (ISkill iSkill : skillService.getAll()) {

String damageType = iSkill.getDamageType();

String s = new String(template);
s = s.replaceAll("\\{\\{skill\\.damageType}}", damageType == null ? "Deals no damage" : damageType);

String id = iSkill.getId();
Expand All @@ -468,9 +469,9 @@ public void load() {
s = s.replaceAll("\\{\\{skill\\.parameters}}", buffer.toString());
skills.append(s);
}
s = assetService.getAssetAsString("templates/SE.md");
template = assetService.getAssetAsString("templates/SE.md");

Files.write(file.toPath(), s.replaceAll("\\{\\{effects}}", finalString.toString())
Files.write(file.toPath(), template.replaceAll("\\{\\{effects}}", finalString.toString())
.replaceAll("\\{\\{skills}}", skills.toString()).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cz.neumimto.rpg.api.inventory.InventoryService;
import cz.neumimto.rpg.api.inventory.ManagedSlot;
import cz.neumimto.rpg.api.inventory.RpgInventory;
import cz.neumimto.rpg.api.items.ItemClass;
import cz.neumimto.rpg.api.items.ItemService;
import cz.neumimto.rpg.api.items.subtypes.ItemSubtype;
import cz.neumimto.rpg.api.items.subtypes.ItemSubtypes;
Expand Down Expand Up @@ -59,7 +60,10 @@ public void initializeManagedSlots(T activeCharacter) {
} else if (armorIds.contains(value.getSlotId())) {
slot = new FilteredManagedSlotImpl(value.getSlotId(), item -> activeCharacter.canWear(item.getItemType()));
} else if (value.getSlotId() >= 0 && value.getSlotId() < 9) {
slot = new FilteredManagedSlotImpl(value.getSlotId(), item -> activeCharacter.canUse(item.getItemType(), EntityHand.MAIN));
slot = new FilteredManagedSlotImpl(value.getSlotId(), item
-> activeCharacter.canUse(item.getItemType(), EntityHand.MAIN));
// || item.getItemType().getItemClass() == ItemClass.ARMOR
// || item.getItemType().getItemClass() == ItemClass.SHIELD);
} else {
slot = new ManagedSlotImpl(value.getSlotId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ gui.label.skill.setting-per-level=Value Per Level
gui.button.label.confirm.skill-selection=Confirm
gui.label.weapons.tooltip=
gui.label.weapons=Weapons
gui.label.item-class=Item Class
gui.label.armor=Armor
label.attribute-points=Attribute Points
gui.label.armor.tooltip=
Expand Down
3 changes: 2 additions & 1 deletion Common/src/test/resources/testconfig/SkillTree01.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Description: 12345
Skills: [
{
SkillId: "test"
SkillName: "test1"
MaxSkillLevel: 2
Combination: LLRQ
MinPlayerLevel: 2
Expand All @@ -11,6 +12,7 @@ Skills: [
}
{
SkillId: "test2"
SkillName: "test2"
MaxSkillLevel: 2
Combination: LLRQ
MinPlayerLevel: 2
Expand All @@ -20,5 +22,4 @@ Skills: [
asdf
]
}

]
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ public static Inventory getCharacterAllowedWeapons(ISpongeCharacter character, i
content.add(is);
}
DynamicInventory inv = dView.setActualContent(content.toArray(new ItemStack[content.size() == 0 ? 0 : content.size() - 1]));
String translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.CHARACTER_ARMOR);
String translate = Rpg.get().getLocalizationService().translate(LocalizationKeys.CHARACTER_WEAPONS);
inventory = createInventoryTemplate(translate);
inv.fill(inventory);
CACHED_MENUS.put(name, inventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public void dumpItems(GameStartedServerEvent event) {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
continue;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
import cz.neumimto.rpg.api.inventory.InventoryService;
import cz.neumimto.rpg.api.inventory.ManagedSlot;
import cz.neumimto.rpg.api.inventory.RpgInventory;
import cz.neumimto.rpg.api.items.ItemClass;
import cz.neumimto.rpg.api.items.RpgItemStack;
import cz.neumimto.rpg.api.items.RpgItemType;
import cz.neumimto.rpg.api.localization.LocalizationKeys;
import cz.neumimto.rpg.api.localization.LocalizationService;
import cz.neumimto.rpg.common.inventory.InventoryHandler;
import cz.neumimto.rpg.sponge.entities.players.ISpongeCharacter;
import cz.neumimto.rpg.sponge.entities.players.SpongeCharacterService;
Expand Down Expand Up @@ -53,6 +57,10 @@
import org.spongepowered.api.item.inventory.query.QueryOperationTypes;
import org.spongepowered.api.item.inventory.transaction.SlotTransaction;
import org.spongepowered.api.item.inventory.type.CarriedInventory;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatType;
import org.spongepowered.api.text.chat.ChatTypes;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.Tristate;
import org.spongepowered.common.event.tracking.phase.packet.drag.DragInventoryAddSlotState;

Expand All @@ -79,6 +87,8 @@ public class InventoryListener {
private InventoryService inventoryService;
@Inject
private SpongeItemService itemService;
@Inject
private LocalizationService localizationService;

@Listener
@IsCancelled(Tristate.FALSE)
Expand Down Expand Up @@ -178,6 +188,8 @@ public void onHotbarInteract(HandInteractEvent event, @First Player player) {
ItemStackUtils.dropItem(player, itemStack);
player.setItemInHand(HandTypes.MAIN_HAND, ItemStack.empty());
character.setLastHotbarSlotInteraction(-1);
String translate = localizationService.translate(LocalizationKeys.CANNOT_USE_ITEM_CONFIGURATION_REASON);
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, translate));
event.setCancelled(true);
character.setRequiresDamageRecalculation(true);
}
Expand All @@ -195,71 +207,71 @@ public void onHotbarInteract(HandInteractEvent event, @First Player player) {
@Include({
ClickInventoryEvent.Primary.class,
ClickInventoryEvent.Secondary.class,
ClickInventoryEvent.Drag.class
ClickInventoryEvent.Drag.class,
ClickInventoryEvent.Shift.class
})
public void onInteract(ClickInventoryEvent event, @Root Player player) {
final List<SlotTransaction> transactions = event.getTransactions();
switch (transactions.size()) {
case 1:
SlotTransaction slotTransaction = transactions.get(0);
Slot slot = slotTransaction.getSlot();
Slot transformed = slot.transform();
Class aClass = transformed.parent().getClass();
int slotId = transformed.getInventoryProperty(SlotIndex.class).get().getValue();
if (!inventoryService.isManagedInventory(aClass, slotId)) {
return;
}
IActiveCharacter character = characterService.getCharacter(player);
Map<Class<?>, RpgInventory> managedInventory = character.getManagedInventory();
RpgInventory rpgInventory = managedInventory.get(aClass);
ManagedSlot managedSlot = rpgInventory.getManagedSlots().get(slotId);
Optional<RpgItemStack> future = itemService.getRpgItemStack(slotTransaction.getFinal().createStack());
Optional<RpgItemStack> original = itemService.getRpgItemStack(slotTransaction.getOriginal().createStack());

if (future.isPresent()) {
RpgItemStack rpgItemStackF = future.get();
//change
if (original.isPresent()) {

RpgItemStack rpgItemStackO = original.get();

boolean k = inventoryHandler.handleCharacterEquipActionPre(character, managedSlot, rpgItemStackF)
&& inventoryHandler.handleCharacterUnEquipActionPre(character, managedSlot, rpgItemStackO);
if (k) {
inventoryHandler.handleCharacterUnEquipActionPost(character, managedSlot);
inventoryHandler.handleCharacterEquipActionPost(character, managedSlot, rpgItemStackF);
character.setRequiresDamageRecalculation(true);
} else {
event.setCancelled(true);
}
for (SlotTransaction slotTransaction : transactions) {
Slot slot = slotTransaction.getSlot();
Slot transformed = slot.transform();
Class aClass = transformed.parent().getClass();
int slotId = transformed.getInventoryProperty(SlotIndex.class).get().getValue();
if (!inventoryService.isManagedInventory(aClass, slotId)) {
return;
}
IActiveCharacter character = characterService.getCharacter(player);
Map<Class<?>, RpgInventory> managedInventory = character.getManagedInventory();
RpgInventory rpgInventory = managedInventory.get(aClass);
ManagedSlot managedSlot = rpgInventory.getManagedSlots().get(slotId);
Optional<RpgItemStack> future = itemService.getRpgItemStack(slotTransaction.getFinal().createStack());
Optional<RpgItemStack> original = itemService.getRpgItemStack(slotTransaction.getOriginal().createStack());

if (future.isPresent()) {
RpgItemStack rpgItemStackF = future.get();
//change
if (original.isPresent()) {

RpgItemStack rpgItemStackO = original.get();

boolean k = inventoryHandler.handleCharacterEquipActionPre(character, managedSlot, rpgItemStackF)
&& inventoryHandler.handleCharacterUnEquipActionPre(character, managedSlot, rpgItemStackO);
if (k) {
inventoryHandler.handleCharacterUnEquipActionPost(character, managedSlot);
inventoryHandler.handleCharacterEquipActionPost(character, managedSlot, rpgItemStackF);
character.setRequiresDamageRecalculation(true);
} else {
//equip
if (inventoryHandler.handleCharacterEquipActionPre(character, managedSlot, rpgItemStackF)) {
inventoryHandler.handleCharacterEquipActionPost(character, managedSlot, rpgItemStackF);
character.setRequiresDamageRecalculation(true);
} else {
event.setCancelled(true);
}
event.setCancelled(true);
return;
}

} else {
//unequip slot
if (original.isPresent()) {
RpgItemStack rpgItemStack = original.get();
if (inventoryHandler.handleCharacterUnEquipActionPre(character, managedSlot, rpgItemStack)) {
inventoryHandler.handleCharacterUnEquipActionPost(character, managedSlot);
}
//equip
//todo do we want to apply custom enchantments from armor from hotbar? I think not => make this configurable
ItemClass itemClass = rpgItemStackF.getItemType().getItemClass();
if (slotId >=0 && slotId <=8 && (itemClass == ItemClass.ARMOR || itemClass == ItemClass.SHIELD)) {
continue;
}

if (inventoryHandler.handleCharacterEquipActionPre(character, managedSlot, rpgItemStackF)) {
inventoryHandler.handleCharacterEquipActionPost(character, managedSlot, rpgItemStackF);
character.setRequiresDamageRecalculation(true);
} else {
event.setCancelled(true);
return;
}

}

} else {
//unequip slot
if (original.isPresent()) {
RpgItemStack rpgItemStack = original.get();
if (inventoryHandler.handleCharacterUnEquipActionPre(character, managedSlot, rpgItemStack)) {
inventoryHandler.handleCharacterUnEquipActionPost(character, managedSlot);
}
}
break;
case 2:
//???
break;
default:
//???//???
return;
}
}

}

@Listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ gui.label.skill.setting-start-at=Base Value
gui.label.skill.setting-per-level=Value Per Level
gui.button.label.confirm.skill-selection=Confirm
gui.label.weapons.tooltip=
gui.label.item-class=Item Class
gui.label.weapons=Weapons
gui.label.armor=Armor
gui.label.armor.tooltip=
Expand Down

0 comments on commit 966961c

Please sign in to comment.