Skip to content

Commit

Permalink
Fix bug with Tag module v0.1.6
Browse files Browse the repository at this point in the history
Check if player inventory is full before giving them the tag
Added config option to Tag to play sound if player's inv is full
  • Loading branch information
Machine-Maker committed Jul 16, 2020
1 parent ed4dc66 commit e5de6da
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/java/me/machinemaker/vanillatweaks/tag/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ class Config extends BaseConfig {

@Path("show-messages")
public Boolean showMessages = true;

@Path("play-sound")
public Boolean playSound = true;
}
16 changes: 14 additions & 2 deletions src/main/java/me/machinemaker/vanillatweaks/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -26,6 +27,8 @@ public class Tag extends BaseModule implements Listener {
final NamespacedKey tagKey = new NamespacedKey(this.plugin, "tag");
private Team colorTeam;

private TagRunnable runnable;

public Tag(VanillaTweaks plugin) {
super(plugin, config -> config.tag);
config.init(plugin, new File(plugin.getDataFolder(), "tag"));
Expand All @@ -41,7 +44,7 @@ public Tag(VanillaTweaks plugin) {
}

@EventHandler
public void onPlayerInteract(EntityDamageByEntityEvent event) {
public void onPlayerDamage(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player) || event.getEntityType() != EntityType.PLAYER) return;
Player damager = (Player) event.getDamager();
Player damagee = (Player) event.getEntity();
Expand All @@ -61,7 +64,13 @@ void setAsIt(Player player) {
player.getPersistentDataContainer().set(this.tagKey, PersistentDataType.BYTE, (byte) 1);
player.setDisplayName(ChatColor.RED + player.getDisplayName());
player.setPlayerListName(ChatColor.RED + player.getDisplayName());
player.getInventory().addItem(tagItem.clone());
int firstEmpty = player.getInventory().firstEmpty();
if (firstEmpty > -1) player.getInventory().setItem(firstEmpty, tagItem.clone());
else {
Item item = player.getWorld().dropItem(player.getLocation(), tagItem.clone());
item.setPickupDelay(0);
this.runnable.addPlayerItem(player, item);
}
}

void removeAsIt(Player player) {
Expand All @@ -75,11 +84,14 @@ void removeAsIt(Player player) {
@Override
public void register() {
this.registerEvents(this);
this.runnable = new TagRunnable(this);
this.runnable.runTaskTimer(this.plugin, 0L, 5L);
}

@Override
public void unregister() {
this.unregisterEvents(this);
this.runnable.cancel();
}

@Override
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/me/machinemaker/vanillatweaks/tag/TagRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.machinemaker.vanillatweaks.tag;

import com.google.common.collect.Maps;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

class TagRunnable extends BukkitRunnable {

private final Map<Player, Item> playerItemMap = Maps.newHashMap();
private final Tag module;
private int count = 1;

public TagRunnable(Tag module) {
this.module = module;
}

public void addPlayerItem(Player player, Item item) {
playerItemMap.put(player, item);
}

@Override
public void run() {
for (Iterator<Entry<Player, Item>> iterator = playerItemMap.entrySet().iterator(); iterator.hasNext(); ) {
Entry<Player, Item> entry = iterator.next();
if (entry.getValue().isDead()) {
iterator.remove();
continue;
}
entry.getValue().teleport(entry.getKey());
if (count % 4 == 0) {
count = 0;
if (this.module.config.playSound) entry.getKey().playSound(entry.getKey().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 0.5f, 0.5f);
}
}
count++;
}
}

0 comments on commit e5de6da

Please sign in to comment.