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

Commit

Permalink
Arbitrary inventory remembering
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 11, 2017
1 parent 371c73e commit c029723
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java
Expand Up @@ -6,6 +6,9 @@
import com.denizenscript.denizen2core.utilities.debugging.Debug;
import com.denizenscript.denizen2core.utilities.yaml.YAMLConfiguration;
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.RememberInventoryCommand;
import com.denizenscript.denizen2sponge.commands.player.*;
import com.denizenscript.denizen2sponge.commands.server.ExecuteCommand;
import com.denizenscript.denizen2sponge.commands.server.ShutdownCommand;
Expand Down Expand Up @@ -45,6 +48,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;

/**
* Main plugin class for Denizen2Sponge.
Expand Down Expand Up @@ -76,6 +80,8 @@ public static Text parseColor(String inp) {
return TextSerializers.formattingCode(Denizen2Sponge.colorChar).deserialize(inp);
}

public static HashMap<String, InventoryTag> rememberedInventories = new HashMap<>();

@Inject
public Logger logger;

Expand Down Expand Up @@ -131,6 +137,10 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.register(new SpawnCommand());
Denizen2Core.register(new TeleportCommand());
Denizen2Core.register(new UnflagCommand());
// Commands: Item
Denizen2Core.register(new CreateInventoryCommand());
Denizen2Core.register(new ForgetInventoryCommand());
Denizen2Core.register(new RememberInventoryCommand());
// Commands: Player
Denizen2Core.register(new ActionBarCommand());
Denizen2Core.register(new BanCommand());
Expand Down
@@ -0,0 +1,77 @@
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.utilities.CoreUtilities;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryArchetype;
import org.spongepowered.api.item.inventory.InventoryArchetypes;

public class CreateInventoryCommand extends AbstractCommand {

// <--[command]
// @Name createinventory
// @Arguments <type>
// @Short creates a new inventory.
// @Updated 2017/06/11
// @Group Items
// @Minimum 1
// @Maximum 1
// @Save createinventory_inv (InventoryTag) returns the created inventory.
// @Description
// Creates a new inventory of the given type.
// See also rememberinventory command.
// @Example
// # This example creates an inventory and saves it in definition "test_inv".
// - createinventory CHEST --save test_inv
// -->

@Override
public String getName() {
return "createinventory";
}

@Override
public String getArguments() {
return "<type>";
}

@Override
public int getMinimumArguments() {
return 1;
}

@Override
public int getMaximumArguments() {
return 1;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
String type = CoreUtilities.toLowerCase(entry.getArgumentObject(queue, 0).toString());
InventoryArchetype arch = null;
for (InventoryArchetype ia : Sponge.getRegistry().getAllOf(InventoryArchetype.class)) {
if (CoreUtilities.toLowerCase(ia.getId()).equals(type)
|| CoreUtilities.toLowerCase(ia.getName()).equals(type)) {
arch = ia;
}
}
if (arch == null) {
for (InventoryArchetype ia : Sponge.getRegistry().getAllOf(InventoryArchetype.class)) {
if (CoreUtilities.toLowerCase(ia.getId()).contains(type)) {
arch = ia;
}
}
}
if (arch == null) {
queue.handleError(entry, "Unknown inventory type!");
return;
}
InventoryTag inv = new InventoryTag(Inventory.builder().of(arch).build(Denizen2Sponge.plugin));
queue.commandStack.peek().setDefinition(entry.resName(queue, "rememberinventory_inv"), inv);
}
}
@@ -0,0 +1,52 @@
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.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;

public class ForgetInventoryCommand extends AbstractCommand {

// <--[command]
// @Name forgetinventory
// @Arguments <name>
// @Short forgets a remembered inventory.
// @Updated 2017/06/11
// @Group Items
// @Minimum 1
// @Maximum 1
// @Description
// Forgets an inventory that was remembered.
// See also rememberinventory command.
// @Example
// # This example forgets the inventory named "Test".
// - forgetinventory "Test"
// -->

@Override
public String getName() {
return "forgetinventory";
}

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

@Override
public int getMinimumArguments() {
return 1;
}

@Override
public int getMaximumArguments() {
return 1;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
String name = entry.getArgumentObject(queue, 0).toString();
Denizen2Sponge.rememberedInventories.remove(name);
}
}
@@ -0,0 +1,59 @@
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.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;

public class RememberInventoryCommand extends AbstractCommand {

// <--[command]
// @Name rememberinventory
// @Arguments <inventory> <name>
// @Short remembers an inventory until shutdown.
// @Updated 2017/06/11
// @Group Items
// @Minimum 2
// @Maximum 2
// @Save rememberinventory_inv (InventoryTag) returns the remembered inventory.
// @Description
// Remembers an inventory until shutdown.
// Does not persist shutdowns.
// Can take any inventory type.
// See also forgetinventory command.
// The inventory will be accessible as "shared/<name>".
// @Example
// # This example remembers the player's inventory as "Test", and stores it in definition "my_inv".
// - rememberinventory <player.inventory> "Test" --save my_inv
// -->

@Override
public String getName() {
return "rememberinventory";
}

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

@Override
public int getMinimumArguments() {
return 2;
}

@Override
public int getMaximumArguments() {
return 2;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
InventoryTag inv = InventoryTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
String name = entry.getArgumentObject(queue, 1).toString();
inv.remAs = name;
Denizen2Sponge.rememberedInventories.put(name, inv);
queue.commandStack.peek().setDefinition(entry.resName(queue, "rememberinventory_inv"), inv);
}
}
Expand Up @@ -6,6 +6,7 @@
import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.Function2;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.utilities.UtilLocation;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier;
Expand All @@ -31,6 +32,8 @@ public class InventoryTag extends AbstractTagObject {

private Inventory internal;

public String remAs = null;

public InventoryTag(Inventory player) {
internal = player;
}
Expand All @@ -50,6 +53,14 @@ public Inventory getInternal() {
// @Example "player/bob" might return "Bob".
// -->
handlers.put("name", (dat, obj) -> new TextTag(((InventoryTag) obj).internal.getName().get(Locale.ENGLISH)));
// @Name InventoryTag.size
// @Updated 2017/06/11
// @Group General Information
// @ReturnType IntegerTag
// @Returns the inventory's size (in slots).
// @Example "block/0,1,2,world" might return "36".
// -->
handlers.put("size", (dat, obj) -> new IntegerTag(((InventoryTag) obj).internal.size()));
}

public static InventoryTag getFor(Action<String> error, String text) {
Expand Down Expand Up @@ -77,6 +88,9 @@ else if (split.get(0).equals("block")) {
LocationTag lt = LocationTag.getFor(error, split.get(1));
return new InventoryTag(((TileEntityCarrier) lt.getInternal().toLocation().getTileEntity().get()).getInventory());
}
else if (split.get(0).equals("shared")) {
return Denizen2Sponge.rememberedInventories.get(split.get(1));
}
else {
error.run("Inventory type not known to the system: " + split.get(0));
return null;
Expand All @@ -103,11 +117,17 @@ public String toString() {
}

public String toString(boolean unks) {
if (remAs != null) {
return "shared/" + remAs;
}
if (internal instanceof PlayerInventory) {
return "player/" + ((PlayerInventory) internal).getCarrier().get().getUniqueId().toString();
}
else if (internal instanceof CarriedInventory) {
Object o = ((CarriedInventory) internal).getCarrier().get();
Object o = ((CarriedInventory) internal).getCarrier().orElse(null);
if (o == null) {
return "((UNKNOWN INVENTORY TYPE))";
}
if (o instanceof Entity) {
return "entity/" + ((Entity) o).getUniqueId().toString();
}
Expand All @@ -125,6 +145,10 @@ else if (o instanceof TileEntityCarrier) {
}
}

@Override
public String savable() {
return getTagTypeName() + saveMark() + toString(false);
}

@Override
public String getTagTypeName() {
Expand Down

0 comments on commit c029723

Please sign in to comment.