Skip to content

Commit

Permalink
Adds LootTableArgument example which was kindly provided by リク from t…
Browse files Browse the repository at this point in the history
…he Discord
  • Loading branch information
Jorel Ali committed Jan 23, 2021
1 parent a4f9265 commit 304b94b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 40 deletions.
27 changes: 19 additions & 8 deletions CommandAPI/commandapi-core/src/test/java/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -43,6 +44,7 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.loot.LootContext;
import org.bukkit.loot.LootTable;
import org.bukkit.loot.Lootable;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
Expand Down Expand Up @@ -518,14 +520,23 @@ public class Examples extends JavaPlugin {
/* ANCHOR: loottablearguments */
new CommandAPICommand("giveloottable")
.withArguments(new LootTableArgument("loottable"))
.executesPlayer((player, args) -> {
LootTable lootTable = (LootTable) args[0];

/* Some generated LootContext relating to the lootTable*/
LootContext context = new LootContext.Builder(player.getLocation()).build();

lootTable.fillInventory(player.getInventory(), new Random(), context);
})
.withArguments(new LocationArgument("location", LocationType.BLOCK_POSITION))
.executes((sender, args) -> {
LootTable lootTable = (LootTable) args[0];
Location location = (Location) args[1];

BlockState state = location.getBlock().getState();

// Check if the input block is a container (e.g. chest)
if(state instanceof Container && state instanceof Lootable) {
Container container = (Container) state;
Lootable lootable = (Lootable) container;

// Apply the loot table to the chest
lootable.setLootTable(lootTable);
container.update();
}
})
.register();
/* ANCHOR_END: loottablearguments */
}
Expand Down
35 changes: 22 additions & 13 deletions docs/5.3/loottableargument.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,32 @@ <h1><a class="header" href="#loottable-argument" id="loottable-argument">LootTab
<p><img src="./images/arguments/loottable.png" alt="" /></p>
<p>The <code>LootTableArgument</code> class can be used to get a Bukkit <code>LootTable</code> object.</p>
<div class="example">
<h3><a class="header" href="#example---filling-an-inventory-with-loot-table-contents" id="example---filling-an-inventory-with-loot-table-contents">Example - Filling an inventory with loot table contents</a></h3>
<h3><a class="header" href="#example---filling-a-chest-with-loot-table-contents" id="example---filling-a-chest-with-loot-table-contents">Example - Filling a chest with loot table contents</a></h3>
<p>Say we wanted to write a command that populates a chest with some loot table contents. For this example, we'll use the following command:</p>
<pre><code>/giveloottable &lt;loottable&gt; &lt;location&gt;
</code></pre>
<p>We ensure that the location provided is a container (such as a chest or shulkerbox) and then update the contents of that container:</p>
<pre><code class="language-java">new CommandAPICommand(&quot;giveloottable&quot;)
.withArguments(new LootTableArgument(&quot;loottable&quot;))
.executesPlayer((player, args) -&gt; {
LootTable lootTable = (LootTable) args[0];

/* Some generated LootContext relating to the lootTable*/
LootContext context = new LootContext.Builder(player.getLocation()).build();

lootTable.fillInventory(player.getInventory(), new Random(), context);
})
.withArguments(new LocationArgument(&quot;location&quot;, LocationType.BLOCK_POSITION))
.executes((sender, args) -&gt; {
LootTable lootTable = (LootTable) args[0];
Location location = (Location) args[1];

BlockState state = location.getBlock().getState();

// Check if the input block is a container (e.g. chest)
if(state instanceof Container &amp;&amp; state instanceof Lootable) {
Container container = (Container) state;
Lootable lootable = (Lootable) container;

// Apply the loot table to the chest
lootable.setLootTable(lootTable);
container.update();
}
})
.register();
</code></pre>
<blockquote>
<p><strong>Developer's Note:</strong></p>
<p>Honestly, I've not managed to get a successful example of using a <code>LootTable</code> in practice, due to being unable to generate a suitable <code>LootContext</code>. If you believe you can supply a suitable example for this page, feel free to send an example <a href="https://github.com/JorelAli/CommandAPI/issues/new/choose">on the CommandAPI issues page</a>.</p>
</blockquote>
</div>
</main>

