Fix some bugs with Tic Tac Toe

This commit is contained in:
Sam 2017-10-25 15:40:26 +01:00 committed by Alexander Meech
parent 8429d2f0d1
commit c1860c03c8
3 changed files with 101 additions and 30 deletions

View File

@ -29,8 +29,11 @@ public class ItemGadgetPage extends GadgetPage
@Override @Override
protected void addCustomLore(Gadget gadget, List<String> lore) protected void addCustomLore(Gadget gadget, List<String> lore)
{ {
lore.add(C.cBlack); if (gadget instanceof ItemGadget && ((ItemGadget) gadget).isUsingAmmo())
lore.add(C.cWhite + "You own " + C.cGreen + getPlugin().getInventoryManager().Get(getPlayer()).getItemCount(gadget.getName())); {
lore.add(C.blankLine);
lore.add(C.cWhite + "You own " + C.cGreen + getPlugin().getInventoryManager().Get(getPlayer()).getItemCount(gadget.getName()));
}
} }
@Override @Override
@ -38,7 +41,7 @@ public class ItemGadgetPage extends GadgetPage
{ {
ItemGadget itemGadget = (ItemGadget) gadget; ItemGadget itemGadget = (ItemGadget) gadget;
if (itemGadget.getAmmo() != null && itemGadget.getAmmo().getCost(GlobalCurrency.TREASURE_SHARD) <= 0) if (itemGadget.isUsingAmmo() && itemGadget.getAmmo().getCost(GlobalCurrency.TREASURE_SHARD) <= 0)
{ {
return; return;
} }

View File

@ -10,12 +10,14 @@ import java.util.concurrent.TimeUnit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -26,6 +28,8 @@ import mineplex.core.common.jsonchat.HoverEvent;
import mineplex.core.common.jsonchat.JsonMessage; import mineplex.core.common.jsonchat.JsonMessage;
import mineplex.core.common.util.C; import mineplex.core.common.util.C;
import mineplex.core.common.util.F; import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilParticle; import mineplex.core.common.util.UtilParticle;
import mineplex.core.common.util.UtilParticle.ParticleType; import mineplex.core.common.util.UtilParticle.ParticleType;
import mineplex.core.common.util.UtilParticle.ViewDist; import mineplex.core.common.util.UtilParticle.ViewDist;
@ -37,6 +41,8 @@ import mineplex.core.gadget.types.ItemGadget;
import mineplex.core.gadget.util.CostConstants; import mineplex.core.gadget.util.CostConstants;
import mineplex.core.itemstack.ItemBuilder; import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.recharge.Recharge; import mineplex.core.recharge.Recharge;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
public class ItemOAndX extends ItemGadget public class ItemOAndX extends ItemGadget
{ {
@ -77,6 +83,7 @@ public class ItemOAndX extends ItemGadget
}; };
private static final ItemStack FLOOR = new ItemStack(Material.WOOL); private static final ItemStack FLOOR = new ItemStack(Material.WOOL);
private static final ItemStack DEFAULT_TILE = new ItemStack(Material.WOOL, 1, (short) 0, (byte) 15); private static final ItemStack DEFAULT_TILE = new ItemStack(Material.WOOL, 1, (short) 0, (byte) 15);
private static final long TIMEOUT = TimeUnit.MINUTES.toMillis(3);
private final Map<String, Pair<String, Long>> _invites; private final Map<String, Pair<String, Long>> _invites;
private final Set<GameBoard> _gameBoards; private final Set<GameBoard> _gameBoards;
@ -106,9 +113,6 @@ public class ItemOAndX extends ItemGadget
} }
} }
Location location = player.getLocation();
//TODO implement space check
return true; return true;
} }
@ -123,6 +127,7 @@ public class ItemOAndX extends ItemGadget
} }
sendGameInvite(nearby, player); sendGameInvite(nearby, player);
return;
} }
} }
@ -143,10 +148,30 @@ public class ItemOAndX extends ItemGadget
private void startGame(Player invitee, Player inviter) private void startGame(Player invitee, Player inviter)
{ {
Location location = inviter.getLocation();
if (!UtilEnt.isGrounded(inviter))
{
inviter.sendMessage(F.main(Manager.getName(), "You must be on the ground to play."));
invitee.sendMessage(F.main(Manager.getName(), "Sorry, " + F.name(inviter.getName()) + " needs to be on the ground to play."));
return;
}
for (Block block : UtilBlock.getInBoundingBox(location.clone().add(4, 1, 4), location.clone().subtract(4, 0, 4)))
{
if (UtilBlock.airFoliage(block))
{
continue;
}
inviter.sendMessage(F.main(Manager.getName(), "You cannot play in such a small area."));
invitee.sendMessage(F.main(Manager.getName(), "Sorry, " + F.name(inviter.getName()) + " needs to find a more open area to play."));
return;
}
invitee.sendMessage(F.main(Manager.getName(), F.name(inviter.getName()) + " challenged you to a game of " + F.name(getName()) + "!")); invitee.sendMessage(F.main(Manager.getName(), F.name(inviter.getName()) + " challenged you to a game of " + F.name(getName()) + "!"));
inviter.sendMessage(F.main(Manager.getName(), "You challenged " + F.name(invitee.getName()) + " to a game of " + F.name(getName()) + "!")); inviter.sendMessage(F.main(Manager.getName(), "You challenged " + F.name(invitee.getName()) + " to a game of " + F.name(getName()) + "!"));
Location location = inviter.getLocation();
location.setYaw(0); location.setYaw(0);
{ {
@ -275,16 +300,58 @@ public class ItemOAndX extends ItemGadget
} }
@EventHandler @EventHandler
public void gameStart(CoreGameStartEvent event) public void playerQuit(PlayerQuitEvent event)
{ {
_gameBoards.forEach(this::cleanupBoard); Player player = event.getPlayer();
_gameBoards.removeIf(board ->
{
if (board.PlayerA.equals(player) || board.PlayerB.equals(player))
{
cleanupBoard(board, false);
return true;
}
return false;
});
} }
private void cleanupBoard(GameBoard board) @EventHandler
public void gameStart(CoreGameStartEvent event)
{
_gameBoards.forEach(board -> cleanupBoard(board, false));
_gameBoards.clear();
}
@EventHandler
public void updateTimeout(UpdateEvent event)
{
if (event.getType() != UpdateType.SLOWER)
{
return;
}
_gameBoards.removeIf(board ->
{
if (UtilTime.elapsed(board.StartTime, TIMEOUT))
{
cleanupBoard(board, false);
return true;
}
return false;
});
}
private void cleanupBoard(GameBoard board, boolean remove)
{ {
Recharge.Instance.use(board.PlayerA, getName(), COOLDOWN_USE, true, true); Recharge.Instance.use(board.PlayerA, getName(), COOLDOWN_USE, true, true);
board.cleanup(); board.cleanup();
_gameBoards.remove(board);
if (remove)
{
_gameBoards.remove(board);
}
} }
private class GameBoard private class GameBoard
@ -352,7 +419,7 @@ public class ItemOAndX extends ItemGadget
if (hasDrawn()) if (hasDrawn())
{ {
UtilTextMiddle.display(C.cRedB + "DRAW", "", 10, 30, 10, PlayerA, PlayerB); UtilTextMiddle.display(C.cRedB + "DRAW", "", 10, 30, 10, PlayerA, PlayerB);
cleanupBoard(this); cleanupBoard(this, true);
return; return;
} }
else if (hasWon(playerATurn ? 1 : 2)) else if (hasWon(playerATurn ? 1 : 2))
@ -364,7 +431,7 @@ public class ItemOAndX extends ItemGadget
UtilTextMiddle.display(C.cRedB + "YOU LOST", "", 10, 30, 10, loser); UtilTextMiddle.display(C.cRedB + "YOU LOST", "", 10, 30, 10, loser);
winner.getWorld().playSound(winner.getLocation(), Sound.VILLAGER_YES, 1, 1); winner.getWorld().playSound(winner.getLocation(), Sound.VILLAGER_YES, 1, 1);
loser.getWorld().playSound(winner.getLocation(), Sound.VILLAGER_NO, 1, 1); loser.getWorld().playSound(winner.getLocation(), Sound.VILLAGER_NO, 1, 1);
cleanupBoard(this); cleanupBoard(this, true);
return; return;
} }
else else

