Skip to content

Commit

Permalink
Merge pull request #253 from Sylfare/fix/batch1
Browse files Browse the repository at this point in the history
fix: various bugs
  • Loading branch information
oddlama authored Jul 4, 2024
2 parents 8bbea3c + 8039217 commit 7327c9b
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 38 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ subprojects {
maven("https://repo.codemc.org/repository/maven-public/")
maven("https://jitpack.io")
maven("https://api.modrinth.com/maven")
maven("https://repo.bluecolored.de/releases")
}

tasks.withType<JavaCompile> {
Expand All @@ -42,8 +43,8 @@ subprojects {
}

dependencies {
compileOnly(group = "org.jetbrains", name = "annotations", version = "24.0.1")
annotationProcessor("org.jetbrains:annotations:24.0.1")
compileOnly(group = "org.jetbrains", name = "annotations", version = "24.1.0")
annotationProcessor("org.jetbrains:annotations:24.1.0")
}
}

Expand Down Expand Up @@ -86,7 +87,7 @@ configure(subprojects.filter {
}

dependencies {
implementation(group = "com.comphenix.protocol", name = "ProtocolLib", version = "5.2.1-SNAPSHOT")
implementation(group = "com.comphenix.protocol", name = "ProtocolLib", version = "5.3.0-SNAPSHOT")

compileOnly(project(":vane-annotations"))
annotationProcessor(project(path = ":vane-annotations", configuration = "reobf"))
Expand Down Expand Up @@ -124,7 +125,7 @@ configure(subprojects.filter {
}) {
dependencies {
implementation(group = "us.dynmap", name = "dynmap-api", version = "3.2-SNAPSHOT")
implementation(group = "com.github.BlueMap-Minecraft", name = "BlueMapAPI", version = "v2.3.0")
implementation(group = "de.bluecolored.bluemap", name = "BlueMapAPI", version = "2.7.2")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.oddlama.vane.core.resourcepack.ResourcePackGenerator;
import org.oddlama.vane.util.StorageUtil;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;

Expand Down Expand Up @@ -126,14 +127,21 @@ public LootTableList default_loot_tables() {
return LootTableList.of();
}

/**
* Returns the type of item for the resource pack
*/
public Key itemType(){
return Key.key(Key.MINECRAFT_NAMESPACE, "item/generated");
}

@Override
public void addResources(final ResourcePackGenerator rp) throws IOException {
final var resource_name = "items/" + key().value() + ".png";
final var resource = get_module().getResource(resource_name);
if (resource == null) {
throw new RuntimeException("Missing resource '" + resource_name + "'. This is a bug.");
}
rp.add_item_model(key(), resource);
rp.add_item_model(key(), resource, itemType());
rp.add_item_override(baseMaterial().getKey(), key(), predicate -> predicate.put("custom_model_data", customModelData()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.oddlama.vane.util.Nms.item_handle;

import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.craftbukkit.entity.CraftItem;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -14,6 +15,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
Expand Down Expand Up @@ -234,4 +236,16 @@ public void on_player_right_click(final PlayerInteractEvent event) {
event.setUseItemInHand(Event.Result.DENY);
}
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void on_dispense(BlockDispenseEvent event) {
if(event.getBlock().getType() != Material.DISPENSER) {
return;
}

final var custom_item = get_module().item_registry().get(event.getItem());
if(inhibit(custom_item, InhibitBehavior.DISPENSE)) {
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public enum InhibitBehavior {
MEND,
/** Prevents players from adding new enchantments via the anvil. */
NEW_ENCHANTS,
/** Prevents dispensers from dispense this item */
DISPENSE
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang3.tuple.Pair;
import org.bukkit.NamespacedKey;
import org.json.JSONArray;
import org.json.JSONObject;

import net.kyori.adventure.key.Key;

public class ResourcePackGenerator {

private String description = "";
private byte[] icon_png_content = null;
private Map<String, Map<String, JSONObject>> translations = new HashMap<>();
private Map<NamespacedKey, List<JSONObject>> item_overrides = new HashMap<>();
private Map<NamespacedKey, Pair<byte[], byte[]>> item_textures = new HashMap<>();
private Map<NamespacedKey, PackEntry> item_textures = new HashMap<>();

public void set_description(String description) {
this.description = description;
Expand All @@ -49,13 +50,13 @@ public JSONObject translations(String namespace, String lang_code) {
}
return lang_map;
}

public void add_item_model(NamespacedKey key, InputStream texture_png) throws IOException {
item_textures.put(key, Pair.of(texture_png.readAllBytes(), null));
public void add_item_model(NamespacedKey key, InputStream texture_png, Key parent) throws IOException {
item_textures.put(key, new PackEntry(texture_png.readAllBytes(), parent));
}

public void add_item_model(NamespacedKey key, InputStream texture_png, InputStream texture_png_mcmeta) throws IOException {
item_textures.put(key, Pair.of(texture_png.readAllBytes(), texture_png_mcmeta.readAllBytes()));
public void add_item_model(NamespacedKey key, InputStream texture_png, InputStream texture_png_mcmeta, Key parent) throws IOException {
item_textures.put(key, new PackEntry(texture_png.readAllBytes(), texture_png_mcmeta.readAllBytes(), parent));
}

public void add_item_override(
Expand Down Expand Up @@ -98,22 +99,20 @@ private void write_translations(final ZipOutputStream zip) throws IOException {
}
}

private JSONObject create_item_model_handheld(NamespacedKey texture) {
private JSONObject create_item_model(NamespacedKey texture, Key item_type) {
// Create model json
final var model = new JSONObject();

// FIXME: hardcoded fixes. better rewrite RP generator
// and use static files for all items. just language should be generated.
if (texture.getNamespace().equals("minecraft") && texture.getKey().equals("dropper")) {
model.put("parent", "minecraft:block/dropper");
} else if (texture.getNamespace().equals("minecraft") && texture.getKey().endsWith("shulker_box")) {
final var textures = new JSONObject();
if (texture.getNamespace().equals("minecraft") && texture.getKey().endsWith("shulker_box")) {
model.put("parent", "minecraft:item/template_shulker_box");
final var textures = new JSONObject();

textures.put("particle", "minecraft:block/" + texture.getKey());
model.put("textures", textures);
} else {
model.put("parent", "minecraft:item/handheld");
final var textures = new JSONObject();
model.put("parent", item_type.toString());
if (texture.getNamespace().equals("minecraft") && texture.getKey().equals("compass")) {
textures.put("layer0", texture.getNamespace() + ":item/compass_16");
} else {
Expand All @@ -128,23 +127,23 @@ private JSONObject create_item_model_handheld(NamespacedKey texture) {
private void write_item_models(final ZipOutputStream zip) throws IOException {
for (var entry : item_textures.entrySet()) {
final var key = entry.getKey();
final var texture_png = entry.getValue().getLeft();
final var texture_png_mcmeta = entry.getValue().getRight();
final var texture_png = entry.getValue().texture_png;
final var texture_png_mcmeta = entry.getValue().texture_png_mcmeta;

// Write texture
zip.putNextEntry(new ZipEntry("assets/" + key.getNamespace() + "/textures/item/" + key.getKey() + ".png"));
zip.write(texture_png);
zip.closeEntry();

// Write mcmeta if given
if (texture_png_mcmeta != null) {
if (texture_png_mcmeta.length > 0) {
zip.putNextEntry(new ZipEntry("assets/" + key.getNamespace() + "/textures/item/" + key.getKey() + ".png.mcmeta"));
zip.write(texture_png_mcmeta);
zip.closeEntry();
}

// Write model json
final var model = create_item_model_handheld(key);
final var model = create_item_model(key, entry.getValue().parent);
zip.putNextEntry(new ZipEntry("assets/" + key.getNamespace() + "/models/item/" + key.getKey() + ".json"));
zip.write(model.toString().getBytes(StandardCharsets.UTF_8));
zip.closeEntry();
Expand All @@ -171,7 +170,7 @@ private void write_item_overrides(final ZipOutputStream zip) throws IOException
})).forEach(overrides::put);

// Create model json
final var model = create_item_model_handheld(key);
final var model = create_item_model(key, override_parent(key));
model.put("overrides", overrides);

// Write item model override
Expand Down Expand Up @@ -200,4 +199,46 @@ public void write(File file) throws IOException {
throw e;
}
}

class PackEntry {
final byte[] texture_png;
final byte[] texture_png_mcmeta;
final Key parent;

PackEntry(byte[] texture_png, byte[] texture_png_mcmeta, Key parent) {
this.texture_png = texture_png;
this.texture_png_mcmeta = texture_png_mcmeta;
this.parent = parent;
}

PackEntry(byte[] texture_png, Key parent) {
this.texture_png = texture_png;
this.texture_png_mcmeta = new byte[0];
this.parent = parent;
}
}

/**
* Gives the type of parent used by the given item
*
* @param item_key
* @return a key containing the parent type
*/
private Key override_parent(NamespacedKey item_key){
switch(item_key.getKey()){
case "wooden_hoe":
case "stone_hoe":
case "iron_hoe":
case "golden_hoe":
case "diamond_hoe":
case "netherite_hoe":
return Key.key("minecraft:item/handheld");
case "warped_fungus_on_a_stick":
return Key.key("minecraft:item/handheld_rod");
case "dropper":
return Key.key("minecraft:block/dropper");
default:
return Key.key("minecraft:item/generated");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jetbrains.annotations.Nullable;
import org.oddlama.vane.annotation.lang.LangMessage;
import org.oddlama.vane.core.Listener;
import org.oddlama.vane.core.item.api.CustomItem;
import org.oddlama.vane.core.lang.TranslatedMessage;
import org.oddlama.vane.core.module.Context;

Expand Down Expand Up @@ -192,9 +193,17 @@ public boolean open_block_state_inventory(@NotNull final Player player, @NotNull
}

// Transfer item name to block-state
Component name = null;
Component name = Component.text("");
if (meta.getBlockState() instanceof Nameable nameable) {
name = meta.hasDisplayName() ? meta.displayName() : null;
name = meta.hasDisplayName() ? meta.displayName() :
meta.hasItemName() ? meta.itemName() : null;


// if the item has neither custom name nor item name (an old item with custom name reset), get the name from registry if possible
if(name == null) {
CustomItem custom_item = get_module().core.item_registry().get(item);
name = custom_item != null ? custom_item.displayName() : Component.text("");
}
nameable.customName(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.oddlama.vane.trifles.Trifles;
import org.oddlama.vane.util.BlockUtil;

import net.kyori.adventure.key.Key;

@VaneItem(name = "file", base = Material.WARPED_FUNGUS_ON_A_STICK, durability = 4000, model_data = 0x760003, version = 1)
public class File extends CustomItem<Trifles> {
public File(Context<Trifles> context) {
Expand Down Expand Up @@ -419,4 +421,9 @@ public void on_player_right_click(final PlayerInteractEvent event) {
damage_item(player, item, 1);
swing_arm(player, event.getHand());
}

@Override
public Key itemType() {
return Key.key(Key.MINECRAFT_NAMESPACE, "item/handheld");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.oddlama.vane.trifles.Trifles;
import org.oddlama.vane.util.StorageUtil;

import net.kyori.adventure.key.Key;

@VaneItem(name = "north_compass", base = Material.COMPASS, model_data = 0x760013, version = 1)
public class NorthCompass extends CustomItem<Trifles> {

public NorthCompass(final Context<Trifles> context) {
super(context);
}
Expand All @@ -43,8 +46,10 @@ public ItemStack updateItemStack(final ItemStack item_stack) {
if (worlds.size() > 0) {
final var world = worlds.get(0);
if (world != null) {
item_stack.editMeta(CompassMeta.class, meta -> meta.setLodestoneTracked(true));
item_stack.editMeta(CompassMeta.class, meta -> meta.setLodestone(new Location(world, 0.0, 0.0, -999999999.0)));
item_stack.editMeta(CompassMeta.class, meta -> {
meta.setLodestone(new Location(world, 0.0, 0.0, -300000000.0));
meta.setLodestoneTracked(false);
});
}
}
return item_stack;
Expand All @@ -71,7 +76,7 @@ public void on_player_click_inventory(final InventoryClickEvent event) {
// is stored forever.
if (!meta.hasLodestone()) {
meta.setLodestoneTracked(false);
meta.setLodestone(new Location(event.getWhoClicked().getWorld(), 0.0, 0.0, -999999999.0));
meta.setLodestone(new Location(event.getWhoClicked().getWorld(), 0.0, 0.0, -300000000.0));
}
});
}
Expand Down Expand Up @@ -135,7 +140,7 @@ public void addResources(final ResourcePackGenerator rp) throws IOException {
throw new RuntimeException("Missing resource '" + resource_name + "'. This is a bug.");
}
final var key_num = StorageUtil.namespaced_key(key().namespace(), String.format("%s_%02d", key().value(), num));
rp.add_item_model(key_num, resource);
rp.add_item_model(key_num, resource, Key.key(Key.MINECRAFT_NAMESPACE, "item/generated"));
rp.add_item_override(base_key, key_num, predicate -> {
predicate.put("custom_model_data", customModelData());
predicate.put("angle", angle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.oddlama.vane.trifles.Trifles;
import org.oddlama.vane.util.StorageUtil;

import net.kyori.adventure.key.Key;

@VaneItem(name = "reinforced_elytra", base = Material.ELYTRA, durability = 864, model_data = 0x760002, version = 1)
public class ReinforcedElytra extends CustomItem<Trifles> {
public static final UUID MODIFIER_UUID_REINFORCED_ELYTRA_DEFENSE = UUID.fromString("8d3a5a3c-06d4-40c5-be66-41ebf6a46435"); // Self-generated; Must always be the same!
Expand Down Expand Up @@ -69,7 +71,7 @@ public void addResources(final ResourcePackGenerator rp) throws IOException {
}

final var key_broken = StorageUtil.subkey(key(), "broken");
rp.add_item_model(key_broken, broken_resource);
rp.add_item_model(key_broken, broken_resource, Key.key(Key.MINECRAFT_NAMESPACE, "item/generated"));
rp.add_item_override(baseMaterial().getKey(), key_broken, predicate -> {
predicate.put("custom_model_data", customModelData());
predicate.put("broken", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.oddlama.vane.trifles.Trifles;
import org.oddlama.vane.util.BlockUtil;

import net.kyori.adventure.key.Key;

public abstract class Sickle extends CustomItem<Trifles> {
@ConfigDouble(def = Double.NaN, desc = "Attack damage modifier.")
public double config_attack_damage;
Expand Down Expand Up @@ -60,4 +62,9 @@ public ItemStack updateItemStack(ItemStack item_stack) {
public EnumSet<InhibitBehavior> inhibitedBehaviors() {
return EnumSet.of(InhibitBehavior.USE_IN_VANILLA_RECIPE, InhibitBehavior.HOE_TILL, InhibitBehavior.USE_OFFHAND);
}

@Override
public Key itemType() {
return Key.key(Key.MINECRAFT_NAMESPACE, "item/handheld");
}
}
Loading

0 comments on commit 7327c9b

Please sign in to comment.