Skip to content

Commit

Permalink
version 2.9.15
Browse files Browse the repository at this point in the history
added custom inventory editor
  • Loading branch information
EriolEandur committed Dec 8, 2024
1 parent d88e3d8 commit 0be7d59
Show file tree
Hide file tree
Showing 27 changed files with 289 additions and 197 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mcmiddleearth</groupId>
<artifactId>MCME-Architect</artifactId>
<version>2.9.15-snapshot</version>
<version>2.9.15</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.mcmiddleearth.connect.log.Log;
import com.mcmiddleearth.util.DevUtil;
import com.mcmiddleearth.util.ResourceUtil;
import com.viaversion.viaversion.api.Via;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -213,6 +214,9 @@ private static ConfigurationSection getConfigSection(String rp, Player player) {
RpPlayerData data;
if (player != null) {
data = getPlayerData(player);
if(data.getProtocolVersion()==0) {
data.setProtocolVersion(Via.getAPI().getPlayerProtocolVersion(player.getUniqueId()).getVersion());
}
} else {
data = new RpPlayerData();
}
Expand Down Expand Up @@ -344,6 +348,7 @@ public static boolean setRp(String rpName, Player player, boolean force) {
RpPlayerData data = getPlayerData(player);
if(url!=null && data!=null && !url.equals("") && (force || !url.equals(data.getCurrentRpUrl()))) {
data.setCurrentRpUrl(url);
//Logger.getGlobal().info("Sending to "+player.getName()+"("+getPlayerData(player).getProtocolVersion()+") RP: "+url);
player.setResourcePack(url, getSHA(rpName, player));
savePlayerData(player);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class RpPluginMessageListener implements PluginMessageListener {
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] bytes) {
//Logger.getGlobal().info("Sodium client detected: "+player.getName());

Logger.getGlobal().info("Received message from player " + player.getName() + " on channel "
+ channel + " with data " + Arrays.toString(bytes));
//Logger.getGlobal().info("Received message from player " + player.getName() + " on channel "
// + channel + " with data " + Arrays.toString(bytes));
try (var dataStream = new DataInputStream(new ByteArrayInputStream(bytes))) {
int stringLength = readVarInt(dataStream);
String jsonString = new String(dataStream.readNBytes(stringLength));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ void onInventoryEdit(final InventoryClickEvent event) {
if(event.getCurrentItem() == null) {
CustomInventoryEditor.addBlock(player, rpName, category, state, event.getRawSlot());
} else {
CustomInventoryEditor.editBlock(player, rpName, category, state, event.getCurrentItem());
CustomInventoryEditor.editBlock(player, category, state, event.getRawSlot(), event.getCurrentItem());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.inventory.ItemStack;

import java.util.*;
import java.util.logging.Logger;

/**
*
Expand Down Expand Up @@ -85,7 +86,10 @@ public boolean isVisible(Player player) {
}

public ItemStack getItem(String id) {
return items.stream().filter(item -> SpecialBlockInventoryData.getSpecialBlockId(item).equals(id)).findAny().orElse(null);
return items.stream().filter(item -> {
String blockId = SpecialBlockInventoryData.getSpecialBlockId(item);
return blockId != null && blockId.equals(id);
}).findAny().orElse(null);
}

public boolean isPublic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.inventory.meta.ItemMeta;

import java.util.*;
import java.util.logging.Logger;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,14 @@ public boolean isEmpty() {

public ItemStack getItem(String id) {
//Logger.getGlobal().info("search getItem: "+id);
return items.stream().filter(item -> item.getItemMeta().getLore().get(1).equals(id)).findFirst().orElse(null);
return items.stream().filter(item -> {
if(item.hasItemMeta() && item.getItemMeta().hasLore()) {
return item.getItemMeta().getLore().get(1).equals(id);
// } else {
// Logger.getGlobal().warning("Invalid item in search inventory: "+item);
}
return false;
}).findFirst().orElse(null);
}

public Set<NamespacedKey> getRecipeKeys() {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mcmiddleearth.architect.specialBlockHandling.customInventories.editor.prompt.add;

import com.mcmiddleearth.architect.ArchitectPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
Expand All @@ -11,6 +13,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -32,7 +35,12 @@ public BlockDataPrompt(String... blockStateKeys) {

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Left-click a block to use for blockData"+blockStateKeys[0]+".";
return "Left-click a block to use for blockData"+blockStateKeys[0]+". You may also type in valid block data.";
}

@Override
protected @Nullable String getFailedValidationText(@NotNull ConversationContext context, @NotNull String invalidInput) {
return "You need to left-click a block or type in valid block data!";
}

@Override
Expand All @@ -42,39 +50,46 @@ public boolean blocksForInput(@NotNull ConversationContext conversationContext)
Bukkit.getPluginManager().registerEvents(this, Objects.requireNonNull(conversationContext.getPlugin()));
listenerRegistered = true;
}
Logger.getGlobal().info("blocksForInput: "+true);
return true;
}

@Override
protected boolean isInputValid(@NotNull ConversationContext conversationContext, @NotNull String input) {
return input.equals("__d0nE__");
try {
Bukkit.createBlockData(input);
return true;
} catch(IllegalArgumentException ignore){}
return false;
}

@Override
@SuppressWarnings("unchecked")
protected @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext conversationContext, @NotNull String input) {
HandlerList.unregisterAll(this);
Map<String,String> blockDatas = (Map<String, String>) Objects.requireNonNull(conversationContext.getSessionData("blockData"));
blockDatas.put(blockStateKeys[0], input);
if(blockStateKeys.length>1) {
return new BlockDataPrompt(Arrays.copyOfRange(blockStateKeys,1,blockStateKeys.length));
} else {
return new ItemPrompt();
}
}

@SuppressWarnings("unchecked")
@EventHandler(priority = EventPriority.LOWEST)
public void onSelectBlockstate(PlayerInteractEvent event) {
Logger.getGlobal().info("onSelectBlockstate");
if(event.getAction().equals(Action.LEFT_CLICK_BLOCK)
&& event.hasItem()) {
if(event.getPlayer().equals(conversationContext.getForWhom()) && event.getAction().equals(Action.LEFT_CLICK_BLOCK)
&& event.getHand()!=null && event.getHand().equals(EquipmentSlot.HAND)
&& event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.PRISMARINE_SHARD)) {
Block block = event.getClickedBlock();
Logger.getGlobal().info("Block: "+block);
if(block != null) {
Logger.getGlobal().info("BlockData: "+block.getBlockData());
Map<String,String> blockDatas = (Map<String, String>) Objects.requireNonNull(conversationContext.getSessionData("blockData"));
blockDatas.put(blockStateKeys[0], block.getBlockData().getAsString());
HandlerList.unregisterAll(this);
event.setCancelled(true);
conversationContext.getForWhom().acceptConversationInput("__d0nE__");
Bukkit.getScheduler().runTaskLater(ArchitectPlugin.getPluginInstance(),()-> {
conversationContext.getForWhom().acceptConversationInput(block.getBlockData().getAsString());
}, 3);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public class BlockIdPrompt extends ValidatingPrompt {

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Type in an unique ID for the new block!";
return "Type in an unique ID for the new item! You may '!cancel' at any point of the conversation.";
}

@Override
protected @Nullable String getFailedValidationText(@NotNull ConversationContext context, @NotNull String invalidInput) {
return "You need to type in an item id that doesn't exist yet.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public BlockTypePrompt() {
super(getBlockTypes());
}

protected BlockTypePrompt(@NotNull String... fixedSet) {
super(fixedSet);
}

@Override
protected @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext conversationContext, @NotNull String input) {
conversationContext.setSessionData("type",input);
Expand All @@ -46,25 +50,18 @@ public BlockTypePrompt() {
}
}
return END_OF_CONVERSATION;
/*return switch(input) {
case "block" -> new BlockDataPrompt("");
case "bisected" -> new BlockDataPrompt("Up", "Down");
case "four_directions" -> new BlockDataPrompt("North", "West", "South", "East");
case "three_axis" -> new BlockDataPrompt("X", "Y", "Z");
case "six_faces" -> new BlockDataPrompt("North", "West", "South", "East", "Up", "Down");
case "branch_twigs" -> ;
case "branch_horizontal" ->
case "vanilla" -> new ItemPrompt();
default -> END_OF_CONVERSATION;
};*/
}

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Which type of block do you want to add? "+formatFixedSet()+" ";
}

@Override
protected @Nullable String getFailedValidationText(@NotNull ConversationContext context, @NotNull String invalidInput) {
return "Not all block types are supported. You need to type in one of the listed block types.";
}

public static String[] getBlockTypes() {
String[] result = new String[blockData.length];
for(int i = 0; i < blockData.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ public CategoryVisiblePrompt() {
super("yes", "no");
}

protected CategoryVisiblePrompt(@NotNull String... fixedSet) {
super(fixedSet);
}

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Should the new inventory item be listed directly in the category of it's collection. "+formatFixedSet();
}

@Override
protected @Nullable String getFailedValidationText(@NotNull ConversationContext context, @NotNull String invalidInput) {
return "If you type in 'no' the item will be available only by a block collection.";
}

@Override
protected @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext conversationContext, @NotNull String input) {
conversationContext.setSessionData("inCategory", input.equalsIgnoreCase("yes"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.mcmiddleearth.architect.specialBlockHandling.customInventories.editor.prompt.add;

import com.mcmiddleearth.architect.specialBlockHandling.customInventories.editor.prompt.edit.ChangeColorPrompt;
import com.mcmiddleearth.pluginutil.NumericUtil;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.ValidatingPrompt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class CmdPrompt extends ValidatingPrompt {

@Override
Expand All @@ -20,12 +23,17 @@ protected boolean isInputValid(@NotNull ConversationContext conversationContext,
if(!input.equalsIgnoreCase("!skip")) {
conversationContext.setSessionData("cmd",NumericUtil.getInt(input));
}
return new ColorPrompt(); //todo: only for leather
if(((String) Objects.requireNonNull(conversationContext.getSessionData("itemMaterial"))).startsWith("LEATHER")) {
return new ColorPrompt();
} else {
return new DisplayPrompt();
}
}

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Type in a value for custom model data or '!skip'"; //todo prompt current cmd
return "Current custom model data is "+conversationContext.getSessionData("cmd")
+". Type in a new custom model data or '!skip'";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ protected boolean isInputValid(@NotNull ConversationContext conversationContext,

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Type in a color for leather armor or '!skip'";
return "Current color is "+conversationContext.getSessionData("color")+". Type in a new color or '!skip'";
}

@Override
protected @Nullable String getFailedValidationText(@NotNull ConversationContext context, @NotNull String invalidInput) {
return "You need to type in a valid rgb color code.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DisplayPrompt extends StringPrompt {

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Enter a block description for the custom inventory:";
return "Enter a description for the custom inventory:";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,46 @@
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.FixedSetPrompt;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.ValidatingPrompt;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ItemPrompt extends ValidatingPrompt {
public class ItemPrompt extends FixedSetPrompt {

public ItemPrompt() {
super("ok");
}

protected ItemPrompt(String... fixedSet) {
super(fixedSet);
}

@Override
protected @Nullable Prompt acceptValidatedInput(@NotNull ConversationContext conversationContext, @NotNull String input) {
conversationContext.setSessionData("inventoryItem", ((Player)conversationContext.getForWhom()).getInventory().getItemInMainHand());
ItemStack item = ((Player)conversationContext.getForWhom()).getInventory().getItemInMainHand();
conversationContext.setSessionData("itemMaterial", item.getType().name());
if(item.hasItemMeta() && item.getItemMeta().hasCustomModelData()) {
conversationContext.setSessionData("cmd", item.getItemMeta().getCustomModelData());
}
if(item.getType().name().startsWith("LEATHER")) {
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
conversationContext.setSessionData("color", meta.getColor().asRGB());
}
return new CmdPrompt();
}

@Override
protected boolean isInputValid(@NotNull ConversationContext context, @NotNull String input) {
return !((Player)context.getForWhom()).getInventory().getItemInMainHand().getType().equals(Material.AIR)
&& input.equalsIgnoreCase("ok");
&& super.isInputValid(context, input);

}

@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext) {
return "Hold the item you want to add to custom inventory in main hand and type 'ok'.";
}

}
Loading

0 comments on commit 0be7d59

Please sign in to comment.