Add config & wither option

This commit is contained in:
KäseToatz
2024-07-15 17:37:58 +02:00
parent 8c9e1b419a
commit cb53cc39fa
3 changed files with 73 additions and 3 deletions

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.11
# Mod Properties
mod_version = 1.0
mod_version = 1.1
maven_group = com.kasetoatz
archives_base_name = noexplosiongrief

View File

@ -0,0 +1,67 @@
package com.kasetoatz.noexplosiongrief.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.crash.CrashException;
import net.minecraft.util.crash.CrashReport;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Config {
public static boolean blockCreeper = true;
public static boolean blockGhast = true;
public static boolean blockWither = true;
private static final File config = new File(MinecraftClient.getInstance().runDirectory, "config/noexplosiongrief.json");
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static void load()
{
if (!config.exists())
{
save();
return;
}
try (FileReader reader = new FileReader(config))
{
JsonObject json = gson.fromJson(reader, JsonObject.class);
if (json.has("blockCreeper"))
{
blockCreeper = json.get("blockCreeper").getAsBoolean();
}
if (json.has("blockGhast"))
{
blockGhast = json.get("blockGhast").getAsBoolean();
}
if (json.has("blockWither"))
{
blockWither = json.get("blockWither").getAsBoolean();
}
}
catch (IOException exc)
{
throw new CrashException(CrashReport.create(exc, "Loading config file."));
}
}
public static void save()
{
JsonObject json = new JsonObject();
json.addProperty("blockCreeper", blockCreeper);
json.addProperty("blockGhast", blockGhast);
json.addProperty("blockWither", blockWither);
try (FileWriter writer = new FileWriter(config))
{
gson.toJson(json, writer);
}
catch (IOException exc)
{
throw new CrashException(CrashReport.create(exc, "Saving config file."));
}
}
}

View File

@ -1,10 +1,13 @@
package com.kasetoatz.noexplosiongrief.mixin;
import com.kasetoatz.noexplosiongrief.config.Config;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.boss.WitherEntity;
import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.entity.mob.GhastEntity;
import net.minecraft.entity.projectile.FireballEntity;
import net.minecraft.entity.projectile.WitherSkullEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.explosion.Explosion;
@ -15,10 +18,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(Entity.class)
public abstract class EntityMixin {
@Inject(method="canExplosionDestroyBlock", at = @At("RETURN"), cancellable = true)
@Inject(method="canExplosionDestroyBlock", at = @At("HEAD"), cancellable = true)
public void onExplosion(Explosion explosion, BlockView world, BlockPos pos, BlockState state, float explosionPower, CallbackInfoReturnable<Boolean> cir) {
Entity entity = (Entity)(Object)this;
if (entity instanceof CreeperEntity || (entity instanceof FireballEntity && ((FireballEntity)entity).getOwner() instanceof GhastEntity))
if ((entity instanceof CreeperEntity && Config.blockCreeper) || ((entity instanceof FireballEntity && ((FireballEntity)entity).getOwner() instanceof GhastEntity) && Config.blockGhast) || ((entity instanceof WitherSkullEntity && ((WitherSkullEntity)entity).getOwner() instanceof WitherEntity) && Config.blockWither))
{
cir.setReturnValue(false);
}