Add ClansLoginManager, TNT Generators (Still needs persistence)

This commit is contained in:
Shaun Bennett 2015-08-29 13:35:31 -07:00
parent 676817601a
commit fae27df496
10 changed files with 464 additions and 71 deletions

View File

@ -7,6 +7,7 @@ import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.util.Vector;
public class UtilWorld
@ -46,6 +47,54 @@ public class UtilWorld
}
}
public static String blockToStr(Block block)
{
if (block == null)
return "";
return block.getWorld().getName() + "," +
block.getX() + "," +
block.getY() + "," +
block.getZ();
}
public static String blockToStrClean(Block block)
{
if (block == null)
return "";
return "(" + block.getX() + "," +
block.getY() + "," +
block.getZ() + ")";
}
public static Block strToBlock(String string)
{
if (string.length() == 0)
return null;
String[] parts = string.split(",");
try
{
for (World cur : UtilServer.getServer().getWorlds())
{
if (cur.getName().equalsIgnoreCase(parts[0]))
{
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[1]);
int z = Integer.parseInt(parts[1]);
cur.getBlockAt(x, y, z);
}
}
}
catch (Exception e)
{
}
return null;
}
public static String locToStr(Location loc)
{
if (loc == null)

View File

@ -26,6 +26,7 @@ import mineplex.game.clans.clans.repository.tokens.ClanEnemyToken;
import mineplex.game.clans.clans.repository.tokens.ClanMemberToken;
import mineplex.game.clans.clans.repository.tokens.ClanTerritoryToken;
import mineplex.game.clans.clans.repository.tokens.ClanToken;
import mineplex.game.clans.clans.tntGenerator.TntGenerator;
public class ClanInfo
{
@ -47,6 +48,8 @@ public class ClanInfo
private Timestamp _dateCreated;
private Timestamp _lastOnline = null;
private TntGenerator _generator;
// Loaded from Client
private NautHashMap<String, ClanRole> _memberMap = new NautHashMap<String, ClanRole>();
private NautHashMap<String, Boolean> _allyMap = new NautHashMap<String, Boolean>();
@ -433,6 +436,16 @@ public class ClanInfo
TimeUnit.FIT) + " to Protection";
}
public TntGenerator getGenerator()
{
return _generator;
}
public void setGenerator(TntGenerator generator)
{
_generator = generator;
}
public boolean isAdmin()
{
return _admin;

View File

@ -2,17 +2,20 @@ package mineplex.game.clans.clans;
import java.sql.Timestamp;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.sun.org.apache.xpath.internal.operations.Bool;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilWorld;
import mineplex.game.clans.clans.event.ClanDeleteEvent;
import mineplex.game.clans.clans.repository.ClanRepository;
import mineplex.game.clans.clans.repository.ClanTerritory;
import mineplex.game.clans.clans.repository.tokens.ClanEnemyToken;
@ -39,36 +42,46 @@ public class ClansDataAccessLayer
@Override
public void run()
{
boolean out = _repository.deleteClan(clan.getId());
final boolean out = _repository.deleteClan(clan.getId());
if (out)
runSync(new Runnable()
{
//Territory Unclaim
for (String cur : clan.getClaimSet())
_manager.getClaimMap().remove(cur);
_manager.getClanMap().remove(clan.getName());
for (String cur : clan.getMembers().keySet())
@Override
public void run()
{
_manager.getClanMemberMap().remove(cur);
if (out)
{
//Territory Unclaim
for (String cur : clan.getClaimSet())
_manager.getClaimMap().remove(cur);
_manager.getClanMap().remove(clan.getName());
for (String cur : clan.getMembers().keySet())
{
_manager.getClanMemberMap().remove(cur);
}
//Clean from Others
for (ClanInfo cur : _manager.getClanMap().values())
{
cur.getAllyMap().remove(clan.getName());
cur.getRequestMap().remove(clan.getName());
}
//Scoreboard
_scoreboard.refresh(clan);
//Log
_manager.log("Deleted [" + clan.getName() + "].");
ClanDeleteEvent event = new ClanDeleteEvent(clan);
Bukkit.getServer().getPluginManager().callEvent(event);
}
if (callback != null) callback.run(out);
}
//Clean from Others
for (ClanInfo cur : _manager.getClanMap().values())
{
cur.getAllyMap().remove(clan.getName());
cur.getRequestMap().remove(clan.getName());
}
//Scoreboard
_scoreboard.refresh(clan);
//Log
_manager.log("Deleted [" + clan.getName() + "].");
}
if (callback != null) callback.run(out);
});
}
});
}

