-
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.
fix: check if block break was cancelled before continuing
- Loading branch information
1 parent
da47424
commit eea41c7
Showing
2 changed files
with
194 additions
and
194 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,76 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.triassic</groupId> | ||
<artifactId>RandomCart</artifactId> | ||
<version>1.0.2</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>RandomCart</name> | ||
|
||
<description>Summons a Minecart randomly with loot inside of it when a block is broken.</description> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<defaultGoal>clean package</defaultGoal> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.2.4</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<createDependencyReducedPom>false</createDependencyReducedPom> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spigotmc-repo</id> | ||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<repository> | ||
<id>sonatype</id> | ||
<url>https://oss.sonatype.org/content/groups/public/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.13-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.triassic</groupId> | ||
<artifactId>RandomCart</artifactId> | ||
<version>1.0.3</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>RandomCart</name> | ||
|
||
<description>Summons a Minecart randomly with loot inside of it when a block is broken.</description> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<defaultGoal>clean package</defaultGoal> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.11.0</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.5.0</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<createDependencyReducedPom>false</createDependencyReducedPom> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spigotmc-repo</id> | ||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<repository> | ||
<id>sonatype</id> | ||
<url>https://oss.sonatype.org/content/groups/public/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.13-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
236 changes: 118 additions & 118 deletions
236
src/main/java/com/triassic/randomcart/listeners/BlockBreakListener.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 |
---|---|---|
@@ -1,119 +1,119 @@ | ||
package com.triassic.randomcart.listeners; | ||
|
||
import com.triassic.randomcart.RandomCart; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.configuration.Configuration; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.enchantments.Enchantment; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.minecart.StorageMinecart; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockBreakEvent; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.logging.Logger; | ||
|
||
public class BlockBreakListener implements Listener { | ||
private final Logger logger; | ||
private final Random random; | ||
private final Configuration config; | ||
private final List<Material> allowedBlocks; | ||
private final int summonChance; | ||
|
||
public BlockBreakListener(Configuration config, Logger logger, Random random) { | ||
this.config = config; | ||
this.logger = logger; | ||
this.random = random; | ||
this.allowedBlocks = RandomCart.loadAllowedBlocks(config, logger); | ||
this.summonChance = config.getInt("randomcart.chance"); | ||
} | ||
|
||
@EventHandler | ||
public void onBlockBreak(BlockBreakEvent event) { | ||
Block block = event.getBlock(); | ||
if (!allowedBlocks.contains(block.getType())) { | ||
return; | ||
} | ||
|
||
if (random.nextInt(100) > summonChance) { | ||
return; | ||
} | ||
|
||
RandomCart.Minecart minecartData = RandomCart.loadMinecart(config, random); | ||
|
||
if (minecartData == null) { | ||
return; | ||
} | ||
|
||
String selectedMinecart = minecartData.getSelectedMinecart(); | ||
|
||
Location blockLocation = block.getLocation(); | ||
blockLocation.setY(blockLocation.getY() + 0.75); | ||
|
||
StorageMinecart chestMinecart = (StorageMinecart) block.getWorld().spawnEntity(blockLocation, EntityType.MINECART_CHEST); | ||
Inventory chestInventory = chestMinecart.getInventory(); | ||
|
||
String chestName = ChatColor.translateAlternateColorCodes('&', minecartData.getName()); | ||
chestMinecart.setCustomName(chestName); | ||
|
||
for (String itemSlot : minecartData.getItems()) { | ||
String materialName = config.getString("minecarts." + selectedMinecart + ".items." + itemSlot + ".material"); | ||
|
||
Material material = Material.getMaterial(materialName); | ||
if (material == null) { | ||
logger.warning("Invalid item name " + materialName + " in " + selectedMinecart); | ||
continue; | ||
} | ||
|
||
String displayName = config.getString("minecarts." + selectedMinecart + ".items." + itemSlot + ".display-name"); | ||
int stackSize = config.getInt("minecarts." + selectedMinecart + ".items." + itemSlot + ".amount"); | ||
List<String> lore = config.getStringList("minecarts." + selectedMinecart + ".items." + itemSlot + ".lore"); | ||
ConfigurationSection enchantmentsSection = config.getConfigurationSection("minecarts." + selectedMinecart + ".items." + itemSlot + ".enchantments"); | ||
|
||
if (stackSize == 0) { | ||
stackSize = 1; | ||
} | ||
|
||
ItemStack item = new ItemStack(material, stackSize); | ||
ItemMeta meta = item.getItemMeta(); | ||
|
||
if (displayName != null) { | ||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName)); | ||
} | ||
|
||
if (lore != null) { | ||
List<String> coloredLore = new ArrayList<>(); | ||
for (String line : lore) { | ||
coloredLore.add(ChatColor.translateAlternateColorCodes('&', line)); | ||
} | ||
|
||
meta.setLore(coloredLore); | ||
} | ||
|
||
if (enchantmentsSection != null) { | ||
for (String ench : enchantmentsSection.getKeys(false)) { | ||
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(ench.toLowerCase())); | ||
int level = enchantmentsSection.getInt(ench + ".level"); | ||
try { | ||
meta.addEnchant(enchantment, level, true); | ||
} catch (IllegalArgumentException e) { | ||
logger.warning("Invalid enchantment name " + ench + " in " + selectedMinecart); | ||
} | ||
} | ||
} | ||
|
||
item.setItemMeta(meta); | ||
chestInventory.setItem(Integer.parseInt(itemSlot), item); | ||
} | ||
} | ||
package com.triassic.randomcart.listeners; | ||
|
||
import com.triassic.randomcart.RandomCart; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.configuration.Configuration; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.enchantments.Enchantment; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.minecart.StorageMinecart; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockBreakEvent; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.logging.Logger; | ||
|
||
public class BlockBreakListener implements Listener { | ||
private final Logger logger; | ||
private final Random random; | ||
private final Configuration config; | ||
private final List<Material> allowedBlocks; | ||
private final int summonChance; | ||
|
||
public BlockBreakListener(Configuration config, Logger logger, Random random) { | ||
this.config = config; | ||
this.logger = logger; | ||
this.random = random; | ||
this.allowedBlocks = RandomCart.loadAllowedBlocks(config, logger); | ||
this.summonChance = config.getInt("randomcart.chance"); | ||
} | ||
|
||
@EventHandler | ||
public void onBlockBreak(BlockBreakEvent event) { | ||
Block block = event.getBlock(); | ||
if (!allowedBlocks.contains(block.getType()) || event.isCancelled()) { | ||
return; | ||
} | ||
|
||
if (random.nextInt(100) > summonChance) { | ||
return; | ||
} | ||
|
||
RandomCart.Minecart minecartData = RandomCart.loadMinecart(config, random); | ||
|
||
if (minecartData == null) { | ||
return; | ||
} | ||
|
||
String selectedMinecart = minecartData.getSelectedMinecart(); | ||
|
||
Location blockLocation = block.getLocation(); | ||
blockLocation.setY(blockLocation.getY() + 0.75); | ||
|
||
StorageMinecart chestMinecart = (StorageMinecart) block.getWorld().spawnEntity(blockLocation, EntityType.MINECART_CHEST); | ||
Inventory chestInventory = chestMinecart.getInventory(); | ||
|
||
String chestName = ChatColor.translateAlternateColorCodes('&', minecartData.getName()); | ||
chestMinecart.setCustomName(chestName); | ||
|
||
for (String itemSlot : minecartData.getItems()) { | ||
String materialName = config.getString("minecarts." + selectedMinecart + ".items." + itemSlot + ".material"); | ||
|
||
Material material = Material.getMaterial(materialName); | ||
if (material == null) { | ||
logger.warning("Invalid item name " + materialName + " in " + selectedMinecart); | ||
continue; | ||
} | ||
|
||
String displayName = config.getString("minecarts." + selectedMinecart + ".items." + itemSlot + ".display-name"); | ||
int stackSize = config.getInt("minecarts." + selectedMinecart + ".items." + itemSlot + ".amount"); | ||
List<String> lore = config.getStringList("minecarts." + selectedMinecart + ".items." + itemSlot + ".lore"); | ||
ConfigurationSection enchantmentsSection = config.getConfigurationSection("minecarts." + selectedMinecart + ".items." + itemSlot + ".enchantments"); | ||
|
||
if (stackSize == 0) { | ||
stackSize = 1; | ||
} | ||
|
||
ItemStack item = new ItemStack(material, stackSize); | ||
ItemMeta meta = item.getItemMeta(); | ||
|
||
if (displayName != null) { | ||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName)); | ||
} | ||
|
||
if (lore != null) { | ||
List<String> coloredLore = new ArrayList<>(); | ||
for (String line : lore) { | ||
coloredLore.add(ChatColor.translateAlternateColorCodes('&', line)); | ||
} | ||
|
||
meta.setLore(coloredLore); | ||
} | ||
|
||
if (enchantmentsSection != null) { | ||
for (String ench : enchantmentsSection.getKeys(false)) { | ||
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(ench.toLowerCase())); | ||
int level = enchantmentsSection.getInt(ench + ".level"); | ||
try { | ||
meta.addEnchant(enchantment, level, true); | ||
} catch (IllegalArgumentException e) { | ||
logger.warning("Invalid enchantment name " + ench + " in " + selectedMinecart); | ||
} | ||
} | ||
} | ||
|
||
item.setItemMeta(meta); | ||
chestInventory.setItem(Integer.parseInt(itemSlot), item); | ||
} | ||
} | ||
} |