Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NMS to check inventory moves are proceeded; Get Item by clicking message #72

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions Prism/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ repositories {

// Adventure snapshot
maven { url = 'https://s01.oss.sonatype.org/content/repositories/snapshots/' }

// NBTAPI
maven { url = 'https://repo.codemc.org/repository/maven-public/' }
}

group = project.property("group")
Expand All @@ -40,6 +43,7 @@ processResources {

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT'
// compileOnly 'org.spigotmc:spigot:1.20.3-R0.1-SNAPSHOT:remapped-mojang' // We actually don't use it in project, all reflect
compileOnly 'com.sk89q.worldedit:worldedit-core:7.2.0-SNAPSHOT'
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.2.0-SNAPSHOT'
implementation 'org.apache.commons:commons-lang3:3.12.0'
Expand All @@ -50,6 +54,7 @@ dependencies {
implementation 'net.kyori:adventure-platform-bukkit:4.3.2-SNAPSHOT'
implementation 'net.kyori:adventure-text-serializer-plain:4.15.0'
implementation 'org.kitteh:paste-gg-api:1.0.0-SNAPSHOT'
implementation 'de.tr7zw:item-nbt-api:2.12.2'
implementation project(':Prism-Api')
}

Expand All @@ -73,6 +78,7 @@ shadowJar {
relocate 'net.kyori', 'network.darkhelmet.prism.kyori'
relocate 'org.kitteh', 'network.darkhelmet.prism.kitteh'
relocate 'org.apache', 'network.darkhelmet.prism.apache'
relocate 'de.tr7zw.changeme.nbtapi', 'network.darkhelmet.prism.de.tr7zw.nbtapi'

minimize()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public FileConfiguration getConfig() {

// Tracker configs
config.addDefault("prism.track-player-ip-on-join", false);
config.addDefault("prism.track-hopper-item-events", false);
config.addDefault("prism.track-hopper-item-events", true);

final Collection<String> doNotTrackCommand = new ArrayList<>();
doNotTrackCommand.add("vanish");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package network.darkhelmet.prism.actionlibs;

import de.tr7zw.changeme.nbtapi.NBTItem;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import network.darkhelmet.prism.actions.ItemStackAction;
import network.darkhelmet.prism.api.actions.ActionType;
import network.darkhelmet.prism.api.actions.Handler;
import network.darkhelmet.prism.utils.block.Utilities;
Expand All @@ -12,6 +16,7 @@
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.regex.Pattern;

Expand Down Expand Up @@ -78,7 +83,7 @@ private TextComponent getMainMessage(ActionType action, String format1) {
.replaceFirstText(Pattern.compile("<location>"),
builder -> Component.text().content(getFormattedLocation()))
.replaceFirstText(Pattern.compile("<count>"),
builder -> Component.text().append(getCount()))
builder -> enhanceByAction(Component.text().append(getCount())))
.replaceFirstText(Pattern.compile("<actionType>"),
builder -> Component.text()
.content("(a:" + action.getShortName() + ")")
Expand All @@ -91,7 +96,7 @@ private TextComponent getMainMessage(ActionType action, String format1) {
.append(result)
.hoverEvent(HoverEvent.showText(Component.text("Click to teleport")
.color(NamedTextColor.DARK_AQUA)))
.clickEvent(ClickEvent.runCommand("/pr tp " + index))
.clickEvent(ClickEvent.runCommand("/prism tp " + index))
.build();

}
Expand Down Expand Up @@ -165,9 +170,17 @@ private TextComponent.Builder getActor(ActionTypeImpl action, TextColor highligh
target = "powder snow ";
}
}
return Component.text()
.content(target)
.color(highlight);
return enhanceByAction(Component.text().content(target).color(highlight));
}

private TextComponent.Builder enhanceByAction(TextComponent.Builder msg) {
if (handler instanceof ItemStackAction) {
ItemStack item = ((ItemStackAction) handler).getItem();
return msg.hoverEvent(HoverEvent.showItem(Key.key(item.getType().getKey().getKey()),
item.getAmount(), BinaryTagHolder.binaryTagHolder(new NBTItem(item).toString())))
.clickEvent(ClickEvent.runCommand("/prism give " + index));
}
return msg;
}

private TextComponent getCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class QueryResult implements Result {
private int perPage = 5;
private int totalPages = 0;
private long lastTeleportIndex = 0;
private long lastGiveIndex = 0;

/**
* Create a Query Result.
Expand Down Expand Up @@ -103,6 +104,14 @@ public void setLastTeleportIndex(long index) {
this.lastTeleportIndex = index;
}

public long getLastGiveIndex() {
return lastGiveIndex;
}

public void setLastGiveIndex(long lastGiveIndex) {
this.lastGiveIndex = lastGiveIndex;
}

public int getIndexOfFirstResult() {
final int index = (page * perPage) - perPage;
return index + 1;
Expand Down
170 changes: 170 additions & 0 deletions Prism/src/main/java/network/darkhelmet/prism/commands/GiveCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package network.darkhelmet.prism.commands;

import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import network.darkhelmet.prism.Il8nHelper;
import network.darkhelmet.prism.Prism;
import network.darkhelmet.prism.actionlibs.ActionsQuery;
import network.darkhelmet.prism.actionlibs.QueryParameters;
import network.darkhelmet.prism.actionlibs.QueryResult;
import network.darkhelmet.prism.actions.ItemStackAction;
import network.darkhelmet.prism.api.actions.Handler;
import network.darkhelmet.prism.commandlibs.CallInfo;
import network.darkhelmet.prism.commandlibs.SubHandler;
import network.darkhelmet.prism.utils.TypeUtils;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;

public class GiveCommand implements SubHandler {

private final Prism plugin;

/**
* GiveCommand.
*
* @param plugin Prism.
*/
GiveCommand(Prism plugin) {
this.plugin = plugin;
}

/**
* Handle the command.
*/
@Override
public void handle(CallInfo call) {

// Is there anything even stored to paginate?
String keyName = "console";
if (call.getSender() instanceof Player) {
keyName = call.getSender().getName();
}
if (!plugin.cachedQueries.containsKey(keyName) && !call.getArg(1).contains("id:")) {
Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError(
"There's no saved query to use results from. Maybe they expired? Try your lookup again."));
return;
}

// Parse the incoming ident
String ident = call.getArg(1);
if (ident.contains("id:")) {
ident = ident.replace("id:", "");
}

// Determine result index to give to - either an id, or the next/previous
// id
long recordId;
if (ident.equals("next") || ident.equals("prev")) {
// Get stored results
final QueryResult results = plugin.cachedQueries.get(keyName);
recordId = results.getLastGiveIndex();
recordId = (recordId == 0 ? 1 : recordId);
if (recordId > 0) {
long tempId = recordId;
if (ident.equals("next")) {
while (results.getActionResults().size() > tempId) {
tempId++;
if (results.getActionResults().get((int) tempId - 1) instanceof ItemStackAction) {
recordId = tempId;
break;
}
}
} else {
while (tempId > 1) {
tempId--;
if (results.getActionResults().get((int) tempId - 1) instanceof ItemStackAction) {
recordId = tempId;
break;
}
}
}
}
} else {
if (!TypeUtils.isNumeric(ident)) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger
.playerError("You must provide a numeric result number or record ID to give to."));
return;
}
recordId = Integer.parseInt(ident);
if (recordId <= 0) {
Prism.messenger.sendMessage(call.getPlayer(),
Prism.messenger.playerError("Result number or record ID must be greater than zero."));
return;
}
}

// If a record id provided, re-query the database
final Handler targetAction;
if (call.getArg(1).contains("id:")) {

// Build params
final QueryParameters params = new QueryParameters();
params.setWorld(call.getPlayer().getWorld().getName());
params.setId(recordId);

// Query
final ActionsQuery aq = new ActionsQuery(plugin);
final QueryResult results = aq.lookup(params, call.getPlayer());
if (results.getActionResults().isEmpty()) {
Prism.messenger.sendMessage(call.getPlayer(),
Prism.messenger.playerError("No records exists with this ID."));
return;
}

// Get the first result
targetAction = results.getActionResults().get(0);

} else {

// Get stored results
final QueryResult results = plugin.cachedQueries.get(keyName);

if (recordId > results.getActionResults().size()) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(
"No records exists at this index. Did you mean /pr give id:" + recordId + " instead?"));
return;
}

final int key = (int) (recordId - 1);

// Get the result index specified
targetAction = results.getActionResults().get(key);

// Refresh the query time and replace
results.setQueryTime();
results.setLastGiveIndex(recordId);
plugin.cachedQueries.replace(keyName, results);
}

Player player = call.getPlayer();
if (!(targetAction instanceof ItemStackAction)) {
Prism.messenger.sendMessage(player, Prism.messenger.playerError("The record at this index is not a ItemStackAction."));
return;
}
ItemStack item = ((ItemStackAction) targetAction).getItem();
item.setAmount(item.getAmount() * targetAction.getAggregateCount());
player.getInventory().addItem(item);
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.5f, 1);
}

@Override
public List<String> handleComplete(CallInfo call) {
return null;
}

@Override
public String[] getHelp() {
return new String[]{Il8nHelper.getRawMessage("help-give")};
}

@Override
public String getRef() {
return "/lookup.html#giving";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public String getRef() {
addSub(new String[]{"setmy"}, new String[]{"prism.setmy.wand"}).setHandler(new SetmyCommand(prism));
addSub(new String[]{"resetmy"}, new String[]{"prism.setmy.wand"}).setHandler(new ResetmyCommand(prism));
addSub("tp", "prism.tp").setMinArgs(1).setHandler(new TeleportCommand(prism));
addSub("give", "prism.give").setMinArgs(1).setHandler(new GiveCommand(prism));
addSub("ex", "prism.extinguish").setHandler(new ExtinguishCommand(prism));
addSub("drain", "prism.drain").setHandler(new DrainCommand(prism));
addSub(new String[]{"preview", "pv"}, "prism.preview").setMinArgs(1).setHandler(new PreviewCommand(prism));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import network.darkhelmet.prism.actionlibs.ActionFactory;
import network.darkhelmet.prism.actionlibs.RecordingQueue;
import network.darkhelmet.prism.api.actions.Handler;
import network.darkhelmet.prism.utils.NmsUtils;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -654,7 +655,7 @@ public void onInventoryClick(final InventoryClickEvent event) {
for (int i = 0; i < length; ++i) {
ItemStack is = contents[i];

if (slotItem.isSimilar(is)) {
if (slotItem.isSimilar(is) && NmsUtils.canAcceptPlaceQuick(event.getView(), slotItem, i)) {
amount = recordDeductTransfer(INSERT,stackSize - is.getAmount(),amount,slotItem,
containerLoc,i,player,event);
if (amount <= 0) {
Expand All @@ -668,7 +669,7 @@ public void onInventoryClick(final InventoryClickEvent event) {
for (int i = 0; i < length; ++i) {
ItemStack is = contents[i];

if (is == null || is.getType() == Material.AIR) {
if ((is == null || is.getType() == Material.AIR) && NmsUtils.canAcceptPlaceQuick(event.getView(), slotItem, i)) {
amount = recordDeductTransfer(INSERT,stackSize,amount,slotItem,
containerLoc,i,player,event);
if (amount <= 0) {
Expand Down
Loading