Expand Down
35 changes: 22 additions & 13 deletions docs/5.3/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -2695,23 +2695,32 @@ <h3><a class="header" href="#example---giving-a-player-an-itemstack" id="example
<p><img src="./images/arguments/loottable.png" alt="" /></p>
<p>The <code>LootTableArgument</code> class can be used to get a Bukkit <code>LootTable</code> object.</p>
<div class="example">
<h3><a class="header" href="#example---filling-an-inventory-with-loot-table-contents" id="example---filling-an-inventory-with-loot-table-contents">Example - Filling an inventory with loot table contents</a></h3>
<h3><a class="header" href="#example---filling-a-chest-with-loot-table-contents" id="example---filling-a-chest-with-loot-table-contents">Example - Filling a chest with loot table contents</a></h3>
<p>Say we wanted to write a command that populates a chest with some loot table contents. For this example, we'll use the following command:</p>
<pre><code>/giveloottable &lt;loottable&gt; &lt;location&gt;
</code></pre>
<p>We ensure that the location provided is a container (such as a chest or shulkerbox) and then update the contents of that container:</p>
<pre><code class="language-java">new CommandAPICommand(&quot;giveloottable&quot;)
.withArguments(new LootTableArgument(&quot;loottable&quot;))
.executesPlayer((player, args) -&gt; {
LootTable lootTable = (LootTable) args[0];

/* Some generated LootContext relating to the lootTable*/
LootContext context = new LootContext.Builder(player.getLocation()).build();

lootTable.fillInventory(player.getInventory(), new Random(), context);
})
.withArguments(new LocationArgument(&quot;location&quot;, LocationType.BLOCK_POSITION))
.executes((sender, args) -&gt; {
LootTable lootTable = (LootTable) args[0];
Location location = (Location) args[1];

BlockState state = location.getBlock().getState();

// Check if the input block is a container (e.g. chest)
if(state instanceof Container &amp;&amp; state instanceof Lootable) {
Container container = (Container) state;
Lootable lootable = (Lootable) container;

// Apply the loot table to the chest
lootable.setLootTable(lootTable);
container.update();
}
})
.register();
</code></pre>
<blockquote>
<p><strong>Developer's Note:</strong></p>
<p>Honestly, I've not managed to get a successful example of using a <code>LootTable</code> in practice, due to being unable to generate a suitable <code>LootContext</code>. If you believe you can supply a suitable example for this page, feel free to send an example <a href="https://github.com/JorelAli/CommandAPI/issues/new/choose">on the CommandAPI issues page</a>.</p>
</blockquote>
</div><h1><a class="header" href="#mathoperation-arguments" id="mathoperation-arguments">MathOperation arguments</a></h1>
<p><img src="./images/arguments/mathop.png" alt="" /></p>
<p>The CommandAPI's <code>MathOperationArgument</code> is used to represent the Minecraft scoreboard arithmetic operation to alter scoreboard scores. Since there is no default representation in the Bukkit API, the CommandAPI provides the <code>MathOperation</code> class to represent each operation:</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/5.3/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/5.3/searchindex.json

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions docssrc/src/loottableargument.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ The `LootTableArgument` class can be used to get a Bukkit `LootTable` object.

<div class="example">

### Example - Filling an inventory with loot table contents
### Example - Filling a chest with loot table contents

Say we wanted to write a command that populates a chest with some loot table contents. For this example, we'll use the following command:

```
/giveloottable <loottable> <location>
```

We ensure that the location provided is a container (such as a chest or shulkerbox) and then update the contents of that container:

```java
{{#include ../../CommandAPI/commandapi-core/src/test/java/Examples.java:loottablearguments}}
```

> **Developer's Note:**
>
> Honestly, I've not managed to get a successful example of using a `LootTable` in practice, due to being unable to generate a suitable `LootContext`. If you believe you can supply a suitable example for this page, feel free to send an example [on the CommandAPI issues page](https://github.com/JorelAli/CommandAPI/issues/new/choose).

</div>

0 comments on commit 304b94b

Please sign in to comment.