Skip to content

Commit

Permalink
Bartender
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzesc committed Feb 21, 2020
0 parents commit 0c622d9
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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>tyze.bukkit.bartender</groupId>
<artifactId>Bartender</artifactId>
<version>1.0</version>

<name>Bartender</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<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.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<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>
</project>
97 changes: 97 additions & 0 deletions src/main/java/tyze/bukkit/bartender/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package tyze.bukkit.bartender;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;

public class App extends JavaPlugin implements Listener {
private static ArrayList<Integer> expTotalTable = new ArrayList<Integer>();
private static ArrayList<Integer> expRequireTable = new ArrayList<Integer>();
private static int cost = 15;

@EventHandler
public void onRightClick(PlayerInteractEvent event) {
// prevent event triggered twice.
if (event.getHand().equals(EquipmentSlot.HAND) == false)
return;

Block block = event.getClickedBlock();
if (block == null || block.getType().equals(Material.DIAMOND_BLOCK) == false)
return;

Player p = event.getPlayer();
ItemStack itemInHand = p.getInventory().getItemInMainHand();
if (itemInHand == null || itemInHand.getType().equals(Material.GLASS_BOTTLE) == false)
return;

int level = p.getLevel();
float exp = p.getExp();

int total = getTotalExp(level, exp);

if (level <= 1 || total < cost) {
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§c§l經驗值不夠哦!快去打怪升等吧"));
event.setCancelled(true);
return;
}

int now = (int) (expRequireTable.get(level) * exp);

// lv3 -> lv4 needs 13 exp, there is not enough. here should be while-loop
while (now < cost) {
level -= 1;
now += expRequireTable.get(level);
}
now -= cost;
exp = now * 1f / expRequireTable.get(level);

if (p.getInventory().addItem(new ItemStack(Material.EXP_BOTTLE)).isEmpty()) {
int amount = p.getInventory().getItemInMainHand().getAmount();
p.getInventory().getItemInMainHand().setAmount(amount - 1);
p.setExp(exp * 1f);
p.setLevel(level);
}
}

private int getTotalExp(Integer level, float exp) {
return expTotalTable.get(level) + (int) (expRequireTable.get(level) * exp);
}

@Override
public void onEnable() {
getLogger().info("Bartender enable.");
int sum = 0;
int req = 0;
for (int level = 0; level <= 25000; level++) {
expTotalTable.add(sum);
if (level <= 15) {
req = level * 2 + 7;
} else if (level <= 30) {
req = level * 5 - 38;
} else {
req = 9 * level - 158;
}
sum += req;
expRequireTable.add(req);
}
getLogger().info("Bartender built exp table.");
Bukkit.getPluginManager().registerEvents(this, this);
}

@Override
public void onDisable() {
getLogger().info("Bartender disable.");
}
}
3 changes: 3 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
main: tyze.bukkit.bartender.App
name: Bartender
version: 1.0

0 comments on commit 0c622d9

Please sign in to comment.