Skip to content

Commit

Permalink
feat(tsl): add SHUFFLE action
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoodie committed Sep 10, 2019
1 parent c2b85e3 commit 321a037
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=1.3.3
mod_version=1.3.4

minecraft_version=1.14.4
forge_version=28.0.83
Expand Down
Expand Up @@ -56,7 +56,7 @@ protected void performAction(ServerPlayerEntity player, EventArguments args) {
CommandSource commandSource = player.getCommandSource()
.withPermissionLevel(9999).withFeedbackDisabled();
player.getServer().getCommandManager().handleCommand(commandSource,
"/playsound minecraft:entity.item.break master @p");
"/playsound minecraft:entity.item.break master @s");
player.getServer().getCommandManager().handleCommand(commandSource,
"/particle minecraft:smoke ~ ~ ~ 2 2 2 0.1 400");
}
Expand Down
Expand Up @@ -7,7 +7,6 @@

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public abstract class ItemSelectiveAction extends TSLAction {

Expand Down Expand Up @@ -73,7 +72,7 @@ protected void parseFrom(List<String> words) throws TSLSyntaxError {
filling.add(word);
}

String inventoryName = rightHand.stream().collect(Collectors.joining(" "));
String inventoryName = String.join(" ", rightHand);

// Parse inventory name
if (inventoryName.equalsIgnoreCase("inventory"))
Expand All @@ -86,7 +85,7 @@ else if (inventoryName.equalsIgnoreCase("armors"))
// Parse index - with two words (e.g "slot 2")
if (leftHand.size() == 2) {
if (leftHand.get(0).equalsIgnoreCase("slot")) {
parseSlot(leftHand);
parseInventoryIndex(leftHand);

} else {
throw new TSLSyntaxError("Unknown inventory selector -> %s", leftHand);
Expand All @@ -109,7 +108,7 @@ else if (inventoryName.equalsIgnoreCase("armors"))
}
}

protected void parseSlot(List<String> leftHand) throws TSLSyntaxError {
protected void parseInventoryIndex(List<String> leftHand) throws TSLSyntaxError {
try {
inventoryIndex = Integer.parseInt(leftHand.get(1));
} catch (Exception e) {
Expand Down
@@ -0,0 +1,109 @@
package net.programmer.igoodie.twitchspawn.tslanguage.action;

import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.programmer.igoodie.twitchspawn.tslanguage.EventArguments;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLParser;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLSyntaxError;

import java.util.List;

public class ShuffleAction extends ItemSelectiveAction {

private int firstIndex;
private int lastIndex;

public ShuffleAction(List<String> words) throws TSLSyntaxError {
this.message = TSLParser.parseMessage(words);
List<String> actionWords = actionPart(words);

if (actionWords.size() == 1)
parseInventoryName(actionWords);

else if (actionWords.size() == 3)
parseSlot(actionWords);

else throw new TSLSyntaxError("Invalid length of words: " + actionWords);
}

private void parseInventoryName(List<String> actionWords) throws TSLSyntaxError {
String inventoryName = actionWords.get(0);

if (inventoryName.equalsIgnoreCase("inventory")) {
this.inventoryType = InventoryType.MAIN_INVENTORY;
this.firstIndex = 0;
this.lastIndex = inventoryType.capacity - 1;

} else if (inventoryName.equalsIgnoreCase("armors")) {
this.inventoryType = InventoryType.ARMOR_INVENTORY;
this.firstIndex = 0;
this.lastIndex = inventoryType.capacity - 1;

} else if (inventoryName.equalsIgnoreCase("hotbar")) {
this.inventoryType = InventoryType.MAIN_INVENTORY;
this.firstIndex = 0;
this.lastIndex = 8;

} else {
throw new TSLSyntaxError("Unknown inventory name -> %s", inventoryName);
}
}

private void parseSlot(List<String> actionWords) throws TSLSyntaxError {
if (!actionWords.get(0).equalsIgnoreCase("slot"))
throw new TSLSyntaxError("Unknown inventory name -> %s", actionWords.get(0));

this.inventoryType = InventoryType.MAIN_INVENTORY;

try { // Parse first index
this.firstIndex = Integer.parseInt(actionWords.get(1));

} catch (NumberFormatException e) {
throw new TSLSyntaxError("Malformed integer -> %s", actionWords.get(1));
}

try { // Parse last index
this.lastIndex = Integer.parseInt(actionWords.get(2));

} catch (NumberFormatException e) {
throw new TSLSyntaxError("Malformed integer -> %s", actionWords.get(2));
}

// Range check
if (this.firstIndex < 0)
throw new TSLSyntaxError("First index cannot be negative");

if (this.lastIndex >= inventoryType.capacity)
throw new TSLSyntaxError("Last index cannot be bigger than %d", inventoryType.capacity - 1);

if (this.firstIndex > this.lastIndex) {
throw new TSLSyntaxError("First index cannot be greater than the last index.");
}

}

@Override
protected void performAction(ServerPlayerEntity player, EventArguments args) {
shuffle(getInventory(player, inventoryType), this.firstIndex, this.lastIndex);

CommandSource commandSource = player.getCommandSource()
.withPermissionLevel(9999).withFeedbackDisabled();
player.getServer().getCommandManager().handleCommand(commandSource,
"/playsound minecraft:block.conduit.activate master @s");
player.getServer().getCommandManager().handleCommand(commandSource,
"/particle minecraft:bubble_pop ~ ~ ~ 2 2 2 0.0001 400");
}

private void shuffle(NonNullList<ItemStack> inventory, int firstIndex, int lastIndex) {
for (int i = lastIndex; i > firstIndex; i--) {
int randomIndex = (int) (Math.random() * (i - firstIndex + 1)) + firstIndex;

ItemStack temp = inventory.get(i);
inventory.set(i, inventory.get(randomIndex));
inventory.set(randomIndex, temp);
}
}

}
Expand Up @@ -71,7 +71,7 @@ protected void performAction(ServerPlayerEntity player, EventArguments args) {
CommandSource commandSource = player.getCommandSource()
.withPermissionLevel(9999).withFeedbackDisabled();
player.getServer().getCommandManager().handleCommand(commandSource,
"/playsound minecraft:entity.ender_pearl.throw master @p");
"/playsound minecraft:entity.ender_pearl.throw master @s");
player.getServer().getCommandManager().handleCommand(commandSource,
"/particle minecraft:witch ~ ~ ~ 2 2 2 0.1 400");
}
Expand Down
Expand Up @@ -9,6 +9,7 @@ public enum TSLActionKeyword {
THROW(true, ThrowAction.class),
CLEAR(true, ClearAction.class),
EXECUTE(true, ExecuteAction.class),
SHUFFLE(true, ShuffleAction.class),
NOTHING(true, NothingAction.class),

EITHER(false, EitherAction.class),
Expand Down

0 comments on commit 321a037

Please sign in to comment.