View File

@ -1,17 +1,11 @@
package mineplex.core.gadget.types; package mineplex.core.gadget.types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F; import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilBlock; import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEvent; import mineplex.core.common.util.UtilEvent;
@ -78,15 +72,12 @@ public abstract class ItemGadget extends Gadget
_active.add(player); _active.add(player);
List<String> itemLore = new ArrayList<>(); giveItem(player);
itemLore.addAll(Arrays.asList(getDescription()));
itemLore.add(C.cBlack);
itemLore.add(C.cWhite + "Your Ammo : " + Manager.getInventoryManager().Get(player).getItemCount(getName()));
player.getInventory().setItem(Manager.getActiveItemSlot(), ItemStackFactory.Instance.CreateStack(getDisplayMaterial(), getDisplayData(), 1, F.item(Manager.getInventoryManager().Get(player).getItemCount(getName()) + " " + getName()), itemLore));
if (inform) if (inform)
UtilPlayer.message(player, F.main("Gadget", "You equipped " + F.elem(getName()) + ".")); {
UtilPlayer.message(player, F.main(Manager.getName(), "You equipped " + F.elem(getName()) + "."));
}
} }
@EventHandler @EventHandler
@ -101,13 +92,18 @@ public abstract class ItemGadget extends Gadget
if (player.isOnline()) if (player.isOnline())
{ {
player.getInventory().remove(getDisplayMaterial()); player.getInventory().remove(getDisplayMaterial());
player.getInventory().setItem(Manager.getActiveItemSlot(), ItemStackFactory.Instance.CreateStack(getDisplayMaterial(), getDisplayData(), 1, F.item(Manager.getInventoryManager().Get(player).getItemCount(getName()) + " " + getName()))); giveItem(player);
UtilInv.Update(player); UtilInv.Update(player);
} }
}, 1); }, 1);
} }
} }
private void giveItem(Player player)
{
player.getInventory().setItem(Manager.getActiveItemSlot(), ItemStackFactory.Instance.CreateStack(getDisplayMaterial(), getDisplayData(), 1, F.item((isUsingAmmo() ? getAmmoCount(player) + " " : "") + getName())));
}
protected void RemoveItem(Player player, boolean message) protected void RemoveItem(Player player, boolean message)
{ {
if (_active.remove(player)) if (_active.remove(player))
@ -157,7 +153,7 @@ public abstract class ItemGadget extends Gadget
UtilPlayer.message(player, F.main(Manager.getName(), "You do not have any " + getAmmo().getName() + " left.")); UtilPlayer.message(player, F.main(Manager.getName(), "You do not have any " + getAmmo().getName() + " left."));
ItemGadgetOutOfAmmoEvent ammoEvent = new ItemGadgetOutOfAmmoEvent(event.getPlayer(), this); ItemGadgetOutOfAmmoEvent ammoEvent = new ItemGadgetOutOfAmmoEvent(event.getPlayer(), this);
Bukkit.getServer().getPluginManager().callEvent(ammoEvent); UtilServer.CallEvent(ammoEvent);
return; return;
} }
@ -181,18 +177,23 @@ public abstract class ItemGadget extends Gadget
if (isUsingAmmo()) if (isUsingAmmo())
{ {
Manager.getInventoryManager().addItemToInventory(player, getName(), -1); Manager.getInventoryManager().addItemToInventory(player, getName(), -1);
player.getInventory().setItem(Manager.getActiveItemSlot(), ItemStackFactory.Instance.CreateStack(getDisplayMaterial(), getDisplayData(), 1, F.item(Manager.getInventoryManager().Get(player).getItemCount(getName()) + " " + getName()))); giveItem(player);
} }
ActivateCustom(event.getPlayer()); ActivateCustom(event.getPlayer());
} }
private int getAmmoCount(Player player)
{
return Manager.getInventoryManager().Get(player).getItemCount(getName());
}
protected boolean hasAmmo(Player player) protected boolean hasAmmo(Player player)
{ {
return !isUsingAmmo() || Manager.getInventoryManager().Get(player).getItemCount(getName()) > 0; return !isUsingAmmo() || Manager.getInventoryManager().Get(player).getItemCount(getName()) > 0;
} }
protected boolean isUsingAmmo() public boolean isUsingAmmo()
{ {
return _ammo != null; return _ammo != null;
} }