Skip to content

Commit

Permalink
Potion effects fixed + janky food
Browse files Browse the repository at this point in the history
  • Loading branch information
btarg committed Dec 12, 2023
1 parent 5285a8f commit cd537e1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.btarg.origami.util.NamespacedKeyHelper;
import io.github.btarg.origami.util.parsers.AttributeParser;
import io.github.btarg.origami.util.parsers.EnchantmentParser;
import io.github.btarg.origami.util.parsers.PotionEffectParser;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
Expand All @@ -17,7 +18,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull;

import java.util.*;
Expand All @@ -27,7 +28,7 @@ public class CustomItemDefinition extends BaseCustomDefinition {
public Map<Enchantment, Integer> enchantments;
public Map<Attribute, Map<UUID, AttributeModifier>> attributes;
public List<ItemFlag> flags = new ArrayList<>();
public List<PotionEffectType> potionEffects = new ArrayList<>();
public List<PotionEffect> potionEffects = new ArrayList<>();
public Integer durability;

public CustomItemDefinition(Map<String, Object> map) {
Expand All @@ -42,7 +43,7 @@ public CustomItemDefinition(Map<String, Object> map) {
this.durability = Objects.requireNonNullElse((Integer) map.get("durability"), (int) this.baseMaterial.getMaxDurability());
this.enchantments = EnchantmentParser.parseEnchantments(Objects.requireNonNullElse((List<String>) map.get("enchantments"), new ArrayList<>()));
this.flags = deserializeFlags(map);
this.potionEffects = deserializePotionEffects(map);
this.potionEffects = PotionEffectParser.parsePotionEffects(Objects.requireNonNullElse((List<String>) map.get("potionEffects"), new ArrayList<>()));
}

private List<ItemFlag> deserializeFlags(Map<String, Object> map) {
Expand All @@ -58,21 +59,6 @@ private List<ItemFlag> deserializeFlags(Map<String, Object> map) {
.toList());
}

private List<PotionEffectType> deserializePotionEffects(Map<String, Object> map) {
List<String> potionEffectNames = (List<String>) map.get("potionEffects");
List<PotionEffectType> potionEffectList = new ArrayList<>();

if (potionEffectNames != null) {
for (String potionEffectName : potionEffectNames) {
PotionEffectType potionEffectType = PotionEffectType.getByName(potionEffectName);
if (potionEffectType != null) {
potionEffectList.add(potionEffectType);
}
}
}
return potionEffectList;
}

@Override
protected Integer getCustomModelData() {
return ResourcePackGenerator.getItemOverride(this);
Expand Down Expand Up @@ -119,18 +105,21 @@ public void registerDefinition(CommandSender sender) {

@Override
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
Map<String, Object> map = super.serialize();

List<String> enchantmentStrings = this.enchantments.entrySet().stream()
.map(entry -> entry.getKey() + "(" + entry.getValue() + ")")
.toList();
List<String> potionEffectStrings = this.potionEffects.stream()
.map(effect -> effect.getType() + "(" + effect.getDuration() + ", " + effect.getAmplifier() + ")")
.toList();

List<String> enchantmentStrings = new ArrayList<>();
for (var entry : this.enchantments.entrySet()) {
enchantmentStrings.add(entry.getKey().getKey().value() + ";" + entry.getValue().toString());
}
map.put("enchantments", enchantmentStrings);
map.put("potionEffects", this.potionEffects.stream().map(PotionEffectType::getName).toList());
map.put("potionEffects", potionEffectStrings);
map.put("flags", this.flags.stream().map(ItemFlag::name).toList());
map.put("durability", this.durability);

map.putAll(super.serialize());
return map;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.btarg.origami.events;

import io.github.btarg.origami.OrigamiMain;
import io.github.btarg.origami.definitions.CustomBlockDefinition;
import io.github.btarg.origami.definitions.CustomItemDefinition;
import io.github.btarg.origami.registry.RegistryHelper;
Expand All @@ -9,6 +10,11 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Objects;

public class CustomEventListener implements Listener {

Expand All @@ -23,7 +29,6 @@ public void onPlayerInteract(PlayerInteractEvent e) {
CustomItemDefinition itemDefinition = RegistryHelper.getDefinitionFromItemstack(e.getItem());
boolean noItem = itemDefinition == null;
if (noBlock && noItem) return;
e.setCancelled(true);

switch (e.getAction()) {
case RIGHT_CLICK_AIR:
Expand All @@ -34,8 +39,10 @@ public void onPlayerInteract(PlayerInteractEvent e) {
break;

case RIGHT_CLICK_BLOCK:
if (!noBlock)
if (!noBlock) {
blockDefinition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);
e.setCancelled(true);
}

if (!noItem) {
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK_BLOCK.toString(), player);
Expand Down Expand Up @@ -66,4 +73,25 @@ public void onPlayerInteract(PlayerInteractEvent e) {
}
}

@EventHandler
public void onCustomFoodEaten(PlayerItemConsumeEvent e) {
Player player = e.getPlayer();
CustomItemDefinition itemDefinition = RegistryHelper.getDefinitionFromItemstack(e.getItem());
if (itemDefinition == null) return;
e.setCancelled(true);
itemDefinition.executeEvent(EventNames.ON_CONSUMED.toString(), player);
//TODO: food saturation level
for (PotionEffect effect : itemDefinition.potionEffects) {
player.addPotionEffect(effect);
}
int amount = Math.max(e.getItem().getAmount() - 1, 0);
new BukkitRunnable() {
@Override
public void run() {
player.getInventory().getItem(Objects.requireNonNull(e.getHand())).setAmount(amount);
player.updateInventory();
}
}.runTaskLater(OrigamiMain.getInstance(), 1);
}

}
4 changes: 2 additions & 2 deletions src/main/java/io/github/btarg/origami/events/EventNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public enum EventNames {
ON_PLACED("onPlaced"),
ON_BROKEN("onBroken"),
ON_DAMAGED("onDamaged"),
ON_PUSHED("onPushed");

ON_PUSHED("onPushed"),
ON_CONSUMED("onConsumed");
private final String eventName;

EventNames(String eventName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.btarg.origami.util.parsers;

import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("deprecation")
public class PotionEffectParser {

public static List<PotionEffect> parsePotionEffects(List<String> effectStrings) {
if (effectStrings == null) return new ArrayList<>();

List<PotionEffect> parsedEffects = new ArrayList<>();

for (String input : effectStrings) {
// Split the input string by "(" and ")"
String[] parts = input.trim().split("\\(");

// Default to 1 so that if we don't specify a count we still get an item stack
int level = 1;
int duration = 20; // Default duration

String effectName = parts[0];

try {
// Attempt to parse the duration and level between "(" and ")"
if (parts.length == 2) {
String[] durationLevel = parts[1].replaceAll("[^0-9,]", "").split(",");
if (durationLevel.length == 2) {
duration = Integer.parseInt(durationLevel[0]);
level = Integer.parseInt(durationLevel[1]);
}
}

} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid number format in input.");
}

PotionEffectType potionEffectType = PotionEffectType.getByName(effectName);
if (potionEffectType != null) {
PotionEffect effect = potionEffectType.createEffect(duration, level);
parsedEffects.add(effect);
}
}

return parsedEffects;
}
}

0 comments on commit cd537e1

Please sign in to comment.