Skip to content
Snippets Groups Projects
Commit 465b1e65 authored by Tim's avatar Tim
Browse files

First test version

parents
Branches
No related tags found
No related merge requests found
pom.xml 0 → 100644
<?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>fsmpi.gaming.tnt-crusher</groupId>
<artifactId>tnt-crusher</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>plugin.yml</include>
</includes>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package fsmpi.gaming.tnt_crusher;
import org.bukkit.block.Block;
public interface BlockModifier {
public void modify(Block block);
}
package fsmpi.gaming.tnt_crusher;
import java.util.Random;
import org.bukkit.Material;
import org.bukkit.block.Block;
public class ChangeBlockModifier implements BlockModifier {
public Material checkFor;
public Material changeTo;
public float modifierChance;
public ChangeBlockModifier(Material checkFor, Material changeTo, float modifierChance) {
this.checkFor = checkFor;
this.changeTo = changeTo;
this.modifierChance = modifierChance;
}
@Override
public void modify(Block block) {
if (block.getType() == checkFor) {
Random r = new Random();
if (r.nextFloat() < modifierChance) {
block.setType(changeTo);
}
}
}
}
package fsmpi.gaming.tnt_crusher;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
public class ExplosionBlockHandler implements Listener {
private ArrayList<BlockModifier> modifiers;
public ExplosionBlockHandler(List<BlockModifier> modifiers) {
this.modifiers = new ArrayList<>(modifiers);
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
handleExplosion(event.getLocation(), event.blockList());
}
@EventHandler
public void onBlockExplode(BlockExplodeEvent event) {
handleExplosion(event.getBlock().getLocation(), event.blockList());
}
public void handleExplosion(Location location, List<Block> blockList) {
HashMap<Location, Block> blockMap = getBlockMap(blockList);
Collection<Block> targetBlocks = getTargetBlockList(location, blockMap);
modifyBlocks(targetBlocks);
}
private HashMap<Location, Block> getBlockMap(List<Block> blockList) {
HashMap<Location, Block> blockMap = new HashMap<>();
for (Block block : blockList) {
blockMap.put(block.getLocation(), block);
}
return blockMap;
}
private Collection<Block> getTargetBlockList(Location center, HashMap<Location, Block> explosionMap) {
HashMap<Location, Block> targetMap = new HashMap<>();
for (Location location : explosionMap.keySet()) {
checkLocation(location.clone().add(1, 0, 0), explosionMap, targetMap);
checkLocation(location.clone().add(-1, 0, 0), explosionMap, targetMap);
checkLocation(location.clone().add(0, 1, 0), explosionMap, targetMap);
checkLocation(location.clone().add(0, -1, 0), explosionMap, targetMap);
checkLocation(location.clone().add(0, 0, 1), explosionMap, targetMap);
checkLocation(location.clone().add(0, 0, -1), explosionMap, targetMap);
}
return targetMap.values();
}
private void checkLocation(Location location, HashMap<Location, Block> explosionMap, HashMap<Location, Block> targetMap) {
if (isTargetLocation(location, explosionMap, targetMap)) {
targetMap.put(location, location.getBlock());
}
}
private boolean isTargetLocation(Location location, HashMap<Location, Block> explosionMap, HashMap<Location, Block> targetMap) {
return !explosionMap.containsKey(location) && !targetMap.containsKey(location);
}
private void modifyBlocks(Collection<Block> blocks) {
for (Block block : blocks) {
for (BlockModifier blockModifier : modifiers) {
blockModifier.modify(block);
}
}
}
public void addBlockModifier(BlockModifier blockModifier) {
modifiers.add(blockModifier);
}
public void removeBlockModifier(BlockModifier blockModifier) {
modifiers.remove(blockModifier);
}
public List<BlockModifier> getBlockModifierList() {
return modifiers.subList(0, modifiers.size() - 1);
}
}
package fsmpi.gaming.tnt_crusher;
import java.util.ArrayList;
import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin {
@Override
public void onEnable() {
ArrayList<BlockModifier> modifiers = new ArrayList<>();
modifiers.add(new ChangeBlockModifier(Material.COBBLESTONE, Material.DIAMOND_BLOCK, 1));
getServer().getPluginManager().registerEvents(new ExplosionBlockHandler(modifiers), this);
}
@Override
public void onDisable() {
}
}
name: TNT-Crusher
main: fsmpi.gaming.tnt_crusher.MyPlugin
version: ${project.version}
api-version: 1.20
commands:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment