Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Inventory command. Will expand soon.
- Loading branch information
1 parent
455756e
commit 27f9506
Showing
7 changed files
with
236 additions
and
38 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
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
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
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
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
122 changes: 122 additions & 0 deletions
122
src/main/java/net/aufdemrand/denizen/scripts/commands/item/InventoryCommand.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,122 @@ | ||
package net.aufdemrand.denizen.scripts.commands.item; | ||
|
||
import net.aufdemrand.denizen.exceptions.CommandExecutionException; | ||
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; | ||
import net.aufdemrand.denizen.objects.aH; | ||
import net.aufdemrand.denizen.objects.dEntity; | ||
import net.aufdemrand.denizen.objects.dInventory; | ||
import net.aufdemrand.denizen.objects.dLocation; | ||
import net.aufdemrand.denizen.scripts.ScriptEntry; | ||
import net.aufdemrand.denizen.scripts.commands.AbstractCommand; | ||
import net.aufdemrand.denizen.utilities.debugging.dB.Messages; | ||
|
||
import org.bukkit.block.BlockState; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.InventoryHolder; | ||
|
||
/** | ||
* Lets you store and edit inventories. | ||
* | ||
* @author David Cernat | ||
*/ | ||
|
||
public class InventoryCommand extends AbstractCommand { | ||
|
||
@Override | ||
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { | ||
|
||
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { | ||
|
||
if (!scriptEntry.hasObject("originEntity") && | ||
!scriptEntry.hasObject("originLocation") && | ||
arg.matchesPrefix("origin, o, source, s")) { | ||
|
||
// Is entity | ||
if (arg.matchesArgumentType(dEntity.class)) | ||
scriptEntry.addObject("originEntity", arg.asType(dEntity.class).setPrefix("entity")); | ||
// Is location | ||
else if (arg.matchesArgumentType(dLocation.class)) | ||
scriptEntry.addObject("originLocation", arg.asType(dLocation.class).setPrefix("location")); | ||
} | ||
|
||
else if (!scriptEntry.hasObject("destinationEntity") && | ||
!scriptEntry.hasObject("destinationLocation") && | ||
arg.matchesPrefix("destination, d")) { | ||
|
||
// Is entity | ||
if (arg.matchesArgumentType(dEntity.class)) | ||
scriptEntry.addObject("destinationEntity", arg.asType(dEntity.class).setPrefix("entity")); | ||
// Is location | ||
else if (arg.matchesArgumentType(dLocation.class)) | ||
scriptEntry.addObject("destinationLocation", arg.asType(dLocation.class).setPrefix("location")); | ||
} | ||
} | ||
|
||
// Check to make sure required arguments have been filled | ||
|
||
if (!scriptEntry.hasObject("originEntity") && | ||
!scriptEntry.hasObject("originLocation")) | ||
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN"); | ||
|
||
if (!scriptEntry.hasObject("destinationEntity") && | ||
!scriptEntry.hasObject("destinationLocation")) | ||
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "DESTINATION"); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException { | ||
// Get objects | ||
dEntity originEntity = (dEntity) scriptEntry.getObject("originEntity"); | ||
dLocation originLocation = (dLocation) scriptEntry.getObject("originLocation"); | ||
|
||
dEntity destinationEntity = (dEntity) scriptEntry.getObject("destinationEntity"); | ||
dLocation destinationLocation = (dLocation) scriptEntry.getObject("destinationLocation"); | ||
|
||
dInventory origin = null; | ||
dInventory destination = null; | ||
|
||
if (originLocation != null) { | ||
|
||
BlockState blockState = originLocation.getBlock().getState(); | ||
|
||
if (blockState instanceof InventoryHolder) { | ||
|
||
origin = new dInventory(((InventoryHolder) blockState).getInventory()); | ||
} | ||
} | ||
else if (originEntity != null) { | ||
|
||
LivingEntity entity = originEntity.getLivingEntity(); | ||
|
||
if (entity instanceof Player) { | ||
|
||
origin = new dInventory(((Player) entity).getInventory()); | ||
} | ||
} | ||
|
||
if (destinationLocation != null) { | ||
|
||
BlockState blockState = destinationLocation.getBlock().getState(); | ||
|
||
if (blockState instanceof InventoryHolder) { | ||
|
||
destination = new dInventory(((InventoryHolder) blockState).getInventory()); | ||
} | ||
} | ||
else if (destinationEntity != null) { | ||
|
||
LivingEntity entity = destinationEntity.getLivingEntity(); | ||
|
||
if (entity instanceof Player) { | ||
|
||
destination = new dInventory(((Player) entity).getInventory()); | ||
} | ||
} | ||
|
||
origin.copy(destination); | ||
|
||
} | ||
} |
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