View File

@ -80,7 +80,7 @@ public class ClansGame extends MiniPlugin
}
}
@EventHandler(priority = EventPriority.LOW)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void SkillTrigger(SkillTriggerEvent event)
{
if (!Clans.getClanUtility().isSafe(event.GetPlayer()))

View File

@ -6,6 +6,7 @@ import java.util.TimeZone;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_7_R4.util.CraftMagicNumbers;
import org.bukkit.entity.Player;
@ -38,6 +39,7 @@ import mineplex.core.common.util.F;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.creature.Creature;
import mineplex.core.creature.event.CreatureSpawnCustomEvent;
import mineplex.core.disguise.DisguiseManager;
@ -69,6 +71,7 @@ import mineplex.game.clans.clans.repository.tokens.ClanMemberToken;
import mineplex.game.clans.clans.repository.tokens.ClanTerritoryToken;
import mineplex.game.clans.clans.repository.tokens.ClanToken;
import mineplex.game.clans.clans.scoreboard.ClansScoreboardManager;
import mineplex.game.clans.clans.tntGenerator.TntGeneratorManager;
import mineplex.game.clans.clans.war.WarManager;
import mineplex.game.clans.economy.GoldManager;
import mineplex.game.clans.clans.worldevent.WorldEventManager;
@ -181,6 +184,7 @@ public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelat
_clanGame = new ClansGame(plugin, this);
_clanUtility = new ClansUtility(this);
_itemMapManager = new ItemMapManager(this, _worldEvent);
new TntGeneratorManager(plugin, this);
_explosion = new Explosion(plugin, blockRestore);
@ -726,19 +730,6 @@ public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelat
}
@EventHandler
public void onBreak(BlockBreakEvent event)
{
if (event.getPlayer().getName().equals("Eniloo"))
{
ItemStack item = event.getPlayer().getItemInHand();
event.getPlayer().sendMessage("Item: " + item.getType().toString());
event.getPlayer().sendMessage("Id: " + item.getTypeId());
event.getPlayer().sendMessage("Durability: " + item.getDurability());
event.getPlayer().sendMessage("Data: " + ((CraftItemStack) item).getHandle().getData());
}
}
// @EventHandler
public void blockBreak(BlockBreakEvent event)
{

View File

@ -15,6 +15,7 @@ import org.jooq.impl.DSL;
public class ClansLoginManager extends MiniPlugin implements ILoginProcessor
{
private boolean _enabled;
private String _serverName;
public ClansLoginManager(JavaPlugin plugin, CoreClientManager clientManager, String serverName)
@ -22,6 +23,7 @@ public class ClansLoginManager extends MiniPlugin implements ILoginProcessor
super("Clans Login Manager", plugin);
_serverName = serverName;
_enabled = false;
clientManager.addStoredProcedureLoginProcessor(this);
}
@ -34,27 +36,30 @@ public class ClansLoginManager extends MiniPlugin implements ILoginProcessor
@Override
public void processLoginResultSet(final String playerName, int accountId, ResultSet resultSet) throws SQLException
{
while (resultSet.next())
if (_enabled)
{
final String serverName = resultSet.getString(1);
if (serverName != null)
while (resultSet.next())
{
System.out.println("Player " + playerName + " is from server: " + serverName);
final String serverName = resultSet.getString(1);
if (!serverName.equals(_serverName))
if (serverName != null)
{
runSyncLater(new Runnable()
System.out.println("Player " + playerName + " is from server: " + serverName);
if (!serverName.equals(_serverName))
{
@Override
public void run()
runSyncLater(new Runnable()
{
final Player player = UtilPlayer.searchExact(playerName);
if (player != null)
kickPlayer(player, serverName);
}
}, 20);
@Override
public void run()
{
final Player player = UtilPlayer.searchExact(playerName);
if (player != null)
kickPlayer(player, serverName);
}
}, 20);
}
}
}
}

View File

