mod version
This commit is contained in:
@@ -1,57 +1,14 @@
|
||||
package com.kasetoatz.fastGhast;
|
||||
package com.kasetoatz.fastghast;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.attribute.AttributeInstance;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDismountEvent;
|
||||
import org.bukkit.event.entity.EntityMountEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import io.papermc.paper.entity.Leashable;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
public final class FastGhast extends JavaPlugin implements Listener
|
||||
import static com.kasetoatz.fastghast.config.Config.load;
|
||||
|
||||
public class Fastghast implements ModInitializer
|
||||
{
|
||||
private static final double DEFAULT = 0.05;
|
||||
private double SPEED;
|
||||
|
||||
@Override
|
||||
public void onEnable()
|
||||
public void onInitialize()
|
||||
{
|
||||
saveDefaultConfig();
|
||||
SPEED = getConfig().getDouble("speed");
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
Bukkit.getScheduler().runTaskTimer(this, () -> {
|
||||
getServer().getWorlds().forEach(world -> world.getEntities().forEach(entity -> {
|
||||
if (entity instanceof Leashable leashed && leashed.isLeashed() && leashed.getLeashHolder() instanceof HappyGhast ghast && leashed.getLocation().distance(ghast.getLocation()) > 8) {
|
||||
leashed.teleport(ghast.getLocation());
|
||||
}
|
||||
}));
|
||||
}, 0, 1);
|
||||
}
|
||||
|
||||
private void setSpeed(Entity entity, double speed)
|
||||
{
|
||||
if (entity instanceof HappyGhast ghast)
|
||||
{
|
||||
AttributeInstance attr = ghast.getAttribute(Attribute.FLYING_SPEED);
|
||||
if (attr != null)
|
||||
{
|
||||
attr.setBaseValue(speed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityMount(EntityMountEvent event)
|
||||
{
|
||||
setSpeed(event.getMount(), SPEED);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityDismount(EntityDismountEvent event)
|
||||
{
|
||||
setSpeed(event.getDismounted(), DEFAULT);
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
62
src/main/java/com/kasetoatz/fastGhast/config/Config.java
Normal file
62
src/main/java/com/kasetoatz/fastGhast/config/Config.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.kasetoatz.fastghast.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.util.crash.CrashException;
|
||||
import net.minecraft.util.crash.CrashReport;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Config
|
||||
{
|
||||
public static double UNMOUNTED_SPEED = 0.05;
|
||||
public static double MOUNTED_SPEED = 0.15;
|
||||
|
||||
private static final Path FILE = FabricLoader.getInstance().getConfigDir().resolve("fastghast.json");
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
public static void load() {
|
||||
if (!Files.exists(FILE))
|
||||
{
|
||||
save();
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
String json = Files.readString(FILE);
|
||||
JsonObject data = GSON.fromJson(json, JsonObject.class);
|
||||
if (data.has("unmounted-speed"))
|
||||
{
|
||||
UNMOUNTED_SPEED = data.get("unmounted-speed").getAsDouble();
|
||||
}
|
||||
if (data.has("mounted-speed"))
|
||||
{
|
||||
MOUNTED_SPEED = data.get("mounted-speed").getAsDouble();
|
||||
}
|
||||
save();
|
||||
}
|
||||
catch (IOException exc)
|
||||
{
|
||||
throw new CrashException(CrashReport.create(exc, "Loading config file."));
|
||||
}
|
||||
}
|
||||
|
||||
private static void save()
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonObject data = new JsonObject();
|
||||
data.addProperty("unmounted-speed", UNMOUNTED_SPEED);
|
||||
data.addProperty("mounted-speed", MOUNTED_SPEED);
|
||||
Files.writeString(FILE, GSON.toJson(data));
|
||||
}
|
||||
catch (IOException exc)
|
||||
{
|
||||
throw new CrashException(CrashReport.create(exc, "Saving config file."));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.kasetoatz.fastghast.mixin;
|
||||
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.attribute.EntityAttribute;
|
||||
import net.minecraft.entity.passive.HappyGhastEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.registry.entry.RegistryEntry;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
|
||||
import static com.kasetoatz.fastghast.config.Config.MOUNTED_SPEED;
|
||||
import static com.kasetoatz.fastghast.config.Config.UNMOUNTED_SPEED;
|
||||
|
||||
@Mixin(HappyGhastEntity.class)
|
||||
public abstract class HappyGhastMixin
|
||||
{
|
||||
@Shadow @Nullable public abstract LivingEntity getControllingPassenger();
|
||||
|
||||
@Redirect(method="travel", at=@At(value="INVOKE", target="Lnet/minecraft/entity/passive/HappyGhastEntity;getAttributeValue(Lnet/minecraft/registry/entry/RegistryEntry;)D"))
|
||||
public double speed(HappyGhastEntity instance, RegistryEntry<EntityAttribute> registryEntry)
|
||||
{
|
||||
return getControllingPassenger() instanceof PlayerEntity ? MOUNTED_SPEED : UNMOUNTED_SPEED;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user