Skip to content

Commit

Permalink
Few bugs fixed, few commands added
Browse files Browse the repository at this point in the history
  • Loading branch information
anOtherAnalyse committed Oct 25, 2020
1 parent 8915bae commit c9f64da
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 51 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ Few commands, use them in chat (Command module must be enabled):

##### get vanished players (online but not shown in the player tab)
```.diff```

##### Disconnect from server
```.disconnect```

##### Open inventory from ride
```.open```
2 changes: 2 additions & 0 deletions src/main/java/family_fun_pack/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public Commands() {
this.registerCommand(new RaytraceCommand());
this.registerCommand(new UnloadedRideCommand());
this.registerCommand(new StalkCommand());
this.registerCommand(new OpenDonkeyCommand());
this.registerCommand(new DisconnectCommand());
}

public void registerCommand(Command cmd) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/family_fun_pack/commands/DisconnectCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package family_fun_pack.commands;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;

import family_fun_pack.FamilyFunPack;

@SideOnly(Side.CLIENT)
public class DisconnectCommand extends Command {

public DisconnectCommand() {
super("disconnect");
}

public String usage() {
return this.getName();
}

public String execute(String[] args) {
FamilyFunPack.getNetworkHandler().disconnect();
return null;
}
}
58 changes: 58 additions & 0 deletions src/main/java/family_fun_pack/commands/OpenDonkeyCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package family_fun_pack.commands;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.passive.EntityDonkey;
import net.minecraft.inventory.ContainerHorseChest;
import net.minecraft.network.EnumPacketDirection;
import net.minecraft.network.Packet;
import net.minecraft.network.play.client.CPacketEntityAction;
import net.minecraft.network.play.server.SPacketOpenWindow;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;

import io.netty.buffer.ByteBuf;

import family_fun_pack.FamilyFunPack;
import family_fun_pack.entities.EntityVoid;
import family_fun_pack.network.PacketListener;

@SideOnly(Side.CLIENT)
public class OpenDonkeyCommand extends Command implements PacketListener {

public OpenDonkeyCommand() {
super("open");
}

public String usage() {
return this.getName();
}

public String execute(String[] args) {
FamilyFunPack.getNetworkHandler().registerListener(EnumPacketDirection.CLIENTBOUND, this, 19);
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketEntityAction(new EntityVoid(Minecraft.getMinecraft().world, 0), CPacketEntityAction.Action.OPEN_INVENTORY));
return null;
}

public void onDisconnect() {
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 19);
}

public Packet<?> packetReceived(EnumPacketDirection direction, int id, Packet<?> packet, ByteBuf in) {
SPacketOpenWindow open = (SPacketOpenWindow) packet;

// Unregister in any case, don't keep listening
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 19);

if("EntityHorse".equals(open.getGuiId())) {
Minecraft mc = Minecraft.getMinecraft();
EntityDonkey fake = new EntityDonkey(mc.world); // Let's assume it's a donkey
fake.setEntityId(open.getEntityId());
fake.setHorseSaddled(true);
fake.setChested(true);
mc.player.openGuiHorseInventory(fake, new ContainerHorseChest(open.getWindowTitle(), open.getSlotCount()));
mc.player.openContainer.windowId = open.getWindowId();
return null;
}
return packet;
}
}
206 changes: 160 additions & 46 deletions src/main/java/family_fun_pack/commands/UnloadedRideCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package family_fun_pack.commands;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.network.EnumPacketDirection;
import net.minecraft.network.Packet;
import net.minecraft.network.play.client.CPacketClickWindow;
import net.minecraft.network.play.client.CPacketPlayerDigging;
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock;
import net.minecraft.network.play.client.CPacketUseEntity;
import net.minecraft.network.play.server.SPacketConfirmTransaction;
import net.minecraft.network.play.server.SPacketSetPassengers;
import net.minecraft.network.play.server.SPacketSpawnObject;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Slot;
Expand All @@ -16,7 +19,9 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;

Expand All @@ -25,87 +30,196 @@
import family_fun_pack.FamilyFunPack;
import family_fun_pack.entities.EntityVoid;
import family_fun_pack.network.PacketListener;
import family_fun_pack.modules.CommandsModule;


// Packets order on item throw (all done when receiving client packet) :
// CPacketClickWindow

// SPacketSpawnObject(this.trackedEntity, 2, 1) - creates entity item
// SPacketEntityMetadata - contains ItemStack info

// SPacketConfirmTransaction
// SPacketSetSlot

@SideOnly(Side.CLIENT)
public class UnloadedRideCommand extends Command implements PacketListener {

private static final int MAX_TRIES = 15;

private int first_id;
private int last_id;
public static int getItemSlot(Item item) {
Minecraft mc = Minecraft.getMinecraft();
int i = 0;
for(Slot slot : mc.player.inventoryContainer.inventorySlots) {
if(slot.getStack().getItem() == item) {
return i;
}
i ++;
}
return -1;
}

private int saved_id;

private int[] limits;

private int[] slots;

private int max_tries;

private boolean success;

private BlockPos target;

public UnloadedRideCommand() {
super("unload");
super("ldride");
this.limits = new int[2];
this.slots = new int[2];
this.target = null;
}

public String usage() {
return this.getName() + " <block_x> <block_y> <block_z>";
return this.getName() + " <reg|exe|get> [break] [nb_tries]";
}

public String execute(String[] args) {
if(args.length > 3) {
if(args.length > 1) {

if(this.target == null) {
Configuration configuration = FamilyFunPack.getModules().getConfiguration();
this.target = new BlockPos(configuration.get(this.getName(), "target_x", 0d).getDouble(), configuration.get(this.getName(), "target_y", 0d).getDouble(), configuration.get(this.getName(), "target_z", 0d).getDouble());
}

Minecraft mc = Minecraft.getMinecraft();
try {
int x = Integer.parseInt(args[1]);
int y = Integer.parseInt(args[2]);
int z = Integer.parseInt(args[3]);
if(args.length > 4) this.max_tries = Integer.parseInt(args[4]);
else this.max_tries = UnloadedRideCommand.MAX_TRIES;

int id = 0;
boolean found = false;
for(Slot slot : mc.player.inventoryContainer.inventorySlots) {
if(slot.getStack().getItem() == Item.getItemFromBlock(Blocks.DIRT)) {
found = true;
break;
if(args[1].equals("get")) {
return String.format("Registered block: (%d, %d, %d)", this.target.getX(), this.target.getY(), this.target.getZ());
} else if(args[1].equals("reg")) {
RayTraceResult target_ray = mc.objectMouseOver;
if(target_ray != null) {
if(target_ray.typeOfHit == RayTraceResult.Type.BLOCK) {
this.target = target_ray.getBlockPos();

// save in configuration file
Configuration configuration = FamilyFunPack.getModules().getConfiguration();
configuration.get(this.getName(), "target_x", 0d).set(this.target.getX());
configuration.get(this.getName(), "target_y", 0d).set(this.target.getY());
configuration.get(this.getName(), "target_z", 0d).set(this.target.getZ());
configuration.save();

return String.format("Registering block: (%d, %d, %d)", this.target.getX(), this.target.getY(), this.target.getZ());
}
}
return "You need to look at a block";
} else if(args[1].equals("exe")) {

this.max_tries = UnloadedRideCommand.MAX_TRIES;
boolean to_break = false;

for(int i = 2; i < args.length; i ++) {
if(args[i].equals("break")) to_break = true;
else {
try {
this.max_tries = Integer.parseInt(args[i]);
} catch(NumberFormatException e) {
return "nb_tries should be a number";
}
}
id ++;
}

if(! found) return "No dirt in your inventory";
this.slots[0] = UnloadedRideCommand.getItemSlot(Item.getItemFromBlock(Blocks.DIRT));
if(this.slots[0] == -1) return "No dirt in your inventory";

this.slots[1] = UnloadedRideCommand.getItemSlot(Item.getItemFromBlock(Blocks.COBBLESTONE));
if(this.slots[1] == -1) return "No cobblestone in your inventory";

FamilyFunPack.getNetworkHandler().registerListener(EnumPacketDirection.CLIENTBOUND, this, 0, 17);

Vec3d look = Minecraft.getMinecraft().player.getLookVec();
this.success = true;

this.first_id = -1;
this.last_id = -1;
FamilyFunPack.getNetworkHandler().registerListener(EnumPacketDirection.CLIENTBOUND, this, 0);
// Drop first item
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketClickWindow(0, this.slots[0], 0, ClickType.THROW, ItemStack.EMPTY, (short)0));

FamilyFunPack.getNetworkHandler().sendPacket(new CPacketClickWindow(0, id, 0, ClickType.THROW, ItemStack.EMPTY, (short)0));
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.START_DESTROY_BLOCK, new BlockPos(x, y, z), EnumFacing.UP));
//FamilyFunPack.getNetworkHandler().sendPacket(new CPacketPlayerTryUseItemOnBlock(new BlockPos(x, y, z), EnumFacing.UP, EnumHand.MAIN_HAND, (float)look.x, (float)look.y, (float)look.z));
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketClickWindow(0, id, 0, ClickType.THROW, ItemStack.EMPTY, (short)1));
// Load unloaded chunk (by breaking / using block)
if(to_break)
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.START_DESTROY_BLOCK, this.target, EnumFacing.UP));
else {
Vec3d look = mc.player.getLookVec();
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketPlayerTryUseItemOnBlock(this.target, EnumFacing.UP, EnumHand.MAIN_HAND, (float)look.x, (float)look.y, (float)look.z));
}

// Drop second item
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketClickWindow(0, this.slots[1], 0, ClickType.THROW, ItemStack.EMPTY, (short)1));

} catch(NumberFormatException e) {
return "Wrong block coordinates";
return null;
}
} else return "Which coordinates ?";
return null;
}
return this.usage();
}

public void onDisconnect() {
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 0);
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 0, 17, 67);
}

public Packet<?> packetReceived(EnumPacketDirection direction, int id, Packet<?> packet, ByteBuf in) {
SPacketSpawnObject spawn = (SPacketSpawnObject) packet;
if(spawn.getType() == 2) { // Object
if(this.first_id == -1) this.first_id = spawn.getEntityID();
else if(this.last_id == -1) {
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 0);
this.last_id = spawn.getEntityID();
FamilyFunPack.printMessage("Entity id from " + Integer.toString(this.first_id) + " to " + Integer.toString(this.last_id));

if(! ItemStack.areItemStacksEqual(Minecraft.getMinecraft().player.getHeldItem(EnumHand.MAIN_HAND), ItemStack.EMPTY)) {
FamilyFunPack.printMessage("Your main hand is not empty !");
if(id == 0) { // SPacketSpawnObject
SPacketSpawnObject spawn = (SPacketSpawnObject) packet;
if(spawn.getType() == 2) { // Object
this.saved_id = spawn.getEntityID();
}
} else if(id == 17) { // SPacketConfirmTransaction
SPacketConfirmTransaction confirm = (SPacketConfirmTransaction) packet;
if(confirm.getWindowId() == 0 && (confirm.getActionNumber() == 0 || confirm.getActionNumber() == 1)) {

Minecraft mc = Minecraft.getMinecraft();

if(! confirm.wasAccepted()) {
FamilyFunPack.printMessage("Fail, unable to drop " + (confirm.getActionNumber() == 0 ? "first" : "second") + " item");
this.success = false;
} else {
int start = this.first_id + 1;
mc.player.inventoryContainer.getSlot(this.slots[confirm.getActionNumber()]).getStack().shrink(1);
this.limits[confirm.getActionNumber()] = this.saved_id;
}

if(confirm.getActionNumber() == 1) {
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 0, 17);

if(! this.success) return packet;

// Try to ride
int start = this.limits[0] + 1;
int stop = start + this.max_tries;
if(stop > this.last_id) stop = this.last_id;
if(stop > this.limits[1]) stop = this.limits[1];

if(stop <= start) {
FamilyFunPack.printMessage("Fail, no new entities were loaded");
return packet;
}

FamilyFunPack.printMessage("Entity id from " + Integer.toString(start) + " to " + Integer.toString(stop - 1));

FamilyFunPack.getNetworkHandler().registerListener(EnumPacketDirection.CLIENTBOUND, this, 67);

for(int i = start; i < stop; i ++) {
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketUseEntity(new EntityVoid(Minecraft.getMinecraft().world, i), EnumHand.MAIN_HAND));
FamilyFunPack.getNetworkHandler().sendPacket(new CPacketUseEntity(new EntityVoid(mc.world, i), EnumHand.MAIN_HAND));
}

// Try to open donkey inventory
((CommandsModule)FamilyFunPack.getModules().getByName("FFP Commands")).getCommand("open").execute(new String[0]);
}
}
} else { // SPacketSetPassengers
FamilyFunPack.getNetworkHandler().unregisterListener(EnumPacketDirection.CLIENTBOUND, this, 67);

SPacketSetPassengers passenger = (SPacketSetPassengers) packet;
Minecraft mc = Minecraft.getMinecraft();
Entity ride = mc.player.getRidingEntity();
if(ride != null && ride.getEntityId() == passenger.getEntityId()) {
for(int i : passenger.getPassengerIds()) {
if(i == mc.player.getEntityId()) return packet;
}
mc.player.dismountRidingEntity();
mc.player.setPosition(ride.posX, ride.posY, ride.posZ); // set position so we don't fall on unloaded chunk
return null;
}
}
return packet;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/family_fun_pack/key/KeyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void onKey(KeyInputEvent event) {
Module m = this.actions.get(i);
m.toggle();
m.save(FamilyFunPack.getModules().getConfiguration());
FamilyFunPack.getModules().getConfiguration().save();
return;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/family_fun_pack/modules/CommandsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public CommandsModule() {
this.commands = new Commands();
}

public Command getCommand(String name) {
return this.commands.getCommand(name);
}

protected void enable() {
MinecraftForge.EVENT_BUS.register(this);
}
Expand Down
Loading

0 comments on commit c9f64da

Please sign in to comment.