This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add super simple start of an inventory tag
- Loading branch information
1 parent
e852ae6
commit 371c73e
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/denizenscript/denizen2sponge/tags/handlers/InventoryTagBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.denizenscript.denizen2sponge.tags.handlers; | ||
|
||
import com.denizenscript.denizen2core.tags.AbstractTagBase; | ||
import com.denizenscript.denizen2core.tags.AbstractTagObject; | ||
import com.denizenscript.denizen2core.tags.TagData; | ||
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag; | ||
import com.denizenscript.denizen2sponge.tags.objects.LocationTag; | ||
|
||
public class InventoryTagBase extends AbstractTagBase { | ||
|
||
// <--[tagbase] | ||
// @Base inventory[<InventoryTag>] | ||
// @Group Sponge Base Types | ||
// @ReturnType InventoryTag | ||
// @Returns the input as an InventoryTag. | ||
// --> | ||
|
||
@Override | ||
public String getName() { | ||
return "inventory"; | ||
} | ||
|
||
@Override | ||
public AbstractTagObject handle(TagData data) { | ||
return InventoryTag.getFor(data.error, data.getNextModifier()).handle(data.shrink()); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/main/java/com/denizenscript/denizen2sponge/tags/objects/InventoryTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.denizenscript.denizen2sponge.tags.objects; | ||
|
||
import com.denizenscript.denizen2core.tags.AbstractTagObject; | ||
import com.denizenscript.denizen2core.tags.TagData; | ||
import com.denizenscript.denizen2core.tags.objects.*; | ||
import com.denizenscript.denizen2core.utilities.Action; | ||
import com.denizenscript.denizen2core.utilities.CoreUtilities; | ||
import com.denizenscript.denizen2core.utilities.Function2; | ||
import com.denizenscript.denizen2sponge.utilities.UtilLocation; | ||
import org.spongepowered.api.Sponge; | ||
import org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier; | ||
import org.spongepowered.api.entity.Entity; | ||
import org.spongepowered.api.entity.living.player.Player; | ||
import org.spongepowered.api.item.inventory.Carrier; | ||
import org.spongepowered.api.item.inventory.Inventory; | ||
import org.spongepowered.api.item.inventory.entity.PlayerInventory; | ||
import org.spongepowered.api.item.inventory.type.CarriedInventory; | ||
import org.spongepowered.api.world.LocatableBlock; | ||
import org.spongepowered.api.world.World; | ||
|
||
import java.util.*; | ||
|
||
public class InventoryTag extends AbstractTagObject { | ||
|
||
// <--[object] | ||
// @Type InventoryTag | ||
// @SubType TextTag | ||
// @Group Items | ||
// @Description Represents an inventory in the world. | ||
// --> | ||
|
||
private Inventory internal; | ||
|
||
public InventoryTag(Inventory player) { | ||
internal = player; | ||
} | ||
|
||
public Inventory getInternal() { | ||
return internal; | ||
} | ||
|
||
public final static HashMap<String, Function2<TagData, AbstractTagObject, AbstractTagObject>> handlers = new HashMap<>(); | ||
|
||
static { | ||
// @Name InventoryTag.name | ||
// @Updated 2017/06/10 | ||
// @Group General Information | ||
// @ReturnType TextTag | ||
// @Returns the inventory's name (in English). | ||
// @Example "player/bob" might return "Bob". | ||
// --> | ||
handlers.put("name", (dat, obj) -> new TextTag(((InventoryTag) obj).internal.getName().get(Locale.ENGLISH))); | ||
} | ||
|
||
public static InventoryTag getFor(Action<String> error, String text) { | ||
List<String> split = CoreUtilities.split(text, '/'); | ||
if (split.get(0).equals("player")) { | ||
Optional<Player> oplayer = Sponge.getServer().getPlayer(UUID.fromString(split.get(1))); | ||
if (!oplayer.isPresent()) { | ||
error.run("Invalid PlayerTag UUID input!"); | ||
return null; | ||
} | ||
return new InventoryTag(oplayer.get().getInventory()); | ||
} | ||
else if (split.get(0).equals("entity")) { | ||
UUID id = UUID.fromString(split.get(1)); | ||
for (World world : Sponge.getServer().getWorlds()) { | ||
Optional<Entity> e = world.getEntity(id); | ||
if (e.isPresent() && e.get() instanceof Carrier) { | ||
return new InventoryTag(((Carrier) e.get()).getInventory()); | ||
} | ||
} | ||
error.run("Invalid EntityTag UUID input!"); | ||
return null; | ||
} | ||
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 { | ||
error.run("Inventory type not known to the system: " + split.get(0)); | ||
return null; | ||
} | ||
} | ||
|
||
public static InventoryTag getFor(Action<String> error, AbstractTagObject text) { | ||
return (text instanceof InventoryTag) ? (InventoryTag) text : getFor(error, text.toString()); | ||
} | ||
|
||
@Override | ||
public HashMap<String, Function2<TagData, AbstractTagObject, AbstractTagObject>> getHandlers() { | ||
return handlers; | ||
} | ||
|
||
@Override | ||
public AbstractTagObject handleElseCase(TagData data) { | ||
return new TextTag(toString(false)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toString(true); | ||
} | ||
|
||
public String toString(boolean unks) { | ||
if (internal instanceof PlayerInventory) { | ||
return "player/" + ((PlayerInventory) internal).getCarrier().get().getUniqueId().toString(); | ||
} | ||
else if (internal instanceof CarriedInventory) { | ||
Object o = ((CarriedInventory) internal).getCarrier().get(); | ||
if (o instanceof Entity) { | ||
return "entity/" + ((Entity) o).getUniqueId().toString(); | ||
} | ||
else if (o instanceof TileEntityCarrier) { | ||
LocatableBlock lb = ((TileEntityCarrier) o).getLocatableBlock(); | ||
return "block/" + new LocationTag(new UtilLocation(lb.getPosition(), lb.getWorld())); | ||
} | ||
} | ||
if (!unks) { | ||
// TODO: Handle all inventory types somehow??? | ||
throw new RuntimeException("Inventory type not known to the system!"); | ||
} | ||
else { | ||
return "((UNKNOWN INVENTORY TYPE))"; | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public String getTagTypeName() { | ||
return "InventoryTag"; | ||
} | ||
@Override | ||
public String debug() { | ||
return toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters