Skip to content

Commit

Permalink
Merge branch 'inv-log' into v3-cn
Browse files Browse the repository at this point in the history
# Conflicts:
#	Prism/build.gradle
#	Prism/src/main/java/network/darkhelmet/prism/commands/PrismCommands.java
#	Prism/src/main/resources/languages/message.properties
  • Loading branch information
Rothes committed Dec 24, 2023
2 parents 4f73b38 + ba94d66 commit 3faa370
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 6 deletions.
5 changes: 5 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 Down Expand Up @@ -58,6 +61,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 'org.bstats:bstats-bukkit:3.0.0'
implementation project(':Prism-Api')
}
Expand All @@ -84,6 +88,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'
relocate 'org.bstats', 'io.github.rothes.prismcn.lib.org.bstats'

minimize()
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 @@ -13,6 +17,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 @@ -79,7 +84,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 @@ -95,7 +100,7 @@ private TextComponent getMainMessage(ActionType action, String format1) {
.colorIfAbsent(NamedTextColor.WHITE)
.hoverEvent(HoverEvent.showText(Component.text("点击传送")
.color(NamedTextColor.DARK_AQUA)))
.clickEvent(ClickEvent.runCommand("/pr tp " + index))
.clickEvent(ClickEvent.runCommand("/prism tp " + index))
.build();

}
Expand Down Expand Up @@ -169,9 +174,17 @@ private TextComponent.Builder getActor(ActionTypeImpl action, TextColor highligh
target = "细雪";
}
}
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,7 +74,8 @@ 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(new String[]{"tp", "传送"}, "prism.tp").setMinArgs(1).setHandler(new TeleportCommand(prism));
addSub(new String[]{"ex", "灭火"}, "prism.extinguish").setHandler(new ExtinguishCommand(prism));
addSub(new String[]{"give", "给予"}, "prism.give").setMinArgs(1).setHandler(new GiveCommand(prism));
addSub(new String[]{"ex", "灭火"}, "prism.extinguish").setHandler(new ExtinguishCommand(prism));
addSub(new String[]{"drain" ,"排水"}, "prism.drain").setHandler(new DrainCommand(prism));
addSub(new String[]{"preview", "pv", "预览"}, "prism.preview").setMinArgs(1).setHandler(new PreviewCommand(prism));
addSub(new String[]{"report", "rp", "报告"}, "prism.report").allowConsole().setHandler(new ReportCommand(prism));
Expand Down
1 change: 1 addition & 0 deletions Prism/src/main/resources/languages/message.properties
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ help-rollback=回滚变化.
help-rollback-preview=预览一次回滚操作.
help-rollback-wand=切换回滚魔棒.
help-teleport=传送到一个查询结果.
help-give=获取查询结果中的物品.
help-undo=撤销一个排水操作.
help-wand-mode-personal=设置您个人的魔棒模式.
help-wand-mode-toItem=设置您个人的魔棒 物品/方块 材料.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ help-rollback=Rollback changes.
help-rollback-preview=Preview a rollback.
help-rollback-wand=Toggle the rollback wand.
help-teleport=Teleport to a lookup result.
help-give=Give the itemstack from a lookup result.
help-undo=Undo a drain.
help-wand-mode-personal=Set your personal wand mode.
help-wand-mode-toItem=Set your personal wand item/block Material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ help-rollback=Rollback changes.
help-rollback-preview=Preview a rollback.
help-rollback-wand=Toggle the rollback wand.
help-teleport=Teleport to a lookup result.
help-give=Give the itemstack from a lookup result.
help-undo=Undo a drain.
help-wand-mode-personal=Set your personal wand mode.
help-wand-mode-toItem=Set your personal wand item/block Material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ help-rollback=回滚变化.
help-rollback-preview=预览一次回滚操作.
help-rollback-wand=切换回滚魔棒.
help-teleport=传送到一个查询结果.
help-give=获取查询结果中的物品.
help-undo=撤销一个排水操作.
help-wand-mode-personal=设置您个人的魔棒模式.
help-wand-mode-toItem=设置您个人的魔棒 物品/方块 材料.
Expand Down

0 comments on commit 3faa370

Please sign in to comment.