Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
dependencies {
api("org.jetbrains:annotations-java5:24.1.0")
compileOnly("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT")
compileOnly("net.countercraft:movecraft:+")
compileOnly("net.countercraft:movecraft:8.0.0_beta-5_dev-2")
compileOnly("net.countercraft.movecraft.combat:movecraft-combat:+")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void onEnable() {
}

manager.runTaskTimer(this, 20, 1);
getServer().getPluginManager().registerEvents(manager, this);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ private boolean checkCommand (CommandSender sender, Player player) {
return true;
}
sender.sendMessage(ChatUtils.MOVECRAFT_COMMAND_PREFIX + " Checking craft of type " + c.getType().getStringProperty(CraftType.NAME) + " commanded by " + player.getDisplayName() + ":");
sender.sendMessage("Current Heat: " + craftHeat.getHeat());
sender.sendMessage("Capacity: " + craftHeat.getHeatCapacity());
sender.sendMessage("Dissipation / second: " + craftHeat.getDissipation());
sender.sendMessage("Current Heat: " + craftHeat.getCraft().getDataTag(CraftHeat.HEAT));
sender.sendMessage("Capacity: " + craftHeat.getCraft().getDataTag(CraftHeat.HEAT_CAPACITY));
sender.sendMessage("Dissipation / second: " + craftHeat.getCraft().getDataTag(CraftHeat.DISSIPATION));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void onBlockDispense (BlockDispenseEvent event) {
if (craft == null) return;
CraftHeat heat = MovecraftOverheat.getInstance().getHeatManager().getHeat(craft);
if (heat != null) {
if (heat.isSilenced() && craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(event.getBlock().getLocation()))) {
if (craft.getDataTag(CraftHeat.SILENCED) && craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(event.getBlock().getLocation()))) {
event.setCancelled(true);
craft.getAudience().playSound(Sound.sound(Key.key("block.dispenser.fail"), Sound.Source.BLOCK, 1f, 1f));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,44 @@
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.PlayerCraft;
import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer;
import net.countercraft.movecraft.craft.datatag.CraftDataTagKey;
import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry;
import net.countercraft.movecraft.movecraftoverheat.Keys;
import net.countercraft.movecraft.movecraftoverheat.MovecraftOverheat;
import net.countercraft.movecraft.movecraftoverheat.config.Settings;
import net.countercraft.movecraft.movecraftoverheat.disaster.DisasterType;
import net.countercraft.movecraft.util.ChatUtils;
import net.countercraft.movecraft.util.Counter;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.jetbrains.annotations.NotNull;

public class CraftHeat {
public static final CraftDataTagKey<Double> HEAT_CAPACITY = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft-overheat", "heat-capacity"), craft -> (double) craft.getOrigBlockCount() * craft.getType().getDoubleProperty(Keys.HEAT_CAPACITY_PER_BLOCK));
public static final CraftDataTagKey<Double> HEAT = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft-overheat", "heat"), craft -> 0D);
public static final CraftDataTagKey<Double> DISSIPATION = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft-overheat", "dissipation"), craft -> (double) craft.getOrigBlockCount() * craft.getType().getDoubleProperty(Keys.HEAT_DISSIPATION_PER_BLOCK));
public static final CraftDataTagKey<Boolean> SILENCED = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft-overheat", "silenced"), craft -> false);
private final Craft craft;
private double heatCapacity;
private double heat;
private double dissipation;
private long lastUpdate;
private long lastDisaster;
private boolean silenced;

private boolean firedThisTick;
private int explosionsThisTick;
private final BossBar bossBar;

public CraftHeat (@NotNull Craft c) {
this.craft = c;
this.heat = 0;
this.bossBar = Bukkit.createBossBar("Heat: " + this.heat + " / " + this.heatCapacity, BarColor.GREEN, BarStyle.SEGMENTED_6);
this.recalculate();
this.bossBar = Bukkit.createBossBar("Heat", BarColor.GREEN, BarStyle.SEGMENTED_6);
bossBar.setVisible(false);
if (craft instanceof PlayerCraft) {
this.bossBar.addPlayer(((PlayerCraft) this.craft).getPilot());
}
Expand All @@ -48,20 +52,20 @@ public void recalculate () {
double cPerBlock = craft.getType().getDoubleProperty(Keys.HEAT_CAPACITY_PER_BLOCK);
double dPerBlock = craft.getType().getDoubleProperty(Keys.HEAT_DISSIPATION_PER_BLOCK);

for (MovecraftLocation location : craft.getHitBox()) {
Material type = craft.getWorld().getBlockAt(location.getX(), location.getY(), location.getZ()).getType();
if (type == Material.AIR || type == Material.CAVE_AIR || type == Material.FIRE) {
continue;
}
if (Settings.HeatSinkBlocks.containsKey(type)) {
newCapacity += Settings.HeatSinkBlocks.get(type) * cPerBlock;
Counter<Material> materials = craft.getDataTag(Craft.MATERIALS);
if (materials.isEmpty())
return;

for (Material m : materials.getKeySet()) {
if (Settings.HeatSinkBlocks.containsKey(m)) {
newCapacity += Settings.HeatSinkBlocks.get(m) * cPerBlock * materials.get(m);
} else {
newCapacity += cPerBlock;
newCapacity += cPerBlock * materials.get(m);
}
if (Settings.RadiatorBlocks.containsKey(type)) {
newDissipation += Settings.RadiatorBlocks.get(type) * dPerBlock;
if (Settings.RadiatorBlocks.containsKey(m)) {
newDissipation += Settings.RadiatorBlocks.get(m) * dPerBlock * materials.get(m);
} else {
newDissipation += dPerBlock;
newDissipation += dPerBlock * materials.get(m);
}
}

Expand All @@ -70,47 +74,52 @@ public void recalculate () {
}
newCapacity = Math.round(newCapacity);

double heat = craft.getDataTag(HEAT);
if (Math.abs(heat) >= 0.01) {
heat *= newCapacity/heatCapacity;
heat *= newCapacity / craft.getDataTag(HEAT_CAPACITY);
craft.setDataTag(HEAT, heat);
}
heatCapacity = newCapacity;
dissipation = newDissipation;
craft.setDataTag(HEAT_CAPACITY, newCapacity);
craft.setDataTag(DISSIPATION, newDissipation);

updateBossBar();
}

public void processDissipation () {
double heat = craft.getDataTag(HEAT);
if (heat > 0.0) {
heat -= dissipation;
heat -= craft.getDataTag(DISSIPATION);
}
if (heat < 0.0) {
heat = 0.0;
}
craft.setDataTag(HEAT, heat);
updateBossBar();
}

public void checkDisasters () {
// Update whether the craft is silenced
if (silenced) {
if (heat < heatCapacity * Settings.SilenceHeatThreshold) {
if (craft.getDataTag(SILENCED)) {
if (craft.getDataTag(HEAT) < craft.getDataTag(HEAT_CAPACITY) * Settings.SilenceHeatThreshold) {
craft.getAudience().sendMessage(Component.text(ChatUtils.MOVECRAFT_COMMAND_PREFIX + "No longer silenced!"));
silenced = false;
craft.setDataTag(SILENCED, false);
}
} else {
if (Settings.SilenceOverheatedCrafts && heat > heatCapacity * Settings.SilenceHeatThreshold) {
if (Settings.SilenceOverheatedCrafts && craft.getDataTag(HEAT) > craft.getDataTag(HEAT_CAPACITY) * Settings.SilenceHeatThreshold) {
craft.getAudience().sendMessage(Component.text(ChatUtils.MOVECRAFT_COMMAND_PREFIX + ChatColor.RED + "Silenced! Your guns are too hot to fire!"));
craft.getAudience().playSound(Sound.sound(Key.key("entity.blaze.death"), Sound.Source.BLOCK, 2.0f, 1.0f));
silenced = true;
craft.setDataTag(SILENCED, true);
}
}
for (DisasterType type : MovecraftOverheat.getDisasterTypes()) {
if (type.getHeatThreshold() * heatCapacity > heat) continue;
if ((1-type.getRandomChance()) * (Math.exp(-1 * type.getRandomChancePowerFactor() * ((heat/heatCapacity)-type.getHeatThreshold()))) > Math.random()) continue;
if (type.getHeatThreshold() * craft.getDataTag(HEAT_CAPACITY) > craft.getDataTag(HEAT)) continue;
if ((1-type.getRandomChance()) * (Math.exp(-1 * type.getRandomChancePowerFactor() * ((craft.getDataTag(HEAT)/craft.getDataTag(HEAT_CAPACITY))-type.getHeatThreshold()))) > Math.random()) continue;
MovecraftOverheat.getInstance().getHeatManager().addDisaster(type.createNew(this));
lastDisaster = System.currentTimeMillis();
}
}

public Craft getCraft () {
public Craft getCraft() {
return craft;
}

Expand All @@ -122,42 +131,28 @@ public void setLastUpdate(long l) {
lastUpdate = l;
}

public double getHeat() {
return heat;
}

public double getHeatCapacity() {
return heatCapacity;
}

public double getDissipation() {
return dissipation;
}

public void addHeat (double heatToAdd) {
double heat = craft.getDataTag(HEAT);
heat += heatToAdd;
craft.setDataTag(HEAT, heat);
updateBossBar();
}

public long getLastDisaster() {
return lastDisaster;
}

public boolean isSilenced() {
return silenced;
}

private void updateBossBar () {
bossBar.setTitle("Heat: " + Math.round(heat*10)/10d + " / " + heatCapacity);
if (heat >= heatCapacity*1.5) {
private void updateBossBar() {
bossBar.setTitle(String.format("Heat: %.1f / %.1f", craft.getDataTag(HEAT), craft.getDataTag(HEAT_CAPACITY)));
if (craft.getDataTag(HEAT) >= craft.getDataTag(HEAT_CAPACITY)*1.5) {
bossBar.setColor(BarColor.RED);
} else if (heat >= heatCapacity) {
} else if (craft.getDataTag(HEAT) >= craft.getDataTag(HEAT_CAPACITY)) {
bossBar.setColor(BarColor.YELLOW);
} else {
bossBar.setColor(BarColor.GREEN);
}
bossBar.setProgress(Math.min(1.0, heat/heatCapacity));
bossBar.setVisible(heat != 0.0);
bossBar.setProgress(Math.min(1.0, craft.getDataTag(HEAT) / craft.getDataTag(HEAT_CAPACITY)));
bossBar.setVisible(craft.getDataTag(HEAT) != 0.0);
}

public void removeBossBar () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package net.countercraft.movecraft.movecraftoverheat.tracking;

import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.features.status.events.CraftStatusUpdateEvent;
import net.countercraft.movecraft.movecraftoverheat.MovecraftOverheat;
import net.countercraft.movecraft.movecraftoverheat.config.Settings;
import net.countercraft.movecraft.movecraftoverheat.disaster.Disaster;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;

public class HeatManager extends BukkitRunnable {
public class HeatManager extends BukkitRunnable implements Listener {
private final HashMap<Craft, CraftHeat> heatTracking = new HashMap<>();
private final Queue<Disaster> disasterQueue = new ConcurrentLinkedQueue();

Expand All @@ -23,7 +28,6 @@ public void run() {
if (heat.getLastUpdate() + Settings.HeatCheckInterval >= time) {
continue;
}
heat.recalculate();
heat.processDissipation();
heat.setLastUpdate(time);
if (heat.getLastDisaster() + Settings.DisasterCheckInterval <= time) {
Expand Down Expand Up @@ -59,4 +63,12 @@ public CraftHeat getHeat(Craft c) {
public void addDisaster (Disaster disaster) {
disasterQueue.add(disaster);
}

@EventHandler(priority = EventPriority.NORMAL)
public void onCraftStatusUpdate(@NotNull CraftStatusUpdateEvent event) {
if (!heatTracking.containsKey(event.getCraft()))
return;

heatTracking.get(event.getCraft()).recalculate();
}
}