@ -0,0 +1,37 @@
package mineplex.game.clans.clans.event;
import mineplex.core.gadget.types.Gadget;
import mineplex.core.mount.Mount;
import mineplex.game.clans.clans.ClanInfo;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ClanDeleteEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
private ClanInfo _clanInfo;
public ClanDeleteEvent(ClanInfo clanInfo)
{
_clanInfo = clanInfo;
}
public ClanInfo getClanInfo()
{
return _clanInfo;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
}

View File

@ -0,0 +1,45 @@
package mineplex.game.clans.clans.tntGenerator;
import org.bukkit.block.Block;
public class TntGenerator
{
private Block _block;
private int _ticks;
private int _stock;
public TntGenerator(Block block)
{
_block = block;
}
public int getTicks()
{
return _ticks;
}
public void setTicks(int ticks)
{
_ticks = ticks;
}
public void incrementTicks()
{
_ticks++;
}
public int getStock()
{
return _stock;
}
public void setStock(int stock)
{
_stock = stock;
}
public Block getBlock()
{
return _block;
}
}

View File

@ -0,0 +1,255 @@
package mineplex.game.clans.clans.tntGenerator;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import net.minecraft.server.v1_7_R4.TileEntityBrewingStand;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilTextMiddle;
import mineplex.core.common.util.UtilTime;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.event.ClanDeleteEvent;
public class TntGeneratorManager extends MiniPlugin
{
public static final Material GENERATOR_MATERIAL = Material.BREWING_STAND;
public static final Material GENERATOR_DROP_MATERIAL = Material.BREWING_STAND_ITEM;
public static final int TICKS_PER_TNT = 60 * 60 * 10; // 10 Hours
public static final int MAX_GENERATOR_STOCK = 3;
private ClansManager _clansManager;
public TntGeneratorManager(JavaPlugin plugin, ClansManager clansManager)
{
super("Tnt Generator", plugin);
_clansManager = clansManager;
}
@EventHandler
public void updateGenerators(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
return;
for (ClanInfo clanInfo : _clansManager.getClanMap().values())
{
TntGenerator generator = clanInfo.getGenerator();
if (generator != null)
{
// Check that generator still exists
if (generator.getBlock().getType() != GENERATOR_MATERIAL)
{
clanInfo.setGenerator(null);
continue;
}
// Check that generator is still in clan territory
ClanInfo owner = _clansManager.getClanUtility().getOwner(generator.getBlock().getLocation());
if (!clanInfo.equals(owner))
{
_clansManager.messageClan(clanInfo, F.main("Clans", "Your " + F.elem("TNT Generator") + " has been destroyed due to the land no longer being claimed"));
destroyGenerator(generator);
clanInfo.setGenerator(null);
continue;
}
if (generator.getStock() >= MAX_GENERATOR_STOCK)
{
generator.setTicks(0);
}
else
{
if (generator.getTicks() >= TICKS_PER_TNT)
{
_clansManager.messageClan(clanInfo, F.main("Clans", "Your " + F.elem("TNT Generator") + " has a new TNT available"));
generator.setStock(generator.getStock() + 1);
generator.setTicks(0);
updateBrewingStand(generator);
}
generator.incrementTicks();
}
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlace(BlockPlaceEvent event)
{
if (event.getBlockPlaced().getType() == GENERATOR_MATERIAL)
{
ClanInfo clan = _clansManager.getClan(event.getPlayer());
if (clan == null)
{
UtilPlayer.message(event.getPlayer(), F.main("Clans", "You must be in a clan to place a " + F.elem("TNT Generator")));
event.setCancelled(true);
return;
}
// Confirm they are placing in their own land
ClanInfo owner = _clansManager.getClanUtility().getOwner(event.getBlock().getLocation());
if (!clan.equals(owner))
{
UtilPlayer.message(event.getPlayer(), F.main("Clans", "You must place the " + F.elem("TNT Generator") + " on your own land"));
event.setCancelled(true);
return;
}
TntGenerator oldGenerator = clan.getGenerator();
if (oldGenerator != null)
{
UtilPlayer.message(event.getPlayer(), F.main("Clans", "Your clan already has a " + F.elem("TNT Generator")));
event.setCancelled(true);
return;
}
// event.getBlockPlaced().setType(GENERATOR_MATERIAL);
TntGenerator generator = new TntGenerator(event.getBlock());
clan.setGenerator(generator);
_clansManager.messageClan(clan, F.main("Clans", F.name(event.getPlayer().getName()) + " placed a " + F.elem("TNT Generator") + " at " + F.elem(UtilWorld.blockToStrClean(event.getBlock()))));
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBreak(BlockBreakEvent event)
{
if (event.getBlock() != null && event.getBlock().getType() == GENERATOR_MATERIAL)
{
ClanInfo clan = getGenerator(event.getBlock());
if (clan != null)
{
destroyGenerator(clan.getGenerator());
clan.setGenerator(null);
_clansManager.messageClan(clan, F.main("Clans", "Your " + F.elem("TNT Generator") + " has been destroyed by " + F.elem(event.getPlayer().getName())));
}
clearStand(event.getBlock());
event.getBlock().setTypeId(0);
event.setCancelled(true);
}
}
@EventHandler
public void onInteract(PlayerInteractEvent event)
{
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null && event.getClickedBlock().getType() == GENERATOR_MATERIAL)
{
ClanInfo clan = getGenerator(event.getClickedBlock());
if (clan != null)
{
TntGenerator generator = clan.getGenerator();
int stock = generator.getStock();
if (stock > 0)
{
generator.setStock(stock - 1);
updateBrewingStand(generator);
dropItem(event.getClickedBlock(), new ItemStack(Material.TNT));
}
else
{
int secondLeft = TICKS_PER_TNT - generator.getTicks();
int msLeft = secondLeft * 1000;
UtilTextMiddle.display("", C.cRed + "Next TNT in " + C.cGold + UtilTime.convertString(msLeft, 1, UtilTime.TimeUnit.FIT), event.getPlayer());
}
}
event.setCancelled(true);
}
}
@EventHandler
public void onDelete(ClanDeleteEvent event)
{
ClanInfo clan = event.getClanInfo();
if (clan != null && clan.getGenerator() != null)
{
clearStand(clan.getGenerator().getBlock());
clan.getGenerator().getBlock().setTypeId(0);
}
}
private void dropItem(Block dropLocation, ItemStack item)
{
dropLocation.getWorld().dropItemNaturally(dropLocation.getLocation().add(0.5, 0.5, 0.5), item);
}
public ClanInfo getGenerator(Block block)
{
for (ClanInfo clanInfo : _clansManager.getClanMap().values())
{
TntGenerator generator = clanInfo.getGenerator();
if (generator != null && block.equals(generator.getBlock()))
return clanInfo;
}
return null;
}
private void destroyGenerator(TntGenerator generator)
{
for (int i = 0; i < generator.getStock(); i++)
{
dropItem(generator.getBlock(), new ItemStack(Material.TNT));
}
generator.setStock(0);
dropItem(generator.getBlock(), new ItemStack(GENERATOR_DROP_MATERIAL));
clearStand(generator.getBlock());
generator.getBlock().setTypeId(0);
}
private void clearStand(Block block)
{
TileEntityBrewingStand tileEntity = (TileEntityBrewingStand)((CraftWorld)block.getWorld()).getTileEntityAt(block.getX(), block.getY(), block.getZ());
for (int i = 0; i < 4; i++)
{
tileEntity.items[i] = null;
}
}
private void updateBrewingStand(TntGenerator generator)
{
Block block = generator.getBlock();
if (block != null && block.getType() == Material.BREWING_STAND)
{
TileEntityBrewingStand tileEntity = (TileEntityBrewingStand)((CraftWorld)block.getWorld()).getTileEntityAt(block.getX(), block.getY(), block.getZ());
for (int i = 0; i < 3; i++)
{
ItemStack item = new ItemStack(Material.TNT);
net.minecraft.server.v1_7_R4.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
int itemSlot = i;
tileEntity.items[itemSlot] = (generator.getStock() > i) ? nmsItem : null;
}
tileEntity.update();
}
}
}

View File

@ -332,21 +332,6 @@ public class Gameplay extends MiniPlugin
event.setCancelled(true);
}
@EventHandler
public void BrewingBreak(BlockBreakEvent event)
{
if (event.isCancelled())
return;
if (event.getBlock().getType() != Material.BREWING_STAND)
return;
event.setCancelled(true);
event.getBlock().setTypeId(0);
event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation().add(0.5, 0.5, 0.5), ItemStackFactory.Instance.CreateStack(Material.BREWING_STAND));
}
@EventHandler
public void AnvilDisable(PlayerInteractEvent event)
{