-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trying to add custom block mining speed
- Loading branch information
Showing
6 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/xyz/prismenetwork/kelpmodloader/Block/BlockUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package xyz.prismenetwork.kelpmodloader.Block; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.BlockFace; | ||
import org.bukkit.block.data.MultipleFacing; | ||
|
||
public class BlockUtils { | ||
public static boolean parseBool(String s) { | ||
if (s.equalsIgnoreCase("1")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public static String parseString(boolean b) { | ||
if (b) { | ||
return "1"; | ||
} else { | ||
return "0"; | ||
} | ||
} | ||
|
||
public static int idFromBlock(Block block) { | ||
if (block.getType().equals(Material.BROWN_MUSHROOM_BLOCK)) { | ||
MultipleFacing facing = (MultipleFacing) block.getBlockData(); | ||
boolean DOWN = facing.hasFace(BlockFace.DOWN); | ||
boolean UP = facing.hasFace(BlockFace.UP); | ||
boolean EAST = facing.hasFace(BlockFace.EAST); | ||
boolean NORTH = facing.hasFace(BlockFace.NORTH); | ||
boolean SOUTH = facing.hasFace(BlockFace.SOUTH); | ||
boolean WEST = facing.hasFace(BlockFace.WEST); | ||
|
||
return Integer.parseInt(parseString(DOWN) + parseString(UP) + parseString(EAST) + parseString(NORTH) + parseString(SOUTH) + parseString(WEST), 2); | ||
} | ||
return 0; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/xyz/prismenetwork/kelpmodloader/Block/BreakHandler/BlockBreakEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package xyz.prismenetwork.kelpmodloader.Block.BreakHandler; | ||
|
||
import com.comphenix.protocol.PacketType; | ||
import com.comphenix.protocol.ProtocolLib; | ||
import com.comphenix.protocol.ProtocolLibrary; | ||
import com.comphenix.protocol.ProtocolManager; | ||
import com.comphenix.protocol.events.PacketContainer; | ||
import com.comphenix.protocol.wrappers.BlockPosition; | ||
import org.bukkit.GameMode; | ||
import org.bukkit.Material; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.Action; | ||
import org.bukkit.event.block.BlockDamageAbortEvent; | ||
import org.bukkit.event.block.BlockDamageEvent; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import xyz.prismenetwork.kelpmodloader.Block.BlockUtils; | ||
import xyz.prismenetwork.kelpmodloader.Block.ModdedBlock; | ||
import xyz.prismenetwork.kelpmodloader.ModsAPI.ItemUtils; | ||
import xyz.prismenetwork.kelpmodloader.Utils; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class BlockBreakEvent implements Listener { | ||
@EventHandler | ||
public void onBreak(BlockDamageEvent event) { | ||
if (event.getBlock().getType().equals(Material.BROWN_MUSHROOM_BLOCK)) { | ||
if (BlockUtils.idFromBlock(event.getBlock()) != 63) { | ||
event.setCancelled(true); | ||
Block block = event.getBlock(); | ||
|
||
BreakManager.PlayerBreakingBlock.put(event.getPlayer(), block); | ||
BreakManager.PlayerBreakingTime.put(event.getPlayer(), Instant.now().toEpochMilli()); | ||
} | ||
} else { | ||
if (BreakManager.PlayerBreakingBlock.containsKey(event.getPlayer())) { | ||
event.getPlayer().sendBlockDamage(BreakManager.PlayerBreakingBlock.get(event.getPlayer()).getLocation(), 0); | ||
BreakManager.PlayerBreakingBlock.remove(event.getPlayer()); | ||
} | ||
} | ||
} | ||
@EventHandler | ||
public void onAbort(BlockDamageAbortEvent event) { | ||
if (BreakManager.PlayerBreakingBlock.containsKey(event.getPlayer())) { | ||
BreakManager.PlayerBreakingBlock.remove(event.getPlayer()); | ||
if (!event.getPlayer().getGameMode().equals(GameMode.CREATIVE)) { | ||
event.getPlayer().setGameMode(GameMode.SURVIVAL); | ||
} | ||
} | ||
if (BreakManager.PlayerBreakingTime.containsKey(event.getPlayer())) { | ||
BreakManager.PlayerBreakingTime.remove(event.getPlayer()); | ||
} | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/xyz/prismenetwork/kelpmodloader/Block/BreakHandler/BreakManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package xyz.prismenetwork.kelpmodloader.Block.BreakHandler; | ||
|
||
import com.comphenix.protocol.PacketType; | ||
import com.comphenix.protocol.ProtocolLibrary; | ||
import com.comphenix.protocol.ProtocolManager; | ||
import com.comphenix.protocol.events.*; | ||
import com.comphenix.protocol.wrappers.BlockPosition; | ||
import org.bukkit.GameMode; | ||
import org.bukkit.attribute.Attribute; | ||
import org.bukkit.attribute.AttributeModifier; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.EquipmentSlot; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.potion.PotionEffect; | ||
import org.bukkit.potion.PotionEffectType; | ||
import org.bukkit.scheduler.BukkitScheduler; | ||
import xyz.prismenetwork.kelpmodloader.Block.BlockUtils; | ||
import xyz.prismenetwork.kelpmodloader.Block.ModdedBlock; | ||
import xyz.prismenetwork.kelpmodloader.Constant; | ||
import xyz.prismenetwork.kelpmodloader.KelpModLoader; | ||
|
||
import javax.swing.plaf.nimbus.State; | ||
import java.time.Instant; | ||
import java.time.temporal.TemporalField; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
public class BreakManager { | ||
public static HashMap<Player, Block> PlayerBreakingBlock = new HashMap<>(); | ||
public static HashMap<Player, Long> PlayerBreakingTime = new HashMap<>(); | ||
public static void Setup() { | ||
BukkitScheduler scheduler = KelpModLoader.getInstance.getServer().getScheduler(); | ||
ProtocolManager manager = ProtocolLibrary.getProtocolManager(); | ||
|
||
//manager.addPacketListener(new PacketAdapter(KelpModLoader.getInstance, ListenerPriority.HIGHEST, PacketType.Play.Server.BLOCK_BREAK_ANIMATION){ | ||
// @Override | ||
// public void onPacketSending(PacketEvent event) { | ||
// event.setCancelled(true); | ||
// } | ||
//}); | ||
|
||
scheduler.scheduleSyncRepeatingTask(KelpModLoader.getInstance, new Runnable() { | ||
@Override | ||
public void run() { | ||
PlayerBreakingBlock.forEach((p, b) -> { | ||
p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 5+((p.getPing()/40)), 200, true, false)); | ||
float breakstage = 0; | ||
|
||
ModdedBlock[] block = {new ModdedBlock()}; | ||
|
||
Constant.Blocks.forEach(blck -> { | ||
if (blck.getId() == BlockUtils.idFromBlock(b)) { | ||
block[0] = blck; | ||
} | ||
}); | ||
|
||
breakstage = ((Instant.now().toEpochMilli() - PlayerBreakingTime.get(p))*1000)/20f; | ||
|
||
PacketContainer container = new PacketContainer(PacketType.Play.Server.BLOCK_BREAK_ANIMATION); | ||
container.getIntegers().write(0, p.getEntityId()*1000); | ||
container.getIntegers().write(1, Math.round(breakstage)); | ||
container.getBlockPositionModifier().write(0, new BlockPosition(b.getX(), b.getY(), b.getZ())); | ||
|
||
if (breakstage > block[0].getBreakTime()) { | ||
b.breakNaturally(); | ||
} | ||
|
||
ProtocolLibrary.getProtocolManager().sendServerPacket(p, container); | ||
}); | ||
} | ||
}, 0L, 0L); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters