generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/plusls/carpet/mixin/rule/playerOperationLimiter/MixinBlockItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.plusls.carpet.mixin.rule.playerOperationLimiter; | ||
|
||
import com.plusls.carpet.PcaSettings; | ||
import com.plusls.carpet.util.rule.playerOperationLimiter.SafeServerPlayerEntity; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.item.BlockItem; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemPlacementContext; | ||
import net.minecraft.util.ActionResult; | ||
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.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(BlockItem.class) | ||
public abstract class MixinBlockItem extends Item { | ||
public MixinBlockItem(Settings settings) { | ||
super(settings); | ||
} | ||
|
||
@Shadow | ||
public abstract ItemPlacementContext getPlacementContext(ItemPlacementContext context); | ||
|
||
@Shadow | ||
protected abstract BlockState getPlacementState(ItemPlacementContext context); | ||
|
||
@Inject(method = "place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;", at = @At(value = "HEAD"), cancellable = true) | ||
private void checkOperationCountPerTick(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> cir) { | ||
if (!PcaSettings.playerOperationLimiter || context.getWorld().isClient()) { | ||
return; | ||
} | ||
|
||
if (context.canPlace()) { | ||
ItemPlacementContext itemPlacementContext = this.getPlacementContext(context); | ||
SafeServerPlayerEntity safeServerPlayerEntity = (SafeServerPlayerEntity) context.getPlayer(); | ||
if (safeServerPlayerEntity != null && itemPlacementContext != null && this.getPlacementState(itemPlacementContext) != null) { | ||
safeServerPlayerEntity.addPlaceBlockCountPerTick(); | ||
if (!safeServerPlayerEntity.allowOperation()) { | ||
cir.setReturnValue(ActionResult.FAIL); | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ain/java/com/plusls/carpet/mixin/rule/playerOperationLimiter/MixinServerPlayerEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.plusls.carpet.mixin.rule.playerOperationLimiter; | ||
|
||
import com.plusls.carpet.util.rule.playerOperationLimiter.SafeServerPlayerEntity; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(ServerPlayerEntity.class) | ||
public class MixinServerPlayerEntity implements SafeServerPlayerEntity { | ||
private int instaBreakCountPerTick = 0; | ||
private int placeBlockCountPerTick = 0; | ||
|
||
@Inject(method = "tick", at = @At(value = "HEAD")) | ||
private void resetOperationCountPerTick(CallbackInfo ci) { | ||
instaBreakCountPerTick = 0; | ||
placeBlockCountPerTick = 0; | ||
} | ||
|
||
@Override | ||
public int getInstaBreakCountPerTick() { | ||
return instaBreakCountPerTick; | ||
} | ||
|
||
@Override | ||
public int getPlaceBlockCountPerTick() { | ||
return placeBlockCountPerTick; | ||
} | ||
|
||
@Override | ||
public void addInstaBreakCountPerTick() { | ||
++instaBreakCountPerTick; | ||
} | ||
|
||
@Override | ||
public void addPlaceBlockCountPerTick() { | ||
++placeBlockCountPerTick; | ||
} | ||
|
||
@Override | ||
public boolean allowOperation() { | ||
return (instaBreakCountPerTick == 0 || placeBlockCountPerTick == 0) && (instaBreakCountPerTick <= 1 && placeBlockCountPerTick <= 2); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.../plusls/carpet/mixin/rule/playerOperationLimiter/MixinServerPlayerInteractionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.plusls.carpet.mixin.rule.playerOperationLimiter; | ||
|
||
import com.plusls.carpet.PcaSettings; | ||
import com.plusls.carpet.util.rule.playerOperationLimiter.SafeServerPlayerEntity; | ||
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket; | ||
import net.minecraft.network.packet.s2c.play.PlayerActionResponseS2CPacket; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.network.ServerPlayerInteractionManager; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Direction; | ||
import org.spongepowered.asm.mixin.Final; | ||
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.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(ServerPlayerInteractionManager.class) | ||
public class MixinServerPlayerInteractionManager { | ||
|
||
@Final | ||
@Shadow | ||
protected ServerPlayerEntity player; | ||
|
||
@Shadow | ||
protected ServerWorld world; | ||
|
||
@Inject(method = "processBlockBreakingAction", at=@At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerInteractionManager;finishMining(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/network/packet/c2s/play/PlayerActionC2SPacket$Action;Ljava/lang/String;)V", ordinal = 1), cancellable = true) | ||
private void checkOperationCountPerTick(BlockPos pos, PlayerActionC2SPacket.Action action, Direction direction, int worldHeight, CallbackInfo ci) { | ||
if (!PcaSettings.playerOperationLimiter) { | ||
return; | ||
} | ||
SafeServerPlayerEntity safeServerPlayerEntity = (SafeServerPlayerEntity)player; | ||
safeServerPlayerEntity.addInstaBreakCountPerTick(); | ||
if (!safeServerPlayerEntity.allowOperation()) { | ||
this.player.networkHandler.sendPacket(new PlayerActionResponseS2CPacket(pos, this.world.getBlockState(pos), action, false, "insta mine")); | ||
ci.cancel(); | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/plusls/carpet/util/rule/playerOperationLimiter/SafeServerPlayerEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.plusls.carpet.util.rule.playerOperationLimiter; | ||
|
||
public interface SafeServerPlayerEntity { | ||
int getInstaBreakCountPerTick(); | ||
int getPlaceBlockCountPerTick(); | ||
void addInstaBreakCountPerTick(); | ||
void addPlaceBlockCountPerTick(); | ||
boolean allowOperation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters