2018-04-18 03:01:53 +02:00
package com.massivecraft.factions.cmd ;
2018-10-23 01:42:57 +02:00
import com.massivecraft.factions.SavageFactions ;
2018-04-18 03:01:53 +02:00
import com.massivecraft.factions.struct.Permission ;
import com.massivecraft.factions.struct.Role ;
2018-04-25 02:32:26 +02:00
import com.massivecraft.factions.zcore.fperms.Access ;
import com.massivecraft.factions.zcore.fperms.PermissableAction ;
2018-04-18 03:01:53 +02:00
import com.massivecraft.factions.zcore.util.TL ;
import org.bukkit.Location ;
import org.bukkit.Material ;
import org.bukkit.block.Dispenser ;
import org.bukkit.entity.Player ;
import org.bukkit.inventory.Inventory ;
import org.bukkit.inventory.ItemStack ;
import org.bukkit.inventory.PlayerInventory ;
public class CmdTntFill extends FCommand {
2018-07-12 18:11:07 +02:00
public CmdTntFill ( ) {
2018-04-18 03:01:53 +02:00
super ( ) ;
this . aliases . add ( " tntfill " ) ;
this . requiredArgs . add ( " radius " ) ;
this . requiredArgs . add ( " amount " ) ;
this . permission = Permission . TNTFILL . node ;
this . disableOnLock = true ;
senderMustBePlayer = true ;
senderMustBeMember = true ;
senderMustBeModerator = false ;
senderMustBeAdmin = false ;
2018-12-21 22:46:10 +01:00
2018-04-18 03:01:53 +02:00
}
@Override
2018-07-12 18:11:07 +02:00
public void perform ( ) {
2019-02-26 19:01:27 +01:00
if ( ! SavageFactions . plugin . getConfig ( ) . getBoolean ( " Tntfill.enabled " ) ) {
this . fme . msg ( TL . GENERIC_DISABLED ) ;
return ;
}
2018-11-07 17:14:42 +01:00
if ( ! fme . isAdminBypassing ( ) ) {
2018-08-02 19:55:21 +02:00
Access access = myFaction . getAccess ( fme , PermissableAction . TNTFILL ) ;
2018-11-07 06:38:43 +01:00
if ( access ! = Access . ALLOW & & fme . getRole ( ) ! = Role . LEADER ) {
2018-08-02 19:55:21 +02:00
fme . msg ( TL . GENERIC_FPERM_NOPERMISSION , " use tnt fill " ) ;
return ;
}
2018-04-25 02:32:26 +02:00
}
2018-04-18 03:06:14 +02:00
msg ( TL . COMMAND_TNTFILL_HEADER ) ;
2018-07-12 18:11:07 +02:00
int radius = argAsInt ( 0 , 16 ) ;
int amount = argAsInt ( 1 , 16 ) ;
2018-11-07 06:38:43 +01:00
if ( radius > SavageFactions . plugin . getConfig ( ) . getInt ( " Tntfill.max-radius " ) ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNTFILL_RADIUSMAX . toString ( ) . replace ( " {max} " , SavageFactions . plugin . getConfig ( ) . getInt ( " Tntfill.max-radius " ) + " " ) ) ;
2018-04-18 03:01:53 +02:00
return ;
}
2018-11-07 06:38:43 +01:00
if ( amount > SavageFactions . plugin . getConfig ( ) . getInt ( " Tntfill.max-amount " ) ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNTFILL_AMOUNTMAX . toString ( ) . replace ( " {max} " , SavageFactions . plugin . getConfig ( ) . getInt ( " Tntfill.max-amount " ) + " " ) ) ;
2018-04-18 03:01:53 +02:00
return ;
}
2019-02-11 05:57:45 +01:00
2018-04-18 03:01:53 +02:00
try {
2018-11-20 20:51:07 +01:00
Integer . parseInt ( args . get ( 1 ) ) ;
2018-04-18 03:01:53 +02:00
} catch ( NumberFormatException e ) {
fme . msg ( TL . COMMAND_TNT_INVALID_NUM ) ;
return ;
}
if ( amount < 0 ) {
fme . msg ( TL . COMMAND_TNT_POSITIVE ) ;
return ;
}
boolean bankMode = fme . getRole ( ) . isAtLeast ( Role . MODERATOR ) ;
Location start = me . getLocation ( ) ;
2018-04-25 02:32:26 +02:00
int counter = 0 ;
2018-04-18 03:01:53 +02:00
for ( double x = start . getX ( ) - radius ; x < = start . getX ( ) + radius ; x + + ) {
for ( double y = start . getY ( ) - radius ; y < = start . getY ( ) + radius ; y + + ) {
for ( double z = start . getZ ( ) - radius ; z < = start . getZ ( ) + radius ; z + + ) {
Location blockLoc = new Location ( start . getWorld ( ) , x , y , z ) ;
2018-07-12 18:11:07 +02:00
if ( blockLoc . getBlock ( ) . getState ( ) instanceof Dispenser ) {
Dispenser disp = ( Dispenser ) blockLoc . getBlock ( ) . getState ( ) ;
Inventory dispenser = disp . getInventory ( ) ;
if ( canHold ( dispenser , amount ) ) {
int fullStacks = amount / 64 ;
int remainderAmt = amount % 64 ;
2018-11-07 17:14:42 +01:00
if ( ! inventoryContains ( me . getInventory ( ) , new ItemStack ( Material . TNT , amount ) ) ) {
if ( ! fme . getRole ( ) . isAtLeast ( Role . MODERATOR ) ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNTFILL_NOTENOUGH . toString ( ) ) ;
2018-07-12 18:11:07 +02:00
sendMessage ( TL . COMMAND_TNTFILL_SUCCESS . toString ( ) . replace ( " {amount} " , amount + " " ) . replace ( " {dispensers} " , counter + " " ) ) ;
me . updateInventory ( ) ;
return ;
} else if ( bankMode ) {
//msg(TL.COMMAND_TNTFILL_MOD.toString().replace("{role}",fme.getRole().nicename));
bankMode = true ;
removeFromBank ( amount ) ;
2018-11-07 17:14:42 +01:00
if ( ! inventoryContains ( me . getInventory ( ) , new ItemStack ( Material . TNT , amount ) ) ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNTFILL_NOTENOUGH . toString ( ) ) ;
2018-07-12 18:11:07 +02:00
sendMessage ( TL . COMMAND_TNTFILL_SUCCESS . toString ( ) . replace ( " {amount} " , amount + " " ) . replace ( " {dispensers} " , counter + " " ) ) ;
2018-04-25 02:32:26 +02:00
me . updateInventory ( ) ;
2018-04-18 03:01:53 +02:00
return ;
}
}
}
2018-07-12 18:11:07 +02:00
ItemStack tnt64 = new ItemStack ( Material . TNT , 64 ) ;
for ( int i = 0 ; i < = fullStacks - 1 ; i + + ) {
dispenser . addItem ( tnt64 ) ;
takeTnt ( 64 ) ;
}
if ( remainderAmt ! = 0 ) {
ItemStack tnt = new ItemStack ( Material . TNT , remainderAmt ) ;
dispenser . addItem ( tnt ) ;
takeTnt ( remainderAmt ) ;
}
//sendMessage(TL.COMMAND_TNTFILL_SUCCESS.toString().replace("{amount}",amount + "").replace("{x}",(int) x + "").replace("{y}",(int) y + "").replace("{z}",(int) z + ""));
counter + + ;
}
2018-04-18 03:01:53 +02:00
}
}
}
}
2018-04-25 02:32:26 +02:00
if ( bankMode ) {
2018-07-12 18:11:07 +02:00
msg ( TL . COMMAND_TNTFILL_MOD . toString ( ) . replace ( " {role} " , fme . getRole ( ) . nicename ) ) ;
2018-04-25 02:32:26 +02:00
}
2018-07-12 18:11:07 +02:00
sendMessage ( TL . COMMAND_TNTFILL_SUCCESS . toString ( ) . replace ( " {amount} " , amount + " " ) . replace ( " {dispensers} " , counter + " " ) ) ;
2018-04-25 02:32:26 +02:00
me . updateInventory ( ) ;
2018-04-18 03:01:53 +02:00
}
2018-04-25 02:32:26 +02:00
2018-07-12 18:11:07 +02:00
private void removeFromBank ( int amount ) {
2018-04-25 02:32:26 +02:00
try {
2018-11-20 20:51:07 +01:00
Integer . parseInt ( args . get ( 1 ) ) ;
2018-04-25 02:32:26 +02:00
} catch ( NumberFormatException e ) {
fme . msg ( TL . COMMAND_TNT_INVALID_NUM ) ;
return ;
}
if ( amount < 0 ) {
fme . msg ( TL . COMMAND_TNT_POSITIVE ) ;
return ;
}
if ( fme . getFaction ( ) . getTnt ( ) < amount ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNT_WIDTHDRAW_NOTENOUGH . toString ( ) ) ;
2018-04-25 02:32:26 +02:00
return ;
}
int fullStacks = amount / 64 ;
int remainderAmt = amount % 64 ;
if ( ( remainderAmt = = 0 & & getEmptySlots ( me ) < = fullStacks ) ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNT_WIDTHDRAW_NOTENOUGH . toString ( ) ) ;
2018-04-25 02:32:26 +02:00
return ;
}
if ( getEmptySlots ( me ) + 1 < = fullStacks ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNT_WIDTHDRAW_NOTENOUGH . toString ( ) ) ;
2018-04-25 02:32:26 +02:00
return ;
}
ItemStack tnt64 = new ItemStack ( Material . TNT , 64 ) ;
for ( int i = 0 ; i < = fullStacks - 1 ; i + + ) {
me . getInventory ( ) . addItem ( tnt64 ) ;
}
if ( remainderAmt ! = 0 ) {
ItemStack tnt = new ItemStack ( Material . TNT , remainderAmt ) ;
me . getInventory ( ) . addItem ( tnt ) ;
}
fme . getFaction ( ) . takeTnt ( amount ) ;
me . updateInventory ( ) ;
}
2018-07-12 18:11:07 +02:00
public void takeTnt ( int amount ) {
2018-04-18 03:01:53 +02:00
Inventory inv = me . getInventory ( ) ;
int invTnt = 0 ;
for ( int i = 0 ; i < = inv . getSize ( ) ; i + + ) {
if ( inv . getItem ( i ) = = null ) {
continue ;
}
if ( inv . getItem ( i ) . getType ( ) = = Material . TNT ) {
invTnt + = inv . getItem ( i ) . getAmount ( ) ;
}
}
if ( amount > invTnt ) {
fme . msg ( TL . COMMAND_TNTFILL_NOTENOUGH ) ;
return ;
}
ItemStack tnt = new ItemStack ( Material . TNT , amount ) ;
2018-11-07 06:38:43 +01:00
if ( fme . getFaction ( ) . getTnt ( ) + amount > SavageFactions . plugin . getConfig ( ) . getInt ( " ftnt.Bank-Limit " ) ) {
2018-04-18 03:01:53 +02:00
msg ( TL . COMMAND_TNT_EXCEEDLIMIT ) ;
return ;
}
removeFromInventory ( me . getInventory ( ) , tnt ) ;
}
2018-07-12 18:11:07 +02:00
public boolean canHold ( Inventory inventory , int amount ) {
2018-04-18 03:01:53 +02:00
int fullStacks = amount / 64 ;
int remainderAmt = amount % 64 ;
if ( ( remainderAmt = = 0 & & getEmptySlots ( me ) < = fullStacks ) ) {
return false ;
}
if ( getEmptySlots ( me ) + 1 < = fullStacks ) {
2018-12-21 03:15:10 +01:00
fme . msg ( TL . COMMAND_TNT_WIDTHDRAW_NOTENOUGH . toString ( ) ) ;
2018-04-18 03:01:53 +02:00
return false ;
}
return true ;
}
public boolean inventoryContains ( Inventory inventory , ItemStack item ) {
int count = 0 ;
ItemStack [ ] items = inventory . getContents ( ) ;
for ( int i = 0 ; i < items . length ; i + + ) {
if ( items [ i ] ! = null & & items [ i ] . getType ( ) = = item . getType ( ) & & items [ i ] . getDurability ( ) = = item . getDurability ( ) ) {
count + = items [ i ] . getAmount ( ) ;
}
if ( count > = item . getAmount ( ) ) {
return true ;
}
}
return false ;
}
public void removeFromInventory ( Inventory inventory , ItemStack item ) {
int amt = item . getAmount ( ) ;
ItemStack [ ] items = inventory . getContents ( ) ;
for ( int i = 0 ; i < items . length ; i + + ) {
if ( items [ i ] ! = null & & items [ i ] . getType ( ) = = item . getType ( ) & & items [ i ] . getDurability ( ) = = item . getDurability ( ) ) {
if ( items [ i ] . getAmount ( ) > amt ) {
items [ i ] . setAmount ( items [ i ] . getAmount ( ) - amt ) ;
break ;
} else if ( items [ i ] . getAmount ( ) = = amt ) {
items [ i ] = null ;
break ;
} else {
amt - = items [ i ] . getAmount ( ) ;
items [ i ] = null ;
}
}
}
inventory . setContents ( items ) ;
}
public int getEmptySlots ( Player p ) {
PlayerInventory inventory = p . getInventory ( ) ;
ItemStack [ ] cont = inventory . getContents ( ) ;
int i = 0 ;
for ( ItemStack item : cont )
if ( item ! = null & & item . getType ( ) ! = Material . AIR ) {
i + + ;
}
return 36 - i ;
}
@Override
public TL getUsageTranslation ( ) {
return TL . COMMAND_TNTFILL_DESCRIPTION ;
}
}