Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Code changes and optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Max094Reikeb committed May 5, 2022
1 parent 2952fbe commit fad9a13
Show file tree
Hide file tree
Showing 37 changed files with 47 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package net.reikeb.notenoughgamerules;

import net.minecraft.entity.damage.DamageSource;

import net.reikeb.notenoughgamerules.mixin.DamageSourceAccessor;

public class DamageSources {

public static final DamageSource EXPLOSION = ((DamageSourceAccessor) newDamageSource("explosion")).invokeSetBypassesArmor();
public static final DamageSource SKY_HIGH = ((DamageSourceAccessor) newDamageSource("sky_high")).invokeSetBypassesArmor();
public static final DamageSource EXPLOSION = newDamageSource("explosion", true);
public static final DamageSource SKY_HIGH = newDamageSource("sky_high", true);

public static DamageSource newDamageSource(String name) {
public static DamageSource newDamageSource(String name, boolean bypassArmor) {
if (bypassArmor)
return ((DamageSourceAccessor) DamageSourceAccessor.invokeConstructor(name)).invokeSetBypassesArmor();
return DamageSourceAccessor.invokeConstructor(name);
}
}
1 change: 0 additions & 1 deletion src/main/java/net/reikeb/notenoughgamerules/Gamerules.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory;
import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry;

import net.minecraft.world.GameRules;

public class Gamerules {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;

import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.events.AfterRespawnListener;
import net.reikeb.notenoughgamerules.events.PlayerSleepsListener;

import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

public class NotEnoughGamerules implements ModInitializer {

@Override
public void onInitialize() {
Gamerules.setupGamerules();
EntitySleepEvents.ALLOW_SLEEPING.register(new PlayerSleepsListener());
ServerPlayerEvents.AFTER_RESPAWN.register(new AfterRespawnListener());
}

public static void damageGamerule(Entity entity, DamageSource source, CallbackInfoReturnable<Boolean> cir) {
if (entity == null) return;
GameRules gameRules = entity.getWorld().getGameRules();
Expand All @@ -39,11 +43,4 @@ public static void damageGamerule(Entity entity, DamageSource source, CallbackIn
cir.cancel();
}
}

@Override
public void onInitialize() {
Gamerules.setupGamerules();
EntitySleepEvents.ALLOW_SLEEPING.register(new PlayerSleepsListener());
ServerPlayerEvents.AFTER_RESPAWN.register(new AfterRespawnListener());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package net.reikeb.notenoughgamerules.events;

import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;

import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

public class AfterRespawnListener implements ServerPlayerEvents.AfterRespawn {

@Override
public void afterRespawn(ServerPlayerEntity oldPlayer, ServerPlayerEntity newPlayer, boolean alive) {
GameRules gameRules = newPlayer.getEntityWorld().getGameRules();
if (gameRules.getBoolean(Gamerules.KEEP_EFFECTS)) {
if (newPlayer.getEntityWorld().getGameRules().getBoolean(Gamerules.KEEP_EFFECTS)) {
for (int i = 0; i < oldPlayer.getStatusEffects().size(); i++) {
StatusEffectInstance statusEffectInstance = oldPlayer.getStatusEffects().stream().toList().get(i);
newPlayer.addStatusEffect(statusEffectInstance, oldPlayer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package net.reikeb.notenoughgamerules.events;

import net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

import org.jetbrains.annotations.Nullable;

public class PlayerSleepsListener implements EntitySleepEvents.AllowSleeping {

@Override
public PlayerEntity.@Nullable SleepFailureReason allowSleep(PlayerEntity player, BlockPos sleepingPos) {
GameRules gameRules = player.getWorld().getGameRules();
if (!gameRules.getBoolean(Gamerules.CAN_PLAYER_SLEEP)) {
if (!player.getWorld().getGameRules().getBoolean(Gamerules.CAN_PLAYER_SLEEP)) {
return PlayerEntity.SleepFailureReason.NOT_POSSIBLE_NOW;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import net.minecraft.server.filter.TextStream;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -20,7 +17,7 @@ public class ChatMixin {

@Inject(method = "handleMessage", at = @At("HEAD"), cancellable = true)
private void handleMessage(TextStream.Message message, CallbackInfo ci) {
GameRules gameRules = this.player.getWorld().getGameRules();
if ((!message.getRaw().startsWith("/")) && (gameRules.getBoolean(Gamerules.DISABLE_CHAT))) ci.cancel();
if ((!message.getRaw().startsWith("/")) && (this.player.getWorld().getGameRules().getBoolean(Gamerules.DISABLE_CHAT)))
ci.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package net.reikeb.notenoughgamerules.mixin;

import net.minecraft.entity.damage.DamageSource;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(DamageSource.class)
public interface DamageSourceAccessor {
@Invoker("<init>")
public static DamageSource invokeConstructor(String name) {
static DamageSource invokeConstructor(String name) {
throw new AssertionError("mixin failed to apply");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import net.minecraft.entity.boss.dragon.EnderDragonFight;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

import org.objectweb.asm.Opcodes;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -25,7 +21,6 @@ public class EnderDragonFightMixin {

@Redirect(method = "dragonKilled", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/boss/dragon/EnderDragonFight;previouslyKilled:Z", opcode = Opcodes.GETFIELD))
private boolean dragonKilled(EnderDragonFight instance) {
GameRules gameRules = this.world.getGameRules();
return this.previouslyKilled && !gameRules.getBoolean(Gamerules.ALWAYS_SPAWN_DRAGON_EGG);
return this.previouslyKilled && !this.world.getGameRules().getBoolean(Gamerules.ALWAYS_SPAWN_DRAGON_EGG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldView;
import net.minecraft.world.biome.Biome;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -23,8 +21,10 @@ public class AbstractBlockStateMixin {
private void onEntityCollision(World world, BlockPos pos, Entity entity, CallbackInfo ci) {
if (!world.getGameRules().getBoolean(Gamerules.CAN_ENTITY_INTERACT_WITH_WORLD)) ci.cancel();
}

@Inject(method = "onUse", at = @At("HEAD"), cancellable = true)
private void onUse(World world, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) {
if (!world.getGameRules().getBoolean(Gamerules.CAN_ENTITY_INTERACT_WITH_BLOCK)) cir.setReturnValue(ActionResult.PASS);
if (!world.getGameRules().getBoolean(Gamerules.CAN_ENTITY_INTERACT_WITH_BLOCK))
cir.setReturnValue(ActionResult.PASS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.minecraft.block.CoralBlockBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.minecraft.block.CoralBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.minecraft.block.CoralFanBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.minecraft.block.CoralWallFanBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.minecraft.block.IceBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import net.minecraft.block.OxidizableBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -19,7 +16,6 @@
public class OxidizableBlockMixin {
@Inject(method = "randomTick", at = @At("HEAD"), cancellable = true)
private void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random, CallbackInfo ci) {
GameRules gameRules = world.getGameRules();
if (!gameRules.getBoolean(Gamerules.CAN_COPPER_OXIDE)) ci.cancel();
if (!world.getGameRules().getBoolean(Gamerules.CAN_COPPER_OXIDE)) ci.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import net.minecraft.block.PistonBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -17,7 +14,6 @@
public class PistonMixin {
@Inject(method = "shouldExtend", at = @At("HEAD"), cancellable = true)
private void shouldExtend(World world, BlockPos pos, Direction pistonFace, CallbackInfoReturnable<Boolean> cir) {
GameRules gameRules = world.getGameRules();
if (gameRules.getBoolean(Gamerules.DISABLE_PISTONS)) cir.setReturnValue(false);
if (world.getGameRules().getBoolean(Gamerules.DISABLE_PISTONS)) cir.setReturnValue(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.minecraft.block.SnowBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -15,7 +12,6 @@
public class AnimalMixin {
@Inject(method = "breed", at = @At("HEAD"), cancellable = true)
private void breed(ServerWorld world, AnimalEntity other, CallbackInfo ci) {
GameRules gameRules = world.getGameRules();
if (!gameRules.getBoolean(Gamerules.DO_BABIES_SPAWN)) ci.cancel();
if (!world.getGameRules().getBoolean(Gamerules.DO_BABIES_SPAWN)) ci.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -27,8 +24,7 @@ public abstract class EntityMixin {
public abstract int getId();

@Shadow
public void onStruckByLightning(ServerWorld world, LightningEntity lightning) {
}
public void onStruckByLightning(ServerWorld world, LightningEntity lightning) {}

@Shadow
public abstract boolean damage(DamageSource source, float amount);
Expand All @@ -38,9 +34,9 @@ public void onStruckByLightning(ServerWorld world, LightningEntity lightning) {

@Inject(method = "onStruckByLightning", at = @At("HEAD"), cancellable = true)
private void onStruckByLightning(ServerWorld world, LightningEntity lightning, CallbackInfo ci) {
GameRules gameRules = lightning.getWorld().getGameRules();
if (gameRules.getInt(Gamerules.LIGHTNING_DAMAGE) > -1) {
this.damage(DamageSource.LIGHTNING_BOLT, (float) gameRules.getInt(Gamerules.LIGHTNING_DAMAGE));
int lightningDamage = lightning.getWorld().getGameRules().getInt(Gamerules.LIGHTNING_DAMAGE);
if (lightningDamage > -1) {
this.damage(DamageSource.LIGHTNING_BOLT, (float) lightningDamage);
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package net.reikeb.notenoughgamerules.mixin.entities;

import net.minecraft.entity.passive.FoxEntity;
import net.minecraft.world.GameRules;

import net.reikeb.notenoughgamerules.Gamerules;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -20,7 +17,6 @@ public class FoxBreadMixin {

@Inject(method = "breed", at = @At("HEAD"), cancellable = true)
private void bread(CallbackInfo ci) {
GameRules gameRules = this.field_17973.world.getGameRules();
if (!gameRules.getBoolean(Gamerules.DO_BABIES_SPAWN)) ci.cancel();
if (!this.field_17973.world.getGameRules().getBoolean(Gamerules.DO_BABIES_SPAWN)) ci.cancel();
}
}
Loading

0 comments on commit fad9a13

Please sign in to comment.