Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
improve inventory remember system
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 6, 2018
1 parent 6f4c0b9 commit 99aefe3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 30 deletions.
Expand Up @@ -9,6 +9,7 @@
import com.denizenscript.denizen2sponge.commands.entity.*;
import com.denizenscript.denizen2sponge.commands.items.CreateInventoryCommand;
import com.denizenscript.denizen2sponge.commands.items.ForgetInventoryCommand;
import com.denizenscript.denizen2sponge.commands.items.GiveCommand;
import com.denizenscript.denizen2sponge.commands.items.RememberInventoryCommand;
import com.denizenscript.denizen2sponge.commands.player.*;
import com.denizenscript.denizen2sponge.commands.server.AnnounceCommand;
Expand Down
Expand Up @@ -24,7 +24,7 @@ public class CreateInventoryCommand extends AbstractCommand {
// @Save createinventory_inv (InventoryTag) returns the created inventory.
// @Description
// Creates a new inventory of the given type.
// See also rememberinventory command.
// See also the <@link command rememberinventory>rememberinventory command<@/link>.
// @Example
// # This example creates an inventory and saves it in definition "test_inv".
// - createinventory CHEST --save test_inv
Expand Down Expand Up @@ -72,6 +72,6 @@ public void execute(CommandQueue queue, CommandEntry entry) {
return;
}
InventoryTag inv = new InventoryTag(Inventory.builder().of(arch).build(Denizen2Sponge.plugin));
queue.commandStack.peek().setDefinition(entry.resName(queue, "rememberinventory_inv"), inv);
queue.commandStack.peek().setDefinition(entry.resName(queue, "createinventory_inv"), inv);
}
}
Expand Up @@ -3,22 +3,25 @@
import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;

