Skip to content

Commit 2769eca

Browse files
committed
Don't you know talking to your plants helps them grow?
1 parent 1f9e42a commit 2769eca

File tree

5 files changed

+164
-20
lines changed

5 files changed

+164
-20
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package tterrag.treesimulator;
2+
3+
import java.util.EnumSet;
4+
5+
import javax.sound.sampled.AudioFormat;
6+
import javax.sound.sampled.AudioSystem;
7+
import javax.sound.sampled.DataLine;
8+
import javax.sound.sampled.LineUnavailableException;
9+
import javax.sound.sampled.TargetDataLine;
10+
11+
import cpw.mods.fml.common.ITickHandler;
12+
import cpw.mods.fml.common.TickType;
13+
import cpw.mods.fml.relauncher.Side;
14+
import cpw.mods.fml.relauncher.SideOnly;
15+
16+
@SideOnly(Side.CLIENT)
17+
public class MicListener implements ITickHandler {
18+
private TargetDataLine line;
19+
private byte[] buffer;
20+
private int bufferOffset;
21+
public int bufferSizeMultiplier = 2;
22+
private boolean isRunning = false;
23+
24+
public void init()
25+
{
26+
AudioFormat format = new AudioFormat(8000.0F, 8, 1, true, true);
27+
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format );
28+
if (!AudioSystem.isLineSupported(info)) {
29+
return;
30+
}
31+
// Obtain and open the line.
32+
try {
33+
line = (TargetDataLine) AudioSystem.getLine(info);
34+
line.open(format);
35+
} catch (LineUnavailableException ex) {
36+
return;
37+
}
38+
line.start();
39+
buffer = new byte[line.getBufferSize() * bufferSizeMultiplier];
40+
bufferOffset = 0;
41+
isRunning = true;
42+
}
43+
44+
public void shutdown()
45+
{
46+
if (isRunning) {
47+
isRunning = false;
48+
line.close();
49+
}
50+
}
51+
52+
@Override
53+
public String getLabel() {
54+
return "treeSimulatorClientTickHandler";
55+
}
56+
57+
@Override
58+
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
59+
}
60+
61+
@Override
62+
public void tickStart(EnumSet<TickType> type, Object... tickData) {
63+
if (isRunning)
64+
{
65+
int available = line.available();
66+
int remaining = buffer.length-bufferOffset;
67+
int bytesRead = line.read(buffer, bufferOffset, Math.min(remaining, available));
68+
bufferOffset += bytesRead;
69+
if (bytesRead < available) {
70+
// buffer is full, analyze data
71+
bufferOffset = 0;
72+
int sum = 0;
73+
for (int i = 0; i < buffer.length; i++) {
74+
sum += buffer[i];
75+
}
76+
double avg = sum / buffer.length;
77+
double sumMeanSquare = 0.0;
78+
for(int i = 0; i < buffer.length; i++) {
79+
sumMeanSquare += Math.pow(buffer[i] - avg, 2.0);
80+
}
81+
82+
double averageMeanSquare = sumMeanSquare / buffer.length;
83+
double rms = Math.pow(averageMeanSquare,0.5d) + 0.5;
84+
if (rms > TreeSimulator.loudnessThreshold) {
85+
System.out.println("Client requested tree growth");
86+
TreeGrower.instance.requestTreeGrowthClient();
87+
}
88+
}
89+
}
90+
}
91+
92+
@Override
93+
public EnumSet<TickType> ticks() {
94+
return EnumSet.of(TickType.CLIENT);
95+
}
96+
}

src/main/java/tterrag/treesimulator/PacketHandlerTGS.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.block.Block;
88
import net.minecraft.block.BlockSapling;
99
import net.minecraft.entity.player.EntityPlayer;
10+
import net.minecraft.entity.player.EntityPlayerMP;
1011
import net.minecraft.network.INetworkManager;
1112
import net.minecraft.network.packet.Packet250CustomPayload;
1213
import cpw.mods.fml.common.network.IPacketHandler;
@@ -15,23 +16,32 @@
1516
public class PacketHandlerTGS implements IPacketHandler{
1617
@Override
1718
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
18-
ByteArrayInputStream byteStream = new ByteArrayInputStream(packet.data);
19-
DataInputStream stream = new DataInputStream(byteStream);
20-
int x, y, z;
21-
try {
22-
x = stream.readInt();
23-
y = stream.readInt();
24-
z = stream.readInt();
25-
} catch (IOException e) {
26-
e.printStackTrace();
27-
return;
19+
if (player instanceof EntityPlayerMP) {
20+
// server side, "request growth" packet
21+
if (TreeSimulator.yellingWorks) {
22+
TreeGrower.instance.requestTreeGrowth((EntityPlayerMP)player);
23+
}
2824
}
29-
30-
EntityPlayer entity = (EntityPlayer) player;
31-
Block block = Block.blocksList[entity.worldObj.getBlockId(x, y, z)];
32-
if (block instanceof BlockSapling)
33-
{
34-
entity.worldObj.playAuxSFX(2005, x, y, z, 0);
25+
else {
26+
// client side, particle effects packet
27+
ByteArrayInputStream byteStream = new ByteArrayInputStream(packet.data);
28+
DataInputStream stream = new DataInputStream(byteStream);
29+
int x, y, z;
30+
try {
31+
x = stream.readInt();
32+
y = stream.readInt();
33+
z = stream.readInt();
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
return;
37+
}
38+
39+
EntityPlayer entity = (EntityPlayer) player;
40+
Block block = Block.blocksList[entity.worldObj.getBlockId(x, y, z)];
41+
if (block instanceof BlockSapling)
42+
{
43+
entity.worldObj.playAuxSFX(2005, x, y, z, 0);
44+
}
3545
}
3646
}
3747
}

src/main/java/tterrag/treesimulator/TickHandlerTGS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public void tickStart(EnumSet<TickType> type, Object... tickData)
4343
{
4444
if ((Math.abs(player.posX - posX) > 0.25 || Math.abs(player.posZ - posZ) > 0.25) && TreeSimulator.sprintingWorks)
4545
{
46-
TreeGrower.requestTreeGrowth((EntityPlayerMP)tickData[0]);
46+
TreeGrower.instance.requestTreeGrowth((EntityPlayerMP)tickData[0]);
4747
}
4848
if ((PlayerState.getState(player.isSneaking()) != state) && TreeSimulator.crouchingWorks)
4949
{
50-
TreeGrower.requestTreeGrowth((EntityPlayerMP)tickData[0]);
50+
TreeGrower.instance.requestTreeGrowth((EntityPlayerMP)tickData[0]);
5151
}
5252
}
5353
else

src/main/java/tterrag/treesimulator/TreeGrower.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import net.minecraftforge.event.entity.player.BonemealEvent;
1616

1717
public class TreeGrower {
18-
private static int movementCounter = 0;
18+
private int movementCounter = 0;
19+
public static TreeGrower instance = new TreeGrower();
1920

2021
private static int[] getNearestSapling(World world, int xpos, int ypos, int zpos) {
2122
for (int x = -5; x <= 5; x++)
@@ -51,7 +52,7 @@ private static void sendPacket(int x, int y, int z, World worldObj, Player playe
5152
PacketDispatcher.sendPacketToPlayer(packet, player);
5253
}
5354

54-
public static void requestTreeGrowth(EntityPlayerMP player) {
55+
public void requestTreeGrowth(EntityPlayerMP player) {
5556
int[] pos = getNearestSapling(player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
5657

5758
if (pos.length == 0)
@@ -79,4 +80,11 @@ public static void requestTreeGrowth(EntityPlayerMP player) {
7980
movementCounter = 0;
8081
}
8182
}
83+
84+
public static void requestTreeGrowthClient() {
85+
Packet250CustomPayload packet = new Packet250CustomPayload();
86+
packet.channel = TreeSimulator.CHANNEL;
87+
packet.length = 0;
88+
PacketDispatcher.sendPacketToServer(packet);
89+
}
8290
}

src/main/java/tterrag/treesimulator/TreeSimulator.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import cpw.mods.fml.common.Mod.Instance;
99
import cpw.mods.fml.common.event.FMLInitializationEvent;
1010
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
11+
import cpw.mods.fml.common.event.FMLServerStartedEvent;
12+
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
1113
import cpw.mods.fml.common.network.NetworkMod;
1214
import cpw.mods.fml.common.registry.TickRegistry;
1315
import cpw.mods.fml.relauncher.Side;
16+
import cpw.mods.fml.relauncher.SideOnly;
1417

1518
@Mod(modid = "treeGrowingSimulator", version = "0.0.3", name = "Tree Growing Simulator 2014")
1619
@NetworkMod(serverSideRequired=true, clientSideRequired=false, channels = {TreeSimulator.CHANNEL}, packetHandler = PacketHandlerTGS.class)
@@ -20,8 +23,13 @@ public class TreeSimulator {
2023
public static boolean showParticles;
2124
public static boolean crouchingWorks;
2225
public static boolean sprintingWorks;
26+
public static boolean yellingWorks;
27+
public static double loudnessThreshold;
2328
public static final String CHANNEL = "TGS2014";
2429

30+
@SideOnly(Side.CLIENT)
31+
public MicListener micListener;
32+
2533
@Instance
2634
public static TreeSimulator instance;
2735

@@ -35,6 +43,9 @@ public void preInit(FMLPreInitializationEvent event)
3543
public void init(FMLInitializationEvent event)
3644
{
3745
TickRegistry.registerTickHandler(new TickHandlerTGS(), Side.SERVER);
46+
if (event.getSide() == Side.CLIENT && yellingWorks) {
47+
micListener = new MicListener();
48+
}
3849
}
3950

4051
private void initConfig(File file)
@@ -47,7 +58,26 @@ private void initConfig(File file)
4758
showParticles = config.get("Tweaks", "showParticles", true, "Show bonemeal particles when appropriate. Not sure why you would turn this off, but eh").getBoolean(true);
4859
crouchingWorks = config.get("Tweaks", "crouchingWorks", true, "Enable crouching to speed growth").getBoolean(true);
4960
sprintingWorks = config.get("Tweaks", "sprintingWorks", true, "Enable sprinting to speed growth").getBoolean(true);
61+
yellingWorks = config.get("Tweaks", "yellingWorks", true, "Enable talking to speed growth").getBoolean(true);
62+
loudnessThreshold = config.get("Tweaks", "loudnessThreshold", 60.0, "What is considered to be the loudness of talking").getDouble(60.0);
5063

5164
config.save();
5265
}
66+
67+
@EventHandler
68+
public void gameStarted(FMLServerStartedEvent e) {
69+
if (e.getSide() == Side.CLIENT && yellingWorks)
70+
{
71+
micListener.init();
72+
TickRegistry.registerTickHandler(micListener, Side.CLIENT);
73+
}
74+
}
75+
76+
@EventHandler
77+
public void gameStopping(FMLServerStoppingEvent e) {
78+
if (e.getSide() == Side.CLIENT && yellingWorks)
79+
{
80+
micListener.shutdown();
81+
}
82+
}
5383
}

0 commit comments

Comments
 (0)