Skip to content

Commit

Permalink
Fix crashing on dedicated servers by canceling on finding null server
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonEggBedrockBreaking committed Feb 24, 2022
1 parent d2cda66 commit 7fe42b5
Show file tree
Hide file tree
Showing 50 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private void addDedicatedOptionsToMap() {
*/
@Inject(method = "performCommand", at = @At(value = "HEAD"), cancellable = true)
private void performCommand(CommandSourceStack source, String command, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
String commandName = command.split(" ")[0].substring(1);
if (commandNameGameruleMap.isEmpty()) {
addOptionsToMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void addOptionsToMap() {
*/
@Inject(method = "isInvulnerableTo", at = @At(value = "TAIL"), cancellable = true)
private void isAlsoInvulnerableTo(DamageSource damageSource, CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (damageSourceMap.isEmpty()) {
addOptionsToMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public abstract class MixinItemEntity {
* @param ci
*/
@Inject(method = "tick", at = @At("HEAD"), cancellable = true)
private void discardItem(CallbackInfo ci) {
private void discardItem(CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (this.age >= MAX * 20 &&
!((Entity) (Object) (this)).getLevel().isClientSide) {
((Entity) (Object) (this)).discard();
Expand All @@ -45,6 +46,7 @@ private void discardItem(CallbackInfo ci) {
cancellable = true
)
private void cancelDiscard(CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (this.age < MAX * 20 &&
this.pickupDelay != Short.MAX_VALUE) {
ci.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private boolean additionalRestrictionsMet() {
*/
@Inject(method = "removeWhenFarAway", at = @At("HEAD"), cancellable = true)
private void cancelRemovelWhenFarAway(double distanceSquared, CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (spawnGroupDespawnMap.isEmpty()) {
addOptionsToMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void addStartOptionsToMap() {
*/
@Inject(method = "getDespawnDistance", at = @At("HEAD"), cancellable = true)
public void editDespawnDistance(CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (spawnGroupImmediateMap.isEmpty()) {
addImmediateOptionsToMap();
}
Expand All @@ -70,6 +71,7 @@ public void editDespawnDistance(CallbackInfoReturnable<Integer> cir) {
*/
@Inject(method = "getNoDespawnDistance", at = @At("HEAD"), cancellable = true)
public void editNoDespawnDistance(CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (spawnGroupStartMap.isEmpty()) {
addStartOptionsToMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class MixinNaturalSpawner {
*/
@Inject(method = "isRightDistanceToPlayerAndSpawnPoint", at = @At("HEAD"), cancellable = true)
private static void mayMeRightDistanceToPlayerAndSpawnPoint(ServerLevel level, ChunkAccess chunk, BlockPos.MutableBlockPos pos, double squaredDistance, CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (squaredDistance <= Math.pow(RegisterGamerules.getServer().getGameRules().getInt(RegisterGamerules.MIN_SPAWN_DISTANCE), 2)) {
cir.setReturnValue(false);
} else if (level.getSharedSpawnPos().closerThan(new Vec3((double)pos.getX() + 0.5, pos.getY(), (double)pos.getZ() + 0.5), RegisterGamerules.getServer().getGameRules().getInt(RegisterGamerules.MIN_SPAWN_DISTANCE))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class MixinThrownEnderpearl {
cancellable = true
)
private void cancelDiscard(CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.ENDER_PEARLS_DESPAWN_ON_DEATH)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private void addOptionsToMap() {
*/
@Inject(method = "canBeAffected", at = @At("HEAD"), cancellable = true)
private void canItBeAffected(MobEffectInstance effect, CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (((Object) this) instanceof ServerPlayer) {
MobEffect statusEffect = effect.getEffect();
if (statusEffectMap.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private static void addOptionsToMap() {
*/
@Inject(method = "getItemEnchantmentLevel", at = @At("HEAD"), cancellable = true)
private static void removeEnchantmentLevel(Enchantment enchantment, ItemStack stack, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (enchantmentMap.isEmpty()) {
addOptionsToMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public boolean isNotUltraWarm(DimensionType type, @Nullable Player player, Level
*/
@Inject(method = "playEmptySound", at = @At(value = "HEAD"), cancellable = true)
protected void cancelPlayiningEmptySound(@Nullable Player player, LevelAccessor world, BlockPos pos, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (world.dimensionType().ultraWarm() && this.content.is(FluidTags.WATER) &&
((ServerLevelAccessor)world).getLevel().getGameRules().getBoolean(RegisterGamerules.WATER_PLACEABLE_IN_NETHER)) {
world.playSound(player, pos, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5f, 2.6f + (world.getRandom().nextFloat() - world.getRandom().nextFloat()) * 0.8f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class MixinEntity {
*/
@Inject(method = "onAboveBubbleCol", at = @At("HEAD"), cancellable = true)
private void cancelAboveBubbleCol(boolean drag, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.BUBBLE_COLUMNS_ENABLED)) {
ci.cancel();
}
Expand All @@ -30,6 +31,7 @@ private void cancelAboveBubbleCol(boolean drag, CallbackInfo ci) {
*/
@Inject(method = "onInsideBubbleColumn", at = @At("HEAD"), cancellable = true)
private void cancelInsideBubbleCol(boolean drag, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.BUBBLE_COLUMNS_ENABLED)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class MixinLavaFluid {
*/
@Inject(method = "getDropOff", at = @At("HEAD"), cancellable = true)
private void getLavaDropOff(LevelReader world, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (world instanceof Level) {
if (world.dimensionType().ultraWarm()) {
cir.setReturnValue(((Level) world).getGameRules().getBoolean(RegisterGamerules.LAVA_REACHES_FAR_IN_NETHER) ? 1 : 2);
Expand All @@ -36,6 +37,7 @@ private void getLavaDropOff(LevelReader world, CallbackInfoReturnable<Integer> c
*/
@Inject(method = "getTickDelay", at = @At("HEAD"), cancellable = true)
private void getLavaTickDelay(LevelReader world, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (world instanceof Level) {
if (world.dimensionType().ultraWarm()) {
cir.setReturnValue(((Level) world).getGameRules().getInt(RegisterGamerules.LAVA_FLOW_SPEED_NETHER));
Expand All @@ -52,6 +54,7 @@ private void getLavaTickDelay(LevelReader world, CallbackInfoReturnable<Integer>
*/
@Inject(method = "canConvertToSource", at = @At("HEAD"), cancellable = true)
private void canLavaConvertToSource(CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
cir.setReturnValue(RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.INFINITE_LAVA));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class MixinWaterFluid {
*/
@Inject(method = "getDropOff", at = @At("HEAD"), cancellable = true)
private void getWaterDropOff(LevelReader world, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (world instanceof Level) {
cir.setReturnValue(((Level) world).getGameRules().getBoolean(RegisterGamerules.WATER_REACHES_FAR) ? 1 : 2);
}
Expand All @@ -32,6 +33,7 @@ private void getWaterDropOff(LevelReader world, CallbackInfoReturnable<Integer>
*/
@Inject(method = "getTickDelay", at = @At("HEAD"), cancellable = true)
private void getWaterTickDelay(LevelReader world, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (world instanceof Level) {
cir.setReturnValue(((Level) world).getGameRules().getInt(RegisterGamerules.WATER_FLOW_SPEED));
}
Expand All @@ -44,6 +46,7 @@ private void getWaterTickDelay(LevelReader world, CallbackInfoReturnable<Integer
*/
@Inject(method = "canConvertToSource", at = @At("HEAD"), cancellable = true)
private void canWaterConvertToSource(CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
cir.setReturnValue(RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.INFINITE_WATER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private boolean isInvulnerableToKnockback(LivingEntity source) {
*/
@Inject(method = "knockback", at = @At("HEAD"), cancellable = true)
public void cancelKnockback(double strength, double x, double z, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if ((Object)this instanceof Player && isInvulnerableToKnockback(this.lastHurtByMob)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class MixinServerGamePacketListenerImpl {
*/
@Inject(method = "send", at = @At("HEAD"), cancellable = true)
public void sendPacket(Packet<?> packet, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (packet instanceof ClientboundExplodePacket &&
!(this.player.getLevel().getGameRules().getBoolean(RegisterGamerules.KNOCKBACK_ENABLED) &&
this.player.getLevel().getGameRules().getBoolean(RegisterGamerules.EXPLOSION_KNOCKBACK))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class MixinBeaconBlockEntity {
*/
@Inject(method = "applyEffects", at = @At("HEAD"), cancellable = true)
private static void cancelEffects(Level level, BlockPos blockPos, int i, MobEffect mobEffect, MobEffect mobEffect2, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.BEACONS_ENABLED)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -23,6 +22,7 @@ public MixinBlock() {
*/
@Inject(method = "getFriction", at = @At("HEAD"), cancellable = true)
private void cancelFriction(CallbackInfoReturnable<Float> cir) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.ICE_SLIDING) && this.friction == 0.98F) {
cir.setReturnValue(Blocks.STONE.getFriction());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class MixinConduitBlockEntity {
*/
@Inject(method = "applyEffects", at = @At("HEAD"), cancellable = true)
private static void cancelEffects(Level level, BlockPos blockPos, List<BlockPos> list, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.CONDUITS_ENABLED)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class MixinEndPortalBlock {
*/
@Inject(method = "entityInside", at = @At("HEAD"), cancellable = true)
private void cancelEntityInside(BlockState state, Level level, BlockPos pos, Entity entity, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.END_PORTALS_ENABLED)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class MixinEntity {
*/
@Inject(method = "getDimensionChangingDelay", at = @At("HEAD"), cancellable = true)
private void modifyDimensionChangingDelay(CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
cir.setReturnValue(RegisterGamerules.getServer().getGameRules().getInt(RegisterGamerules.NETHER_PORTAL_COOLDOWN));
}

Expand All @@ -39,6 +40,7 @@ private void modifyDimensionChangingDelay(CallbackInfoReturnable<Integer> cir) {
*/
@Inject(method = "onInsideBlock", at = @At("HEAD"), cancellable = true)
private void killOnHorizontalCollision(BlockState state, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.OLD_BOATS) &&
(Object) this instanceof Boat) {
for (Direction direction : Direction.Plane.HORIZONTAL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public abstract class MixinFarmBlock {
)
)
private void cancelTurningToDirt(BlockState state, Level level, BlockPos pos, Level theWorld, BlockState blockState, BlockPos blockPos, Entity entity, float fallDistance) {
if (RegisterGamerules.getServer() == null) return;
if (RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.CROP_TRAMPLING)) {
level.setBlockAndUpdate(pos, FarmBlock.pushEntitiesUp(state, Blocks.DIRT.defaultBlockState(), level, pos));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class MixinNetherPortalBlock {
*/
@Inject(method = "entityInside", at = @At("HEAD"), cancellable = true)
private void cancelInsideEntity(BlockState state, Level level, BlockPos pos, Entity entity, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.NETHER_PORTALS_ENABLED)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class MixinTheEndGatewayBlockEntity {
*/
@Inject(method = "teleportEntity", at = @At("HEAD"), cancellable = true)
private static void cancelTeleportingEntity(Level level, BlockPos pos, BlockState state, Entity entity, TheEndGatewayBlockEntity blockEntity, CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.END_GATEWAYS_ENABLED)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class MixinItem {
*/
@Inject(method = "getUseDuration", at = @At("HEAD"), cancellable = true)
private void editUseDuration(ItemStack stack, CallbackInfoReturnable<Integer> cir) {
if (RegisterGamerules.getServer() == null) return;
if (RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.OLD_HUNGER) && stack.getItem().isEdible()) {
cir.setReturnValue(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class MixinPlayer {
*/
@Inject(method = "eat", at = @At("HEAD"), cancellable = true)
private void changeEating(Level level, ItemStack stack, CallbackInfoReturnable<ItemStack> cir) {
if (RegisterGamerules.getServer() == null) return;
if (RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.OLD_HUNGER) && stack.getItem().isEdible()) {
((LivingEntity)(Object)this).setHealth(((LivingEntity)(Object)this).getHealth() + stack.getItem().getFoodProperties().getNutrition());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class MixinAbstractPiglin {
*/
@Inject(method = "isImmuneToZombification", at = @At("HEAD"), cancellable = true)
protected void setImmuneToZombification(CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.PIGLINS_CONVERT_TO_ZIGLINS)) {
cir.setReturnValue(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class MixinAbstractVillager {
require = 0
)
private void cancelUses(MerchantOffer tradeOffer, MerchantOffer offer) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.INFINITE_TRADING)) {
offer.increaseUses();
} else {
Expand All @@ -41,6 +42,7 @@ private void cancelUses(MerchantOffer tradeOffer, MerchantOffer offer) {
*/
@Inject(method = "getOffers", at = @At("HEAD"), cancellable = true)
private void clearOffers(CallbackInfoReturnable<MerchantOffers> cir) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.VILLAGER_TRADING_ENABLED)) {
cir.setReturnValue(new MerchantOffers());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class MixinHoglin {
*/
@Inject(method = "isImmuneToZombification", at = @At("HEAD"), cancellable = true)
protected void setImmuneToZombification(CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.HOGLINS_CONVERT_TO_ZOGLINS)) {
cir.setReturnValue(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class MixinHusk {
*/
@Inject(method = "doUnderWaterConversion", at = @At("HEAD"), cancellable = true)
private void cancelConversionInWater(CallbackInfo ci) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.HUSKS_CONVERT_TO_ZOMBIES)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class MixinMob {
*/
@Inject(method = "isSunBurnTick", at = @At("HEAD"), cancellable = true)
private void stopBurning(CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.MOBS_BURN_IN_SUNLIGHT)) {
cir.setReturnValue(false);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/uk/debb/vanilla_disable/mixin/mob/MixinPig.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public MixinPig(EntityType<? extends Animal> entityType, Level level) {
*/
@Inject(method = "isFood", at = @At("HEAD"), cancellable = true)
private void changeFood(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.PIGS_BREED_WITH_WHEAT)) {
cir.setReturnValue(Ingredient.of(Items.WHEAT).test(stack));
}
Expand All @@ -52,6 +53,7 @@ private void changeFood(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
*/
@Overwrite
public void registerGoals() {
if (RegisterGamerules.getServer() == null) return;
this.goalSelector.addGoal(0, new FloatGoal(this));
this.goalSelector.addGoal(1, new PanicGoal(this, 1.25));
this.goalSelector.addGoal(3, new BreedGoal(this, 1.0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class MixinPiglinAi {
*/
@Inject(method = "isBarterCurrency", at = @At("HEAD"), cancellable = true)
private static void cancelBarter(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
if (RegisterGamerules.getServer() == null) return;
if (!RegisterGamerules.getServer().getGameRules().getBoolean(RegisterGamerules.PIGLIN_BARTERING_ENABLED)) {
cir.setReturnValue(false);
}
Expand Down
Loading

0 comments on commit 7fe42b5

Please sign in to comment.