public class ForgetInventoryCommand extends AbstractCommand {

// <--[command]
// @Since 0.3.0
// @Name forgetinventory
// @Arguments <name>
// @Arguments <inventory/name>
// @Short forgets a remembered inventory.
// @Updated 2017/06/11
// @Updated 2018/06/05
// @Group Items
// @Minimum 1
// @Maximum 1
// @Description
// Forgets an inventory that was remembered.
// See also rememberinventory command.
// See also the <@link command rememberinventory>rememberinventory command<@/link>.
// @Example
// # This example forgets the inventory named "Test".
// - forgetinventory "Test"
Expand All @@ -31,7 +34,7 @@ public String getName() {

@Override
public String getArguments() {
return "<name>";
return "<inventory/name>";
}

@Override
Expand All @@ -46,7 +49,22 @@ public int getMaximumArguments() {

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
String name = entry.getArgumentObject(queue, 0).toString();
Denizen2Sponge.rememberedInventories.remove(name);
AbstractTagObject inv = entry.getArgumentObject(queue, 0);
String name;
if (inv instanceof InventoryTag) {
if (((InventoryTag) inv).remAs != null) {
name = ((InventoryTag) inv).remAs;
}
else {
queue.error.run("Trying to forget an inventory that isn't a remembered inventory - " + ColorSet.emphasis + inv.debug());
return;
}
}
else {
name = inv.toString();
}
if (Denizen2Sponge.rememberedInventories.remove(name) == null) {
queue.error.run("Trying to forget an inventory by a name that isn't remembered - " + ColorSet.emphasis + inv.debug());
}
}
}
@@ -1,11 +1,15 @@
package com.denizenscript.denizen2sponge.commands.player;
package com.denizenscript.denizen2sponge.commands.items;

import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import org.spongepowered.api.item.inventory.Carrier;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;

Expand All @@ -24,14 +28,14 @@ public class GiveCommand extends AbstractCommand {
// <--[command]
// @Since 0.3.0
// @Name give
// @Arguments <player> <item>
// @Short gives a player an item.
// @Updated 2016/11/24
// @Group Player
// @Arguments <inventory/entity> <item>
// @Short gives an inventory an item.
// @Updated 2018/06/05
// @Group Items
// @Minimum 2
// @Maximum 2
// @Description
// Gives a player an item.
// Gives an inventory an item. Input an entity that carries an inventory (such as a player) to give to that entity's inventory.
// Related information: <@link explanation Item Types>item types<@/link>.
// TODO: Explain more!
// @Example
Expand All @@ -46,7 +50,7 @@ public String getName() {

@Override
public String getArguments() {
return "<player> <item>";
return "<inventory/entity> <item>";
}

@Override
Expand All @@ -61,21 +65,35 @@ public int getMaximumArguments() {

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
PlayerTag player = PlayerTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
ItemTag item = ItemTag.getFor(queue.error, entry.getArgumentObject(queue, 1), queue);
AbstractTagObject ato = entry.getArgumentObject(queue, 0);
InventoryTag inv;
if (ato instanceof InventoryTag) {
inv = (InventoryTag) ato;
}
else {
EntityTag et = EntityTag.getFor(queue.error, ato);
if (et instanceof Carrier) {
inv = new InventoryTag(((Carrier) et).getInventory());
}
else {
queue.error.run("Entity " + ColorSet.emphasis + et.debug() + ColorSet.warning + " does not carry an inventory!");
return;
}
}
if (queue.shouldShowGood()) {
queue.outGood("Giving " + ColorSet.emphasis + player.debug() + ColorSet.good
+ ": " + ColorSet.emphasis + item.debug());
queue.outGood("Giving " + ColorSet.emphasis + ato.debug() + ColorSet.good + ": " + ColorSet.emphasis + item.debug());
}
InventoryTransactionResult itr = player.getInternal().getInventory().offer(item.getInternal().copy());
InventoryTransactionResult itr = inv.getInternal().offer(item.getInternal().copy());
queue.outGood("Give result: " + ColorSet.emphasis + itr.getType().name());
for (ItemStackSnapshot iss : itr.getReplacedItems()) {
if (queue.shouldShowGood()) {
queue.outGood("Gave: " + new ItemTag(iss.createStack()).debug());
queue.outGood("Successfully gave: " + ColorSet.emphasis + new ItemTag(iss.createStack()).debug());
}
}
for (ItemStackSnapshot iss : itr.getRejectedItems()) {
if (queue.shouldShowGood()) {
queue.outGood("Failed to give: " + new ItemTag(iss.createStack()).debug());
queue.outGood("Failed to give: " + ColorSet.emphasis + new ItemTag(iss.createStack()).debug());
}
}
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;

Expand All @@ -13,17 +14,20 @@ public class RememberInventoryCommand extends AbstractCommand {
// @Name rememberinventory
// @Arguments <inventory> <name>
// @Short remembers an inventory until shutdown.
// @Updated 2017/06/11
// @Updated 2018/06/05
// @Group Items
// @Minimum 2
// @Maximum 2
// @Save rememberinventory_inv (InventoryTag) returns the remembered inventory.
// @Description
// Remembers an inventory until shutdown.
// Does not persist shutdowns.
// Does not persist shutdowns. // TODO: Make persistable! Ideally an argument to this command for persistent or not.
// Will exactly store the original inventory - meaning a player inventory remembered will still be owned by that player.
// TODO: Clear way to duplicate an existing inventory to form a new rememberd inventory.
// Can take any inventory type.
// See also forgetinventory command.
// See also the <@link command forgetinventory>forgetinventory command<@/link>.
// The inventory will be accessible as "shared/<name>".
// Inputting an already used name will override it.
// @Example
// # This example remembers the player's inventory as "Test", and stores it in definition "my_inv".
// - rememberinventory <player.inventory> "Test" --save my_inv
Expand Down Expand Up @@ -56,5 +60,8 @@ public void execute(CommandQueue queue, CommandEntry entry) {
inv.remAs = name;
Denizen2Sponge.rememberedInventories.put(name, inv);
queue.commandStack.peek().setDefinition(entry.resName(queue, "rememberinventory_inv"), inv);
if (queue.shouldShowGood()) {
queue.outGood("Inventory " + ColorSet.emphasis + inv.remAs + ColorSet.good + " remembered as " + ColorSet.emphasis + name);
}
}
}
Expand Up @@ -223,6 +223,7 @@ public ItemStack generateItem(CommandQueue queue) {
if (k == null) {
queue.error.run("Error handling item script '" + ColorSet.emphasis + title + ColorSet.warning
+ "': key '" + ColorSet.emphasis + input.one + ColorSet.warning + "' does not seem to exist.");
return null;
}
DataKeys.tryApply(toRet, k, parseVal(queue, input.two, varBack), queue.error);
}
Expand Down
Expand Up @@ -98,13 +98,23 @@ public Inventory getInternal() {
return new ItemTag(item);
});
// <--[tag]
// @Since 0.5.0
// @Name InventoryTag.remembered_name
// @Updated 2018/06/05
// @Group General Information
// @ReturnType TextTag
// @Returns the inventory's remembered name (as is set by the <@link command rememberinventory>rememberinventory command<@/link>, if any.
// @Example "shared/test" returns "test".
// -->
handlers.put("remembered_name", (dat, obj) -> new TextTag(((InventoryTag) obj).remAs));
// <--[tag]
// @Since 0.3.0
// @Name InventoryTag.name
// @Updated 2017/06/10
// @Group General Information
// @ReturnType TextTag
// @Returns the inventory's name (in English).
// @Example "player/bob" .name might return "Bob".
// @Returns the inventory's name (in English). // TODO: Translation options? Also, output testing?
// @Example "player/bob" .name returns "Bob".
// -->
handlers.put("name", (dat, obj) -> new TextTag(((InventoryTag) obj).internal.getName().get(Locale.ENGLISH)));
// <--[tag]
Expand Down Expand Up @@ -220,15 +230,15 @@ public String toString() {
}

public String toString(boolean unks) {
if (remAs != null) {
return "shared/" + remAs;
}
if (internal instanceof MainPlayerInventory) {
return "player/" + ((PlayerInventory) (internal).parent()).getCarrier().get().getUniqueId().toString();
}
else if (internal instanceof CarriedInventory) {
Object o = ((CarriedInventory) internal).getCarrier().orElse(null);
if (o == null) {
if (remAs != null) {
return "shared/" + remAs;
}
return "((UNKNOWN INVENTORY TYPE))";
}
if (o instanceof Entity) {
Expand All @@ -239,6 +249,10 @@ else if (o instanceof TileEntityCarrier) {
return "block/" + new LocationTag(new UtilLocation(lb.getPosition(), lb.getWorld()));
}
}
else if (remAs != null) {
return "shared/" + remAs;
}
// TODO: Generic inventory save method
if (!unks) {
// TODO: Handle all inventory types somehow???
throw new RuntimeException("Inventory type not known to the system!");
Expand All @@ -260,6 +274,6 @@ public String getTagTypeName() {

@Override
public String debug() {
return toString();
return toString(true);
}
}

0 comments on commit 99aefe3

Please sign in to comment.